changeset 1:b2249314c1af 4.8

Synchronize user after role changed
author Cédric Krier <ced@b2ck.com>
date Mon, 03 Sep 2018 16:57:14 +0200
parents c45430f9a8de
children ea5ff9f9ef91
files res.py tests/scenario_user_role.rst
diffstat 2 files changed, 21 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/res.py	Mon Sep 03 15:41:38 2018 +0200
+++ b/res.py	Mon Sep 03 16:57:14 2018 +0200
@@ -13,6 +13,18 @@
     name = fields.Char("Name", required=True, translate=True)
     groups = fields.Many2Many('res.role-res.group', 'role', 'group', "Groups")
 
+    @classmethod
+    def write(cls, *args):
+        pool = Pool()
+        User = pool.get('res.user')
+        roles = sum(args[0:None:2], [])
+        super(Role, cls).write(*args)
+
+        users = User.search([
+                ('roles', 'in', [r.id for r in roles]),
+                ])
+        User.sync_roles(users)
+
 
 class RoleGroup(ModelSQL):
     "Role - Group"
--- a/tests/scenario_user_role.rst	Mon Sep 03 15:41:38 2018 +0200
+++ b/tests/scenario_user_role.rst	Mon Sep 03 16:57:14 2018 +0200
@@ -43,8 +43,6 @@
     >>> user_role.role = role
     >>> user.save()
 
-Check groups::
-
     >>> len(user.groups)
     2
     >>> user.groups == [groups[0], groups[1]]
@@ -57,8 +55,6 @@
     >>> user_role.to_date = None
     >>> user.save()
 
-Check no groups::
-
     >>> len(user.groups)
     0
 
@@ -69,8 +65,6 @@
     >>> user_role.to_date = datetime.date.today() - datetime.timedelta(days=1)
     >>> user.save()
 
-Check no groups::
-
     >>> len(user.groups)
     0
 
@@ -81,7 +75,14 @@
     >>> user_role.to_date = datetime.date.today() + datetime.timedelta(days=1)
     >>> user.save()
 
-Check no groups::
-
     >>> len(user.groups)
     2
+
+Changing groups to role::
+
+    >>> role.groups.append(Group(groups[4].id))
+    >>> role.save()
+
+    >>> user.reload()
+    >>> len(user.groups)
+    3