Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIG] base_user_role_company: Migration to 15.0 #139

Closed
wants to merge 7 commits into from
Closed
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
115 changes: 115 additions & 0 deletions base_user_role_company/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
=====================
User roles by company
=====================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |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%2Fserver--backend-lightgray.png?logo=github
:target: https://github.com/OCA/server-backend/tree/15.0/base_user_role_company
: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-15-0/server-backend-14-0-base_user_role_company
: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/15.0
:alt: Try me on Runbot

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

Enable User Roles depending on the Companies selected.

A company specific Role will only be enabled
if it is set for **all** the currently selected companies.

For example, if a user is "Sales Manager" only for Company A,
it will see that role enabled only if Company A is selected.
If the user selects Company A and Company B,
then the "Sales Manager" role won't be enabled.

**Table of contents**

.. contents::
:local:

Configuration
=============

Roles are set on the User form.

The "Company" additional column allows to set a Role as only valid for specific companies.

There is also a "Active Role" techincal field, only visible in developer mode.
It shows what roles are active, after applying the company selection rules.

Usage
=====

Select the active companies from the web client widget, near the top right corner.
When doing so, the User's security Groups are recomputed, based on the Roles.

When the user changes the company selection, only the groups available to all active companies will be activated.

For example:

* A "SALES PERSON" and a "SALES MANAGER" roles are created.

* A user is assigned to the roles:
* "SALES PERSON", with no specific company assigned (meaning all)
* "SALES MANAGER" only to "My Company (Chicago)"

* When selecting active companies from the UI widget:
* If only "My Company (San Francisco)" is active, "SALES PERSON" will be active.
* If only "My Company (Chicago)" is active, "SALES PERSON" and "SALES MANAGER" will be active.
* If both "My Company (San Francisco)" and "My Company (Chicago)" is active, "SALES PERSON" will be active.

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

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_company%0Aversion:%2014.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
~~~~~~~

* Open Source Integrators

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

`Open Source Integrators <http://opensourceintegrators.com>`_

* Daniel Reis <dreis@opensourceintegrators.com>
* Chandresh Thakkar <cthakkr@opensourceintegrators.com>

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.

This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/15.0/base_user_role_company>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions base_user_role_company/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
19 changes: 19 additions & 0 deletions base_user_role_company/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "User roles by company",
"version": "15.0.0.0.0",
"category": "Tools",
"author": "Open Source Integrators, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/server-backend",
"depends": ["base_user_role"],
"data": [
"views/role.xml",
],
"installable": True,
"auto_install": True,
"maintainer": "dreispt",
"development_status": "Beta",
}
78 changes: 78 additions & 0 deletions base_user_role_company/i18n/base_user_role_company.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_user_role_company
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 14.0\n"
"Report-Msgid-Bugs-To: \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: base_user_role_company
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users_role_line__active_role
msgid "Active Role"
msgstr ""

#. module: base_user_role_company
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users_role_line__company_id
msgid "Company"
msgstr ""

#. module: base_user_role_company
#: model:ir.model.fields,field_description:base_user_role_company.field_ir_http__display_name
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users__display_name
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users_role_line__display_name
msgid "Display Name"
msgstr ""

#. module: base_user_role_company
#: model:ir.model,name:base_user_role_company.model_ir_http
msgid "HTTP Routing"
msgstr ""

#. module: base_user_role_company
#: model:ir.model.fields,field_description:base_user_role_company.field_ir_http__id
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users__id
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users_role_line__id
msgid "ID"
msgstr ""

#. module: base_user_role_company
#: model:ir.model.fields,help:base_user_role_company.field_res_users_role_line__company_id
msgid ""
"If set, this role only applies when this is the main company selected. "
"Otherwise it applies to all companies."
msgstr ""

#. module: base_user_role_company
#: model:ir.model.fields,field_description:base_user_role_company.field_ir_http____last_update
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users____last_update
#: model:ir.model.fields,field_description:base_user_role_company.field_res_users_role_line____last_update
msgid "Last Modified on"
msgstr ""

#. module: base_user_role_company
#: model:ir.model.constraint,message:base_user_role_company.constraint_res_users_role_line_user_role_uniq
msgid "Roles can be assigned to a user only once at a time"
msgstr ""

#. module: base_user_role_company
#: code:addons/base_user_role_company/models/role.py:0
#, python-format
msgid "User \"{}\" does not have access to the company \"{}\""
msgstr ""

#. module: base_user_role_company
#: model:ir.model,name:base_user_role_company.model_res_users
msgid "Users"
msgstr ""

#. module: base_user_role_company
#: model:ir.model,name:base_user_role_company.model_res_users_role_line
msgid "Users associated to a role"
msgstr ""
5 changes: 5 additions & 0 deletions base_user_role_company/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import role
from . import ir_http
23 changes: 23 additions & 0 deletions base_user_role_company/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models
from odoo.http import request


class IrHttp(models.AbstractModel):
_inherit = "ir.http"

def session_info(self):
"""
Based on the selected companies (cids),
calculate the roles to enable.
A role should be enabled only when it applies to all selected companies.
"""
result = super(IrHttp, self).session_info()
if self.env.user.role_line_ids:
cids_str = request.httprequest.cookies.get("cids", str(self.env.company.id))
cids = [int(cid) for cid in cids_str.split(",")]
self.env.user._set_session_active_roles(cids)
self.env.user.set_groups_from_roles()
return result
65 changes: 65 additions & 0 deletions base_user_role_company/models/role.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (C) 2021 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ResUsersRoleLine(models.Model):
_inherit = "res.users.role.line"

company_id = fields.Many2one(
"res.company",
"Company",
help="If set, this role only applies when this is the main company selected."
" Otherwise it applies to all companies.",
)
active_role = fields.Boolean(default=True)

@api.constrains("user_id", "company_id")
def _check_company(self):
for record in self:
if (
record.company_id
and record.company_id != record.user_id.company_id
and record.company_id not in record.user_id.company_ids
):
raise ValidationError(
_('User "{}" does not have access to the company "{}"').format(
record.user_id.name, record.company_id.name
)
)

_sql_constraints = [
(
"user_role_uniq",
"unique (user_id,role_id,company_id)",
"Roles can be assigned to a user only once at a time",
)
]


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

def _get_enabled_roles(self):
res = super()._get_enabled_roles()
return res.filtered("active_role")

@api.model
def _set_session_active_roles(self, cids):
"""
Based on the selected companies (cids),
calculate the roles to enable.
A role should be enabled only when it applies to all selected companies.
"""
for role_line in self.env.user.role_line_ids:
if not role_line.company_id:
role_line.active_role = True
elif role_line.company_id.id in cids:
is_on_companies = self.env.user.role_line_ids.filtered(
lambda x: x.role_id == role_line.role_id and x.company_id.id in cids
)
role_line.active_role = len(is_on_companies) == len(cids)
else:
role_line.active_role = False
6 changes: 6 additions & 0 deletions base_user_role_company/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Roles are set on the User form.

The "Company" additional column allows to set a Role as only valid for specific companies.

There is also a "Active Role" techincal field, only visible in developer mode.
It shows what roles are active, after applying the company selection rules.
4 changes: 4 additions & 0 deletions base_user_role_company/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
`Open Source Integrators <http://opensourceintegrators.com>`_

* Daniel Reis <dreis@opensourceintegrators.com>
* Chandresh Thakkar <cthakkr@opensourceintegrators.com>
9 changes: 9 additions & 0 deletions base_user_role_company/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Enable User Roles depending on the Companies selected.

A company specific Role will only be enabled
if it is set for **all** the currently selected companies.

For example, if a user is "Sales Manager" only for Company A,
it will see that role enabled only if Company A is selected.
If the user selects Company A and Company B,
then the "Sales Manager" role won't be enabled.
17 changes: 17 additions & 0 deletions base_user_role_company/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Select the active companies from the web client widget, near the top right corner.
When doing so, the User's security Groups are recomputed, based on the Roles.

When the user changes the company selection, only the groups available to all active companies will be activated.

For example:

* A "SALES PERSON" and a "SALES MANAGER" roles are created.

* A user is assigned to the roles:
* "SALES PERSON", with no specific company assigned (meaning all)
* "SALES MANAGER" only to "My Company (Chicago)"

* When selecting active companies from the UI widget:
* If only "My Company (San Francisco)" is active, "SALES PERSON" will be active.
* If only "My Company (Chicago)" is active, "SALES PERSON" and "SALES MANAGER" will be active.
* If both "My Company (San Francisco)" and "My Company (Chicago)" is active, "SALES PERSON" will be active.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading