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.

40 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, fields, models
  4. from odoo.exceptions import ValidationError
  5. class ResUsersRoleLine(models.Model):
  6. _inherit = "res.users.role.line"
  7. allowed_company_ids = fields.Many2many(related="user_id.company_ids")
  8. company_id = fields.Many2one(
  9. "res.company",
  10. "Company",
  11. domain="[('id', 'in', allowed_company_ids)]",
  12. help="If set, this role only applies when this is the main company selected."
  13. " Otherwise it applies to all companies.",
  14. )
  15. @api.constrains("user_id", "company_id")
  16. def _check_company(self):
  17. for record in self:
  18. if (
  19. record.company_id
  20. and record.company_id != record.user_id.company_id
  21. and record.company_id not in record.user_id.company_ids
  22. ):
  23. raise ValidationError(
  24. _('User "{}" does not have access to the company "{}"').format(
  25. record.user_id.name, record.company_id.name
  26. )
  27. )
  28. _sql_constraints = [
  29. (
  30. "user_role_uniq",
  31. "unique (user_id,role_id,company_id)",
  32. "Roles can be assigned to a user only once at a time",
  33. )
  34. ]