mirror of
https://github.com/OCA/server-backend.git
synced 2025-02-18 09:52:42 +02:00
[FIX] base_user_role_company: roles not properly applied on login
This commit is contained in:
committed by
Robin Conjour
parent
b7c3aa688d
commit
31b92c7de9
36
base_user_role_company/models/user.py
Normal file
36
base_user_role_company/models/user.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# Copyright (C) 2021 Open Source Integrators
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, models
|
||||
|
||||
|
||||
class ResUsers(models.Model):
|
||||
_inherit = "res.users"
|
||||
|
||||
@classmethod
|
||||
def authenticate(cls, db, login, password, user_agent_env):
|
||||
uid = super().authenticate(db, login, password, user_agent_env)
|
||||
# On login, ensure the proper roles are applied
|
||||
# The last Role applied may not be the correct one,
|
||||
# sonce the new session current company can be different
|
||||
with cls.pool.cursor() as cr:
|
||||
env = api.Environment(cr, uid, {})
|
||||
if env.user.role_line_ids:
|
||||
env.user.set_groups_from_roles()
|
||||
return uid
|
||||
|
||||
def _get_enabled_roles(self):
|
||||
res = super()._get_enabled_roles()
|
||||
# Enable only the Roles corresponing to the currently selected company
|
||||
if self.env.user.role_line_ids:
|
||||
curr_company = self.env.company
|
||||
res = res.filtered(
|
||||
lambda x: not x.company_id or x.company_id == curr_company
|
||||
)
|
||||
return res
|
||||
|
||||
def set_groups_from_roles(self, force=False, company_id=False):
|
||||
# When using the Company Switcher widget, the self.env.company is not yet set
|
||||
if company_id:
|
||||
self = self.with_company(company_id)
|
||||
return super().set_groups_from_roles(force=force)
|
||||
Reference in New Issue
Block a user