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.

56 lines
1.9 KiB

  1. # Copyright 2014 ABF OSIELL <http://osiell.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class ResUsers(models.Model):
  5. _inherit = 'res.users'
  6. role_line_ids = fields.One2many(
  7. comodel_name='res.users.role.line',
  8. inverse_name='user_id', string="Role lines")
  9. role_ids = fields.One2many(
  10. comodel_name='res.users.role', string="Roles",
  11. compute='_compute_role_ids')
  12. @api.multi
  13. @api.depends('role_line_ids.role_id')
  14. def _compute_role_ids(self):
  15. for user in self:
  16. user.role_ids = user.role_line_ids.mapped('role_id')
  17. @api.model
  18. def create(self, vals):
  19. new_record = super(ResUsers, self).create(vals)
  20. new_record.set_groups_from_roles()
  21. return new_record
  22. @api.multi
  23. def write(self, vals):
  24. res = super(ResUsers, self).write(vals)
  25. self.sudo().set_groups_from_roles()
  26. return res
  27. @api.multi
  28. def set_groups_from_roles(self, force=False):
  29. """Set (replace) the groups following the roles defined on users.
  30. If no role is defined on the user, its groups are let untouched unless
  31. the `force` parameter is `True`.
  32. """
  33. for user in self:
  34. if not user.role_line_ids and not force:
  35. continue
  36. group_ids = []
  37. role_lines = user.role_line_ids.filtered(
  38. lambda rec: rec.is_enabled)
  39. for role_line in role_lines:
  40. role = role_line.role_id
  41. if role:
  42. group_ids.append(role.group_id.id)
  43. group_ids.extend(role.implied_ids.ids)
  44. group_ids = list(set(group_ids)) # Remove duplicates IDs
  45. vals = {
  46. 'groups_id': [(6, 0, group_ids)],
  47. }
  48. super(ResUsers, user).write(vals)
  49. return True