You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.3 KiB

  1. # Copyright (C) 2021 Open Source Integrators
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, models
  4. class ResUsers(models.Model):
  5. _inherit = "res.users"
  6. @classmethod
  7. def authenticate(cls, db, login, password, user_agent_env):
  8. uid = super().authenticate(db, login, password, user_agent_env)
  9. # On login, ensure the proper roles are applied
  10. # The last Role applied may not be the correct one,
  11. # sonce the new session current company can be different
  12. with cls.pool.cursor() as cr:
  13. env = api.Environment(cr, uid, {})
  14. if env.user.role_line_ids:
  15. env.user.set_groups_from_roles()
  16. return uid
  17. def _get_enabled_roles(self):
  18. res = super()._get_enabled_roles()
  19. # Enable only the Roles corresponing to the currently selected company
  20. if self.role_line_ids:
  21. res = res.filtered(
  22. lambda x: not x.company_id or x.company_id == self.env.company
  23. )
  24. return res
  25. def set_groups_from_roles(self, force=False, company_id=False):
  26. # When using the Company Switcher widget, the self.env.company is not yet set
  27. if company_id:
  28. self = self.with_company(company_id)
  29. return super().set_groups_from_roles(force=force)