From 588e8e0daaa206f489ae93679cc3389e586599ed Mon Sep 17 00:00:00 2001 From: Kevin Khao Date: Wed, 4 Mar 2020 11:45:57 +0100 Subject: [PATCH] [IMP] base_user_role: Added multicompany support from commit server-tool repo commit 840c2c1119460424230c935da24ec42f95c21122 --- base_user_role/README.rst | 36 +++------- base_user_role/__manifest__.py | 2 +- base_user_role/models/role.py | 5 +- base_user_role/models/user.py | 13 ++-- base_user_role/readme/CONTRIBUTORS.rst | 2 + base_user_role/static/description/index.html | 19 +++-- base_user_role/tests/test_user_role.py | 74 ++++++++++++++++++++ base_user_role/views/role.xml | 1 + base_user_role/views/user.xml | 1 + 9 files changed, 111 insertions(+), 42 deletions(-) diff --git a/base_user_role/README.rst b/base_user_role/README.rst index 1ad67ef5..944af875 100644 --- a/base_user_role/README.rst +++ b/base_user_role/README.rst @@ -13,17 +13,11 @@ User roles .. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 -.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github - :target: https://github.com/OCA/server-backend/tree/12.0/base_user_role - :alt: OCA/server-backend -.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/server-backend-12-0/server-backend-12-0-base_user_role - :alt: Translate me on Weblate -.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/253/12.0 - :alt: Try me on Runbot +.. |badge3| image:: https://img.shields.io/badge/github-oca%2Fserver--backend-lightgray.png?logo=github + :target: https://github.com/oca/server-backend/tree/12.0/base_user_role + :alt: oca/server-backend -|badge1| |badge2| |badge3| |badge4| |badge5| +|badge1| |badge2| |badge3| This module was written to extend the standard functionality regarding users and groups management. @@ -98,10 +92,10 @@ To use this module, you need to: Bug Tracker =========== -Bugs are tracked on `GitHub Issues `_. +Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -119,6 +113,8 @@ Contributors * Sébastien Alix * Duc, Dao Dong (https://komit-consulting.com) * Jean-Charles Drubay (https://komit-consulting.com) +* Pierrick Brun +* Kevin Khao Do not contact contributors directly about support or help with technical issues. @@ -133,16 +129,6 @@ Images Maintainers ~~~~~~~~~~~ -This module is maintained by the OCA. - -.. image:: https://odoo-community.org/logo.png - :alt: Odoo Community Association - :target: https://odoo-community.org - -OCA, or the Odoo Community Association, is a nonprofit organization whose -mission is to support the collaborative development of Odoo features and -promote its widespread use. - .. |maintainer-ABF OSIELL| image:: https://github.com/ABF OSIELL.png?size=40px :target: https://github.com/ABF OSIELL :alt: ABF OSIELL @@ -150,10 +136,10 @@ promote its widespread use. :target: https://github.com/jcdrubay :alt: jcdrubay -Current `maintainers `__: +Current maintainers: |maintainer-ABF OSIELL| |maintainer-jcdrubay| -This module is part of the `OCA/server-backend `_ project on GitHub. +This module is part of the `oca/server-backend `_ project on GitHub. -You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. +You are welcome to contribute. diff --git a/base_user_role/__manifest__.py b/base_user_role/__manifest__.py index 90b7ff39..a1526dbc 100644 --- a/base_user_role/__manifest__.py +++ b/base_user_role/__manifest__.py @@ -3,7 +3,7 @@ { "name": "User roles", - "version": "12.0.1.1.0", + "version": "12.0.1.1.1", "category": "Tools", "author": "ABF OSIELL, Odoo Community Association (OCA)", "license": "AGPL-3", diff --git a/base_user_role/models/role.py b/base_user_role/models/role.py index 55aeecbf..9cb6b5de 100644 --- a/base_user_role/models/role.py +++ b/base_user_role/models/role.py @@ -39,7 +39,7 @@ class ResUsersRole(models.Model): string="Associated category", help="Associated group's category", ) - comment = fields.Html(string="Internal Notes",) + comment = fields.Html(string="Internal Notes") @api.multi @api.depends("line_ids.user_id") @@ -90,6 +90,9 @@ class ResUsersRoleLine(models.Model): date_from = fields.Date("From") date_to = fields.Date("To") is_enabled = fields.Boolean("Enabled", compute="_compute_is_enabled") + company_id = fields.Many2one( + "res.company", "Company", default=lambda self: self.env.user.company_id + ) @api.multi @api.depends("date_from", "date_to") diff --git a/base_user_role/models/user.py b/base_user_role/models/user.py index c4a62f86..f90e4b57 100644 --- a/base_user_role/models/user.py +++ b/base_user_role/models/user.py @@ -54,6 +54,14 @@ class ResUsers(models.Model): self.sudo().set_groups_from_roles() return res + def _get_applicable_roles(self): + return self.role_line_ids.filtered( + lambda rec: rec.is_enabled + and ( + not rec.company_id or rec.company_id == rec.user_id.company_id + ) + ) + @api.multi def set_groups_from_roles(self, force=False): """Set (replace) the groups following the roles defined on users. @@ -75,10 +83,7 @@ class ResUsers(models.Model): if not user.role_line_ids and not force: continue group_ids = [] - role_lines = user.role_line_ids.filtered( - lambda rec: rec.is_enabled - ) - for role_line in role_lines: + for role_line in user._get_applicable_roles(): role = role_line.role_id if role: group_ids += role_groups[role] diff --git a/base_user_role/readme/CONTRIBUTORS.rst b/base_user_role/readme/CONTRIBUTORS.rst index 4510b413..8297f5f3 100644 --- a/base_user_role/readme/CONTRIBUTORS.rst +++ b/base_user_role/readme/CONTRIBUTORS.rst @@ -1,5 +1,7 @@ * Sébastien Alix * Duc, Dao Dong (https://komit-consulting.com) * Jean-Charles Drubay (https://komit-consulting.com) +* Pierrick Brun +* Kevin Khao Do not contact contributors directly about support or help with technical issues. diff --git a/base_user_role/static/description/index.html b/base_user_role/static/description/index.html index f5e7beb5..95b2247b 100644 --- a/base_user_role/static/description/index.html +++ b/base_user_role/static/description/index.html @@ -367,7 +367,7 @@ ul.auto-toc { !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> -

Beta License: AGPL-3 OCA/server-backend Translate me on Weblate Try me on Runbot

+

Beta License: AGPL-3 oca/server-backend

This module was written to extend the standard functionality regarding users and groups management. It helps creating well-defined user roles and associating them to users.

@@ -445,10 +445,10 @@ section). ]

Bug Tracker

-

Bugs are tracked on GitHub Issues. +

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -feedback.

+feedback.

Do not contact contributors directly about support or help with technical issues.

@@ -465,6 +465,8 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
  • Sébastien Alix <sebastien.alix@osiell.com>
  • Duc, Dao Dong <duc.dd@komit-consulting.com> (https://komit-consulting.com)
  • Jean-Charles Drubay <jc@komit-consulting.com> (https://komit-consulting.com)
  • +
  • Pierrick Brun <pierrick.brun@akretion.com>
  • +
  • Kevin Khao <kevin.khao@akretion.com>
  • Do not contact contributors directly about support or help with technical issues.

    @@ -479,15 +481,10 @@ If you spotted it first, help us smashing it by providing a detailed and welcome

    Maintainers

    -

    This module is maintained by the OCA.

    -Odoo Community Association -

    OCA, or the Odoo Community Association, is a nonprofit organization whose -mission is to support the collaborative development of Odoo features and -promote its widespread use.

    -

    Current maintainers:

    +

    Current maintainers:

    ABF OSIELL jcdrubay

    -

    This module is part of the OCA/server-backend project on GitHub.

    -

    You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

    +

    This module is part of the oca/server-backend project on GitHub.

    +

    You are welcome to contribute.

    diff --git a/base_user_role/tests/test_user_role.py b/base_user_role/tests/test_user_role.py index ae4e9389..e25da499 100644 --- a/base_user_role/tests/test_user_role.py +++ b/base_user_role/tests/test_user_role.py @@ -50,6 +50,16 @@ class TestUserRole(TransactionCase): ], } self.role2_id = self.role_model.create(vals) + self.company1 = self.env.ref("base.main_company") + self.company2 = self.env["res.company"].create({"name": "company2"}) + self.user_id.write( + { + "company_ids": [ + (4, self.company1.id, 0), + (4, self.company2.id, 0), + ] + } + ) def test_role_1(self): self.user_id.write( @@ -203,3 +213,67 @@ class TestUserRole(TransactionCase): ) roles = self.role_model.browse([self.role1_id.id, self.role2_id.id]) self.assertEqual(user.role_ids, roles) + + def test_user_role_different_company(self): + self.user_id.write({"company_id": self.company1.id}) + self.user_id.write( + { + "role_line_ids": [ + ( + 0, + 0, + { + "role_id": self.role2_id.id, + "company_id": self.company2.id, + }, + ) + ] + } + ) + # Check that user does not have any groups + self.assertEquals( + self.user_id.groups_id, self.env["res.groups"].browse() + ) + + def test_user_role_same_company(self): + self.user_id.write({"company_id": self.company1.id}) + self.user_id.write( + { + "role_line_ids": [ + ( + 0, + 0, + { + "role_id": self.role1_id.id, + "company_id": self.company1.id, + }, + ) + ] + } + ) + user_group_ids = sorted( + set([group.id for group in self.user_id.groups_id]) + ) + role_group_ids = self.role1_id.trans_implied_ids.ids + role_group_ids.append(self.role1_id.group_id.id) + role_group_ids = sorted(set(role_group_ids)) + # Check that user have groups implied by role 1 + self.assertEqual(user_group_ids, role_group_ids) + + def test_user_role_no_company(self): + self.user_id.write({"company_id": self.company1.id}) + self.user_id.write( + { + "role_line_ids": [ + (0, 0, {"role_id": self.role2_id.id, "company_id": False}) + ] + } + ) + user_group_ids = sorted( + set([group.id for group in self.user_id.groups_id]) + ) + role_group_ids = self.role2_id.trans_implied_ids.ids + role_group_ids.append(self.role2_id.group_id.id) + role_group_ids = sorted(set(role_group_ids)) + # Check that user have groups implied by role 2 + self.assertEqual(user_group_ids, role_group_ids) diff --git a/base_user_role/views/role.xml b/base_user_role/views/role.xml index edcee9d8..35a29465 100644 --- a/base_user_role/views/role.xml +++ b/base_user_role/views/role.xml @@ -24,6 +24,7 @@ + diff --git a/base_user_role/views/user.xml b/base_user_role/views/user.xml index 09ba5e66..ae5a179c 100644 --- a/base_user_role/views/user.xml +++ b/base_user_role/views/user.xml @@ -16,6 +16,7 @@ +