diff --git a/base_user_role/README.rst b/base_user_role/README.rst index 4081f31dd..215f9a641 100644 --- a/base_user_role/README.rst +++ b/base_user_role/README.rst @@ -33,6 +33,9 @@ To configure this module, you need to go to *Configuration / Users / Roles*, and create a new role. From there, you can add groups to compose your role, and then associate users to it. +You can also define default roles for a new user by editing the user called +"Default User". + Roles: .. image:: /base_user_role/static/description/roles.png diff --git a/base_user_role/__manifest__.py b/base_user_role/__manifest__.py index 0fdafe5fa..6ee7c64fd 100644 --- a/base_user_role/__manifest__.py +++ b/base_user_role/__manifest__.py @@ -3,7 +3,7 @@ { 'name': 'User roles', - 'version': '11.0.1.0.0', + 'version': '11.0.1.0.1', 'category': 'Tools', 'author': 'ABF OSIELL, Odoo Community Association (OCA)', 'license': 'AGPL-3', diff --git a/base_user_role/models/user.py b/base_user_role/models/user.py index f1c406efc..8413f1535 100644 --- a/base_user_role/models/user.py +++ b/base_user_role/models/user.py @@ -8,11 +8,29 @@ class ResUsers(models.Model): role_line_ids = fields.One2many( comodel_name='res.users.role.line', - inverse_name='user_id', string="Role lines") + inverse_name='user_id', + string="Role lines", + default=lambda self: self._default_role_lines() + ) role_ids = fields.One2many( comodel_name='res.users.role', string="Roles", compute='_compute_role_ids') + @api.model + def _default_role_lines(self): + default_user = self.env.ref( + 'base.default_user', raise_if_not_found=False) + default_values = [] + if default_user: + for role_line in default_user.role_line_ids: + default_values.append({ + 'role_id': role_line.role_id.id, + 'date_from': role_line.date_from, + 'date_to': role_line.date_to, + 'is_enabled': role_line.is_enabled, + }) + return default_values + @api.multi @api.depends('role_line_ids.role_id') def _compute_role_ids(self): diff --git a/base_user_role/tests/test_user_role.py b/base_user_role/tests/test_user_role.py index bc5f8ff65..18535ea3f 100644 --- a/base_user_role/tests/test_user_role.py +++ b/base_user_role/tests/test_user_role.py @@ -13,6 +13,7 @@ class TestUserRole(TransactionCase): self.user_model = self.env['res.users'] self.role_model = self.env['res.users.role'] + self.default_user = self.env.ref('base.default_user') self.user_id = self.user_model.create( {'name': "USER TEST (ROLES)", 'login': 'user_test_roles'}) @@ -140,3 +141,21 @@ class TestUserRole(TransactionCase): user_group_ids = sorted(set([ group.id for group in self.user_id.groups_id])) self.assertEqual(user_group_ids, []) + + def test_default_user_roles(self): + self.default_user.write({ + 'role_line_ids': [ + (0, 0, { + 'role_id': self.role1_id.id, + }), + (0, 0, { + 'role_id': self.role2_id.id, + }) + ] + }) + user = self.user_model.create({ + 'name': "USER TEST (DEFAULT ROLES)", + 'login': 'user_test_default_roles' + }) + roles = self.role_model.browse([self.role1_id.id, self.role2_id.id]) + self.assertEqual(user.role_ids, roles) diff --git a/base_user_role/views/role.xml b/base_user_role/views/role.xml index 6422d9b53..bd99f11f6 100644 --- a/base_user_role/views/role.xml +++ b/base_user_role/views/role.xml @@ -7,7 +7,7 @@ res.users.role.form res.users.role -
+ @@ -19,7 +19,7 @@ - + @@ -37,7 +37,7 @@ res.users.role.tree res.users.role - + diff --git a/base_user_role/views/user.xml b/base_user_role/views/user.xml index 4319940e9..09ba5e663 100644 --- a/base_user_role/views/user.xml +++ b/base_user_role/views/user.xml @@ -11,7 +11,7 @@ - +