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.

54 lines
1.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 ABF OSIELL <http://osiell.com>
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  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):
  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.
  30. """
  31. for user in self:
  32. if not user.role_line_ids:
  33. continue
  34. group_ids = []
  35. role_lines = user.role_line_ids.filtered(
  36. lambda rec: rec.is_enabled)
  37. for role_line in role_lines:
  38. role = role_line.role_id
  39. group_ids.append(role.group_id.id)
  40. group_ids.extend(role.implied_ids.ids)
  41. group_ids = list(set(group_ids)) # Remove duplicates IDs
  42. vals = {
  43. 'groups_id': [(6, 0, group_ids)],
  44. }
  45. super(ResUsers, user).write(vals)
  46. return True