Skip to content

Commit

Permalink
[IMP] impersonate_login: Update create_uid for impersonated record cr…
Browse files Browse the repository at this point in the history
…eation

- Ensure impersonation is properly reflected in record creation
- Update related tests to verify correct impersonation behavior
  • Loading branch information
toita86 committed Nov 7, 2024
1 parent f997464 commit 79f2454
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 15 additions & 3 deletions impersonate_login/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@
# @author Kévin Roche <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from psycopg2.extensions import AsIs

from odoo import api, models
from odoo.http import request


class BaseModel(models.AbstractModel):
_inherit = "base"

@api.model_create_multi
@api.model
def _create(self, data_list):
res = super()._create(data_list)
if (
request
and request.session.impersonate_from_uid
and "create_uid" in self._fields
):
for rec in res:
rec["create_uid"] = request.session.impersonate_from_uid
self.env.cr.execute(
"""
UPDATE %(table)s
SET create_uid = %(impersonator_id)s
WHERE id IN %(record_ids)s
""",
{
"table": AsIs(self._table),
"impersonator_id": request.session.impersonate_from_uid,
"record_ids": tuple(rec.id for rec in res),
},
)
return res

def write(self, vals):
Expand Down
4 changes: 4 additions & 0 deletions impersonate_login/tests/test_impersonate_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def test_01_admin_impersonates_user_demo(self):

# Check impersonate log
log2 = self.env["impersonate.log"].search([], order="id desc", limit=1)
# Refresh the log1 after the attribute date_end is updated
log1.refresh()
self.assertEqual(log1, log2)
self.assertTrue(log1.date_start)
self.assertTrue(log1.date_end)
Expand Down Expand Up @@ -254,6 +256,8 @@ def test_04_write_uid(self):
data = response.json()
result = data["result"]

# Refresh contact to reflect changes in the database
self.assertEqual(result, True)
contact.invalidate_cache()
self.assertEqual(contact.ref, "abc")
self.assertEqual(contact.write_uid, self.admin_user)

0 comments on commit 79f2454

Please sign in to comment.