Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .copier-answers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ org_name: Odoo Community Association (OCA)
org_slug: OCA
rebel_module_groups:
- product_category_company
- partner_multi_company
repo_description: 'TODO: add repo description.'
repo_name: Tools for managing instances containing multiple companies
repo_slug: multi-company
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ jobs:
name: test with Odoo
makepot: "true"
- container: ghcr.io/oca/oca-ci/py3.10-odoo16.0:latest
exclude: "product_category_company"
include: "partner_multi_company"
name: test with Odoo
makepot: "true"
- container: ghcr.io/oca/oca-ci/py3.10-odoo16.0:latest
exclude: "product_category_company,partner_multi_company"
name: test with Odoo
makepot: "true"
services:
Expand Down
95 changes: 95 additions & 0 deletions res_company_access_all_children/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
==================================
Companies - Access to All Children
==================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:0da71a75ce2c6065dc8e739e585f7bee426e1a22f7c2513b56c605204ecc76b3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |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%2Fmulti--company-lightgray.png?logo=github
:target: https://github.com/OCA/multi-company/tree/16.0/res_company_access_all_children
:alt: OCA/multi-company
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/multi-company-16-0/multi-company-16-0-res_company_access_all_children
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/multi-company&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module is usefull in a multi-company context with hierarchy.
(with parent and child companies)

Once installed, if a user has access to a parent company,
he will have access to all the child companies.

If a new child company is created, all the users that have
access to the parent company will have access to the new child
company.

This module avoids the need for parameterization,
which can be fastidious in a context with many companies and users.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/multi-company/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/multi-company/issues/new?body=module:%20res_company_access_all_children%0Aversion:%2016.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.

Credits
=======

Authors
~~~~~~~

* GRAP

Contributors
~~~~~~~~~~~~

* Sylvain LE GAL <https://twitter.com/legalsylvain>

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-legalsylvain| image:: https://github.com/legalsylvain.png?size=40px
:target: https://github.com/legalsylvain
:alt: legalsylvain

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-legalsylvain|

This module is part of the `OCA/multi-company <https://github.com/OCA/multi-company/tree/16.0/res_company_access_all_children>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions res_company_access_all_children/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from .hooks import post_init_hook
18 changes: 18 additions & 0 deletions res_company_access_all_children/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (C) 2024 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Companies - Access to All Children",
"summary": "Give access to all children companies"
" to users that have access to a parent company",
"version": "16.0.1.0.0",
"category": "Tools",
"author": "GRAP, Odoo Community Association (OCA)",
"maintainers": ["legalsylvain"],
"website": "https://github.com/OCA/multi-company",
"license": "AGPL-3",
"depends": ["base"],
"installable": True,
"post_init_hook": "post_init_hook",
}
19 changes: 19 additions & 0 deletions res_company_access_all_children/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) 2024 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)


def post_init_hook(cr, registry):
_logger.info(
"Give access to all the child companies for users"
" that have access to parent companies."
)
env = api.Environment(cr, SUPERUSER_ID, {})
users = env["res.users"].search([])
users._propagate_access_to_child_companies()
26 changes: 26 additions & 0 deletions res_company_access_all_children/i18n/fr.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * res_company_access_all_children
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-03 23:09+0000\n"
"PO-Revision-Date: 2024-08-03 23:09+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: res_company_access_all_children
#: model:ir.model,name:res_company_access_all_children.model_res_company
msgid "Companies"
msgstr "Sociétés"

#. module: res_company_access_all_children
#: model:ir.model,name:res_company_access_all_children.model_res_users
msgid "User"
msgstr "Utilisateur"
2 changes: 2 additions & 0 deletions res_company_access_all_children/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import res_company
from . import res_users
21 changes: 21 additions & 0 deletions res_company_access_all_children/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2024-Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models


class ResCompany(models.Model):
_inherit = "res.company"

@api.model_create_multi
def create(self, vals_list):
companies = super().create(vals_list)
companies.mapped("parent_id").user_ids._propagate_access_to_child_companies()
return companies

def write(self, vals):
res = super().write(vals)
if vals.get("parent_id", False):
self.mapped("parent_id").user_ids._propagate_access_to_child_companies()
return res
42 changes: 42 additions & 0 deletions res_company_access_all_children/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (C) 2024-Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import Command, api, models


class ResUsers(models.Model):
_inherit = "res.users"

@api.model_create_multi
def create(self, vals_list):
users = super().create(vals_list)
users._propagate_access_to_child_companies()
return users

def write(self, vals):
res = super().write(vals)
if vals.get("company_ids", False):
self._propagate_access_to_child_companies()
return res

def _propagate_access_to_child_companies(self):
"""If a user has access to a parent company, so he'll have
access to all the child companies"""

def _get_recursive_child_companies(self, companies):
result = companies.child_ids
if companies.child_ids.child_ids:
result |= _get_recursive_child_companies(self, companies.child_ids)
return result

for user in self:
existing_companies = user.company_ids
all_companies = _get_recursive_child_companies(self, existing_companies)

new_company_ids = list(set(all_companies.ids) - set(existing_companies.ids))
if new_company_ids:
super(ResUsers, user).write(
{"company_ids": [Command.link(x) for x in new_company_ids]}
)
return
1 change: 1 addition & 0 deletions res_company_access_all_children/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Sylvain LE GAL <https://twitter.com/legalsylvain>
12 changes: 12 additions & 0 deletions res_company_access_all_children/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This module is usefull in a multi-company context with hierarchy.
(with parent and child companies)

Once installed, if a user has access to a parent company,
he will have access to all the child companies.

If a new child company is created, all the users that have
access to the parent company will have access to the new child
company.

This module avoids the need for parameterization,
which can be fastidious in a context with many companies and users.
Loading