Skip to content

Commit 57b3201

Browse files
committed
[ADD] res_company_access_all_childs: new module to give access to user to all child companies,
when a user has access to parent company
1 parent 3d1b6bd commit 57b3201

15 files changed

Lines changed: 773 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
================================
2+
Companies - Access to All Childs
3+
================================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:0da71a75ce2c6065dc8e739e585f7bee426e1a22f7c2513b56c605204ecc76b3
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fmulti--company-lightgray.png?logo=github
20+
:target: https://github.com/OCA/multi-company/tree/16.0/res_company_access_all_childs
21+
:alt: OCA/multi-company
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/multi-company-16-0/multi-company-16-0-res_company_access_all_childs
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/multi-company&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module is usefull in a multi-company context with hierarchy.
32+
(parent and child companies)
33+
34+
Once installed, if a user has access to a parent company,
35+
he will have access to all the child companies.
36+
37+
If a new child company is created, all the users that have
38+
access to the parent company will have access to the new child
39+
company.
40+
41+
This module avoids the need for parameterization,
42+
which can be fastidious in a context with many companies and users.
43+
44+
**Table of contents**
45+
46+
.. contents::
47+
:local:
48+
49+
Bug Tracker
50+
===========
51+
52+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/multi-company/issues>`_.
53+
In case of trouble, please check there if your issue has already been reported.
54+
If you spotted it first, help us to smash it by providing a detailed and welcomed
55+
`feedback <https://github.com/OCA/multi-company/issues/new?body=module:%20res_company_access_all_childs%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
56+
57+
Do not contact contributors directly about support or help with technical issues.
58+
59+
Credits
60+
=======
61+
62+
Authors
63+
~~~~~~~
64+
65+
* GRAP
66+
67+
Contributors
68+
~~~~~~~~~~~~
69+
70+
* Sylvain LE GAL <https://twitter.com/legalsylvain>
71+
72+
Maintainers
73+
~~~~~~~~~~~
74+
75+
This module is maintained by the OCA.
76+
77+
.. image:: https://odoo-community.org/logo.png
78+
:alt: Odoo Community Association
79+
:target: https://odoo-community.org
80+
81+
OCA, or the Odoo Community Association, is a nonprofit organization whose
82+
mission is to support the collaborative development of Odoo features and
83+
promote its widespread use.
84+
85+
.. |maintainer-legalsylvain| image:: https://github.com/legalsylvain.png?size=40px
86+
:target: https://github.com/legalsylvain
87+
:alt: legalsylvain
88+
89+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
90+
91+
|maintainer-legalsylvain|
92+
93+
This module is part of the `OCA/multi-company <https://github.com/OCA/multi-company/tree/16.0/res_company_access_all_childs>`_ project on GitHub.
94+
95+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import models
2+
from .hooks import post_init_hook
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (C) 2024 - Today: GRAP (http://www.grap.coop)
2+
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4+
5+
{
6+
"name": "Companies - Access to All Childs",
7+
"summary": "Give access to all child companies"
8+
" to users that have access to a parent company",
9+
"version": "16.0.1.0.0",
10+
"category": "Tools",
11+
"author": "GRAP, Odoo Community Association (OCA)",
12+
"maintainers": ["legalsylvain"],
13+
"website": "https://github.com/OCA/multi-company",
14+
"license": "AGPL-3",
15+
"depends": ["base"],
16+
"installable": True,
17+
"post_init_hook": "post_init_hook",
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (C) 2024 - Today: GRAP (http://www.grap.coop)
2+
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4+
5+
import logging
6+
7+
from odoo import SUPERUSER_ID, api
8+
9+
_logger = logging.getLogger(__name__)
10+
11+
12+
def post_init_hook(cr, registry):
13+
_logger.info(
14+
"Give access to all the child companies for users"
15+
" that have access to parent companies."
16+
)
17+
env = api.Environment(cr, SUPERUSER_ID, {})
18+
users = env["res.users"].search([])
19+
users._propagate_access_to_child_companies()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Translation of Odoo Server.
2+
# This file contains the translation of the following modules:
3+
# * res_company_access_all_childs
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: Odoo Server 16.0\n"
8+
"Report-Msgid-Bugs-To: \n"
9+
"POT-Creation-Date: 2024-08-03 23:09+0000\n"
10+
"PO-Revision-Date: 2024-08-03 23:09+0000\n"
11+
"Last-Translator: \n"
12+
"Language-Team: \n"
13+
"MIME-Version: 1.0\n"
14+
"Content-Type: text/plain; charset=UTF-8\n"
15+
"Content-Transfer-Encoding: \n"
16+
"Plural-Forms: \n"
17+
18+
#. module: res_company_access_all_childs
19+
#: model:ir.model,name:res_company_access_all_childs.model_res_company
20+
msgid "Companies"
21+
msgstr "Sociétés"
22+
23+
#. module: res_company_access_all_childs
24+
#: model:ir.model,name:res_company_access_all_childs.model_res_users
25+
msgid "User"
26+
msgstr "Utilisateur"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import res_company
2+
from . import res_users
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (C) 2024-Today: GRAP (http://www.grap.coop)
2+
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4+
5+
from odoo import api, models
6+
7+
8+
class ResCompany(models.Model):
9+
_inherit = "res.company"
10+
11+
@api.model_create_multi
12+
def create(self, vals_list):
13+
companies = super().create(vals_list)
14+
companies.mapped("parent_id").user_ids._propagate_access_to_child_companies()
15+
return companies
16+
17+
def write(self, vals):
18+
res = super().write(vals)
19+
if vals.get("parent_id", False):
20+
self.mapped("parent_id").user_ids._propagate_access_to_child_companies()
21+
return res
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (C) 2024-Today: GRAP (http://www.grap.coop)
2+
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
4+
5+
from odoo import Command, api, models
6+
7+
8+
class ResUsers(models.Model):
9+
_inherit = "res.users"
10+
11+
@api.model_create_multi
12+
def create(self, vals_list):
13+
users = super().create(vals_list)
14+
users._propagate_access_to_child_companies()
15+
return users
16+
17+
def write(self, vals):
18+
res = super().write(vals)
19+
if vals.get("company_ids", False):
20+
self._propagate_access_to_child_companies()
21+
return res
22+
23+
def _propagate_access_to_child_companies(self):
24+
"""If a user has access to a parent company, so he'll have
25+
access to all the child companies"""
26+
27+
def _get_recursive_child_companies(self, companies):
28+
result = companies.child_ids
29+
if companies.child_ids.child_ids:
30+
result |= _get_recursive_child_companies(self, companies.child_ids)
31+
return result
32+
33+
for user in self:
34+
existing_companies = user.company_ids
35+
all_companies = _get_recursive_child_companies(self, existing_companies)
36+
37+
new_company_ids = list(set(all_companies.ids) - set(existing_companies.ids))
38+
if new_company_ids:
39+
super(ResUsers, user).write(
40+
{"company_ids": [Command.link(x) for x in new_company_ids]}
41+
)
42+
return
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Sylvain LE GAL <https://twitter.com/legalsylvain>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This module is usefull in a multi-company context with hierarchy.
2+
(parent and child companies)
3+
4+
Once installed, if a user has access to a parent company,
5+
he will have access to all the child companies.
6+
7+
If a new child company is created, all the users that have
8+
access to the parent company will have access to the new child
9+
company.
10+
11+
This module avoids the need for parameterization,
12+
which can be fastidious in a context with many companies and users.

0 commit comments

Comments
 (0)