From 9d6feea45d825aa54a57c2d5629007ddd4816338 Mon Sep 17 00:00:00 2001 From: sebalix Date: Thu, 21 Sep 2017 11:21:34 +0200 Subject: [PATCH 1/3] [FIX] base_user_role: update users' groups when a role is deleted --- base_user_role/models/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base_user_role/models/role.py b/base_user_role/models/role.py index 96c874143..3504b5a7e 100644 --- a/base_user_role/models/role.py +++ b/base_user_role/models/role.py @@ -50,7 +50,7 @@ class ResUsersRole(models.Model): @api.multi def unlink(self): - users = self.mapped('user_id') + users = self.mapped('user_ids') res = super(ResUsersRole, self).unlink() users.set_groups_from_roles() return res From 15a081df1f3f2a97cdd85a2aea0a19d16eedb6d1 Mon Sep 17 00:00:00 2001 From: sebalix Date: Sat, 23 Sep 2017 20:24:41 +0200 Subject: [PATCH 2/3] [IMP] base_user_role: add some test cases to check user's groups when its last role line is removed --- base_user_role/tests/test_user_role.py | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/base_user_role/tests/test_user_role.py b/base_user_role/tests/test_user_role.py index 08a1f613c..56d42654b 100644 --- a/base_user_role/tests/test_user_role.py +++ b/base_user_role/tests/test_user_role.py @@ -105,3 +105,53 @@ class TestUserRole(TransactionCase): role1_group_ids.append(role1.group_id.id) role_group_ids = sorted(set(role1_group_ids)) self.assertEqual(user_group_ids, role_group_ids) + + def test_role_unlink(self): + # Get role1 groups + role1 = self.role_model.browse(self.cr, self.uid, self.role1_id) + role1_group_ids = role1.implied_ids.ids + role1_group_ids.append(role1.group_id.id) + role1_group_ids = sorted(set(role1_group_ids)) + # Get role2 + role2 = self.role_model.browse(self.cr, self.uid, self.role2_id) + # Configure the user with role1 and role2 + self.user_model.write( + self.cr, self.uid, [self.user_id], + {'role_line_ids': [ + (0, 0, {'role_id': self.role1_id}), + (0, 0, {'role_id': self.role2_id}), + ]}) + user = self.user_model.browse(self.cr, self.uid, self.user_id) + # Remove role2 + role2.unlink() + user_group_ids = sorted(set([group.id for group in user.groups_id])) + self.assertEqual(user_group_ids, role1_group_ids) + # Remove role1 + role1.unlink() + user_group_ids = sorted(set([group.id for group in user.groups_id])) + self.assertEqual(user_group_ids, []) + + def test_role_line_unlink(self): + # Get role1 groups + role1 = self.role_model.browse(self.cr, self.uid, self.role1_id) + role1_group_ids = role1.implied_ids.ids + role1_group_ids.append(role1.group_id.id) + role1_group_ids = sorted(set(role1_group_ids)) + # Configure the user with role1 and role2 + self.user_model.write( + self.cr, self.uid, [self.user_id], + {'role_line_ids': [ + (0, 0, {'role_id': self.role1_id}), + (0, 0, {'role_id': self.role2_id}), + ]}) + user = self.user_model.browse(self.cr, self.uid, self.user_id) + # Remove role2 from the user + user.role_line_ids.filtered( + lambda l: l.role_id.id == self.role2_id).unlink() + user_group_ids = sorted(set([group.id for group in user.groups_id])) + self.assertEqual(user_group_ids, role1_group_ids) + # Remove role1 from the user + user.role_line_ids.filtered( + lambda l: l.role_id.id == self.role1_id).unlink() + user_group_ids = sorted(set([group.id for group in user.groups_id])) + self.assertEqual(user_group_ids, []) From 45c766903e6560564e0bf4ac3db94e89fb2dbb8b Mon Sep 17 00:00:00 2001 From: sebalix Date: Sat, 23 Sep 2017 20:33:09 +0200 Subject: [PATCH 3/3] [FIX] base_user_role: ensure that the user's groups are updated when its last role line is removed --- base_user_role/models/role.py | 4 ++-- base_user_role/models/user.py | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/base_user_role/models/role.py b/base_user_role/models/role.py index 3504b5a7e..67f4b8405 100644 --- a/base_user_role/models/role.py +++ b/base_user_role/models/role.py @@ -52,7 +52,7 @@ class ResUsersRole(models.Model): def unlink(self): users = self.mapped('user_ids') res = super(ResUsersRole, self).unlink() - users.set_groups_from_roles() + users.set_groups_from_roles(force=True) return res @api.multi @@ -99,5 +99,5 @@ class ResUsersRoleLine(models.Model): def unlink(self): users = self.mapped('user_id') res = super(ResUsersRoleLine, self).unlink() - users.set_groups_from_roles() + users.set_groups_from_roles(force=True) return res diff --git a/base_user_role/models/user.py b/base_user_role/models/user.py index a15849ced..5f06dbae9 100644 --- a/base_user_role/models/user.py +++ b/base_user_role/models/user.py @@ -32,12 +32,13 @@ class ResUsers(models.Model): return res @api.multi - def set_groups_from_roles(self): + def set_groups_from_roles(self, force=False): """Set (replace) the groups following the roles defined on users. - If no role is defined on the user, its groups are let untouched. + If no role is defined on the user, its groups are let untouched unless + the `force` parameter is `True`. """ for user in self: - if not user.role_line_ids: + if not user.role_line_ids and not force: continue group_ids = [] role_lines = user.role_line_ids.filtered(