[IMP] base_user_role: Added multicompany support from commit server-tool repo commit 840c2c1119460424230c935da24ec42f95c21122

This commit is contained in:
Kevin Khao
2020-03-04 11:45:57 +01:00
parent d64b4123a9
commit 588e8e0daa
9 changed files with 111 additions and 42 deletions

View File

@@ -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 <https://github.com/OCA/server-backend/issues>`_.
Bugs are tracked on `GitHub Issues <https://github.com/oca/server-backend/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 <https://github.com/OCA/server-backend/issues/new?body=module:%20base_user_role%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/oca/server-backend/issues/new?body=module:%20base_user_role%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Do not contact contributors directly about support or help with technical issues.
@@ -119,6 +113,8 @@ Contributors
* 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.
@@ -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 <https://odoo-community.org/page/maintainer-role>`__:
Current maintainers:
|maintainer-ABF OSIELL| |maintainer-jcdrubay|
This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/12.0/base_user_role>`_ project on GitHub.
This module is part of the `oca/server-backend <https://github.com/oca/server-backend/tree/12.0/base_user_role>`_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
You are welcome to contribute.

View File

@@ -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",

View File

@@ -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")

View File

@@ -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]

View File

@@ -1,5 +1,7 @@
* 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.

View File

@@ -367,7 +367,7 @@ ul.auto-toc {
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-backend/tree/12.0/base_user_role"><img alt="OCA/server-backend" src="https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-backend-12-0/server-backend-12-0-base_user_role"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/253/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/oca/server-backend/tree/12.0/base_user_role"><img alt="oca/server-backend" src="https://img.shields.io/badge/github-oca%2Fserver--backend-lightgray.png?logo=github" /></a></p>
<p>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.</p>
@@ -445,10 +445,10 @@ section). ]</dd>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#id3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-backend/issues">GitHub Issues</a>.
<p>Bugs are tracked on <a class="reference external" href="https://github.com/oca/server-backend/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/server-backend/issues/new?body=module:%20base_user_role%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/oca/server-backend/issues/new?body=module:%20base_user_role%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
@@ -465,6 +465,8 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
<li>Sébastien Alix &lt;<a class="reference external" href="mailto:sebastien.alix&#64;osiell.com">sebastien.alix&#64;osiell.com</a>&gt;</li>
<li>Duc, Dao Dong &lt;<a class="reference external" href="mailto:duc.dd&#64;komit-consulting.com">duc.dd&#64;komit-consulting.com</a>&gt; (<a class="reference external" href="https://komit-consulting.com">https://komit-consulting.com</a>)</li>
<li>Jean-Charles Drubay &lt;<a class="reference external" href="mailto:jc&#64;komit-consulting.com">jc&#64;komit-consulting.com</a>&gt; (<a class="reference external" href="https://komit-consulting.com">https://komit-consulting.com</a>)</li>
<li>Pierrick Brun &lt;<a class="reference external" href="mailto:pierrick.brun&#64;akretion.com">pierrick.brun&#64;akretion.com</a>&gt;</li>
<li>Kevin Khao &lt;<a class="reference external" href="mailto:kevin.khao&#64;akretion.com">kevin.khao&#64;akretion.com</a>&gt;</li>
</ul>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
@@ -479,15 +481,10 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#id9">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>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.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainers</a>:</p>
<p>Current maintainers:</p>
<p><a class="reference external" href="https://github.com/ABFOSIELL"><img alt="ABF OSIELL" src="https://github.com/ABFOSIELL.png?size=40px" /></a> <a class="reference external" href="https://github.com/jcdrubay"><img alt="jcdrubay" src="https://github.com/jcdrubay.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-backend/tree/12.0/base_user_role">OCA/server-backend</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/oca/server-backend/tree/12.0/base_user_role">oca/server-backend</a> project on GitHub.</p>
<p>You are welcome to contribute.</p>
</div>
</div>
</div>

View File

@@ -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)

View File

@@ -24,6 +24,7 @@
<field name="date_from"/>
<field name="date_to"/>
<field name="is_enabled"/>
<field name="company_id" groups="base.group_multi_company"/>
</tree>
</field>
</page>

View File

@@ -16,6 +16,7 @@
<field name="date_from"/>
<field name="date_to"/>
<field name="is_enabled"/>
<field name="company_id" groups="base.group_multi_company"/>
</tree>
</field>
</page>