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.

55 lines
1.8 KiB

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