From ee689a59451ccef794a0ee195967956c5a1daa16 Mon Sep 17 00:00:00 2001 From: Pierre Verkest Date: Fri, 3 Jun 2022 13:11:50 +0000 Subject: [PATCH] [FIX] base: discard clear_cache on method res_users.has_group Overriding method has_group() in some extension of model 'res.users' is not possible because of code invoking model.has_group.clear_cache(). As clear_cache() is no longer invoked on ormcached methods, we can simply replace the code by a global cache invalidation. In this case, it is not even necessary, since method call_cache_clearing_methods() does it already. closes odoo/odoo#95071 X-original-commit: 2dc80fe49ec2cb11e65465d4616b9bbb136bf6ab Signed-off-by: Raphael Collet --- odoo/addons/base/models/res_users.py | 3 --- odoo/addons/base/tests/test_user_has_group.py | 25 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/odoo/addons/base/models/res_users.py b/odoo/addons/base/models/res_users.py index cc556069aae..1188d93a1ac 100644 --- a/odoo/addons/base/models/res_users.py +++ b/odoo/addons/base/models/res_users.py @@ -215,7 +215,6 @@ class Groups(models.Model): # DLE P139 if self.ids: self.env['ir.model.access'].call_cache_clearing_methods() - self.env['res.users'].has_group.clear_cache(self.env['res.users']) return super(Groups, self).write(vals) @@ -910,8 +909,6 @@ class Users(models.Model): (SELECT res_id FROM ir_model_data WHERE module=%s AND name=%s)""", (self._uid, module, ext_id)) return bool(self._cr.fetchone()) - # for a few places explicitly clearing the has_group cache - has_group.clear_cache = _has_group.clear_cache def _action_show(self): """If self is a singleton, directly access the form view. If it is a recordset, open a tree view""" diff --git a/odoo/addons/base/tests/test_user_has_group.py b/odoo/addons/base/tests/test_user_has_group.py index 27bec76e73c..141ca697fa1 100644 --- a/odoo/addons/base/tests/test_user_has_group.py +++ b/odoo/addons/base/tests/test_user_has_group.py @@ -250,3 +250,28 @@ class TestHasGroup(TransactionCase): with self.assertRaises(ValidationError): user_b.write({"groups_id": [Command.link(group_C.id)]}) + + def test_has_group_cleared_cache_on_write(self): + self.registry._clear_cache() + self.assertFalse(self.registry._Registry__cache, "Ensure ormcache is empty") + + def populate_cache(): + self.test_user.has_group('test_user_has_group.group0') + self.assertTrue(self.registry._Registry__cache, "user.has_group cache must be populated") + + populate_cache() + + self.env.ref(self.group0).write({"share": True}) + self.assertFalse(self.registry._Registry__cache, "Writing on group must invalidate user.has_group cache") + + populate_cache() + # call_cache_clearing_methods is called in res.groups.write to invalidate + # cache before calling its parent class method (`odoo.models.Model.write`) + # as explain in the `res.group.write` comment. + # This verifies that calling `call_cache_clearing_methods()` invalidates + # the ormcache of method `user.has_group()` + self.env['ir.model.access'].call_cache_clearing_methods() + self.assertFalse( + self.registry._Registry__cache, + "call_cache_clearing_methods() must invalidate user.has_group cache" + )