Skip to content

Commit

Permalink
[16.0][FIX] auditlog: Add/Remove User Group
Browse files Browse the repository at this point in the history
When adding/removing a group in a user (and tracking a user or a partner), odoo sends a 'reified' val 'in_group_{group_id}'. We must convert this value into the real groups_id one.
  • Loading branch information
BT-anieto committed Feb 15, 2024
1 parent a3abec1 commit 8de1324
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions auditlog/models/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ def write_full(self, vals, **kwargs):
.with_context(prefetch_fields=False)
.read(fields_list)
}
# invalidate_recordset method must be called with existing fields
if self._name == "res.users":
vals = self._remove_reified_groups(vals)
# Prevent the cache of modified fields from being poisoned by
# x2many items inaccessible to the current user.
self.invalidate_recordset(vals.keys())
Expand Down
79 changes: 79 additions & 0 deletions auditlog/tests/test_auditlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from odoo.tests.common import Form, TransactionCase

from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
from odoo.addons.base.models.res_users import name_boolean_group


class AuditlogCommon(object):
Expand Down Expand Up @@ -612,3 +613,81 @@ def test_06_AuditlogFull_unlink_log(self):

# Removing auditlog_rule
self.auditlog_rule.unlink()


class AuditLogRuleTestForUserModel(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# get User model id
cls.user_model_id = cls.env["ir.model"].search([("model", "=", "res.users")]).id

# creating auditlog.rule
cls.auditlog_rule = (
cls.env["auditlog.rule"]
.with_context(tracking_disable=True)
.create(
{
"name": "testrule 01",
"model_id": cls.user_model_id,
"log_read": True,
"log_create": True,
"log_write": True,
"log_unlink": True,
"log_type": "full",
"capture_record": True,
}
)
)

# Subscribe auditlog.rule
cls.auditlog_rule.subscribe()

# Create user id
cls.user = (
cls.env["res.users"]
.with_context(no_reset_password=True, tracking_disable=True)
.create(
{
"name": "Test User",
"login": "testuser",
}
)
)
cls.group = cls.env.ref("auditlog.group_auditlog_manager")

cls.auditlog_log = cls.env["auditlog.log"]

def test_01_AuditlogFull_field_group_write_log(self):
"""Change group and check successfully created log"""
self.user.with_context(tracking_disable=True).write(
{"groups_id": [(4, self.group.id)]}
)
# Checking log is created for testpartner1
write_log_record = self.auditlog_log.search(
[
("model_id", "=", self.auditlog_rule.model_id.id),
("method", "=", "write"),
("res_id", "=", self.user.id),
]
).ensure_one()
self.assertTrue(write_log_record)

def test_02_AuditlogFull_field_group_write_log(self):
"""Change group and check successfully created log, but using reified fields"""
fname = name_boolean_group(self.group.id)

self.user.with_context(tracking_disable=True).write(
{
fname: True,
}
)
# Checking log is created for testpartner1
write_log_record = self.auditlog_log.search(
[
("model_id", "=", self.auditlog_rule.model_id.id),
("method", "=", "write"),
("res_id", "=", self.user.id),
]
).ensure_one()
self.assertTrue(write_log_record)

1 comment on commit 8de1324

@agajic-modoolar
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @BT-anieto
This commit breaks some of our tests of a module that depends on auditlog.
It would be great if you had added something like this:

    @classmethod
    def tearDownClass(cls):
        cls.auditlog_rule.unsubscribe()
        return super().tearDownClass()

Even though DB gets rolled back, monkey patches remain active because unsubscribe never happened.
Thanks!

Please sign in to comment.