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.

85 lines
3.1 KiB

  1. # Copyright 2014 ABF OSIELL <http://osiell.com>
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  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',
  9. string="Role lines",
  10. default=lambda self: self._default_role_lines()
  11. )
  12. role_ids = fields.One2many(
  13. comodel_name='res.users.role', string="Roles",
  14. compute='_compute_role_ids')
  15. @api.model
  16. def _default_role_lines(self):
  17. default_user = self.env.ref(
  18. 'base.default_user', raise_if_not_found=False)
  19. default_values = []
  20. if default_user:
  21. for role_line in default_user.role_line_ids:
  22. default_values.append({
  23. 'role_id': role_line.role_id.id,
  24. 'date_from': role_line.date_from,
  25. 'date_to': role_line.date_to,
  26. 'is_enabled': role_line.is_enabled,
  27. })
  28. return default_values
  29. @api.multi
  30. @api.depends('role_line_ids.role_id')
  31. def _compute_role_ids(self):
  32. for user in self:
  33. user.role_ids = user.role_line_ids.mapped('role_id')
  34. @api.model
  35. def create(self, vals):
  36. new_record = super(ResUsers, self).create(vals)
  37. new_record.set_groups_from_roles()
  38. return new_record
  39. @api.multi
  40. def write(self, vals):
  41. res = super(ResUsers, self).write(vals)
  42. self.sudo().set_groups_from_roles()
  43. return res
  44. @api.multi
  45. def set_groups_from_roles(self, force=False):
  46. """Set (replace) the groups following the roles defined on users.
  47. If no role is defined on the user, its groups are let untouched unless
  48. the `force` parameter is `True`.
  49. """
  50. role_groups = {}
  51. # We obtain all the groups associated to each role first, so that
  52. # it is faster to compare later with each user's groups.
  53. for role in self.mapped('role_line_ids.role_id'):
  54. role_groups[role] = list(set(
  55. role.group_id.ids + role.implied_ids.ids +
  56. role.trans_implied_ids.ids))
  57. for user in self:
  58. if not user.role_line_ids and not force:
  59. continue
  60. group_ids = []
  61. role_lines = user.role_line_ids.filtered(
  62. lambda rec: rec.is_enabled)
  63. for role_line in role_lines:
  64. role = role_line.role_id
  65. group_ids += role_groups[role]
  66. group_ids = list(set(group_ids)) # Remove duplicates IDs
  67. groups_to_add = list(set(group_ids) - set(user.groups_id.ids))
  68. groups_to_remove = list(set(user.groups_id.ids) - set(group_ids))
  69. to_add = [(4, gr) for gr in groups_to_add]
  70. to_remove = [(3, gr) for gr in groups_to_remove]
  71. groups = to_remove + to_add
  72. if groups:
  73. vals = {
  74. 'groups_id': groups,
  75. }
  76. super(ResUsers, user).write(vals)
  77. return True