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.

86 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", compute="_compute_role_ids"
  14. )
  15. @api.model
  16. def _default_role_lines(self):
  17. default_user = self.env.ref("base.default_user", raise_if_not_found=False)
  18. default_values = []
  19. if default_user:
  20. for role_line in default_user.role_line_ids:
  21. default_values.append(
  22. {
  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. )
  29. return default_values
  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. def write(self, vals):
  40. res = super(ResUsers, self).write(vals)
  41. self.sudo().set_groups_from_roles()
  42. return res
  43. def _get_enabled_roles(self):
  44. return self.role_line_ids.filtered(lambda rec: rec.is_enabled)
  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(
  55. set(
  56. role.group_id.ids
  57. + role.implied_ids.ids
  58. + role.trans_implied_ids.ids
  59. )
  60. )
  61. for user in self:
  62. if not user.role_line_ids and not force:
  63. continue
  64. group_ids = []
  65. for role_line in user._get_enabled_roles():
  66. role = role_line.role_id
  67. group_ids += role_groups[role]
  68. group_ids = list(set(group_ids)) # Remove duplicates IDs
  69. groups_to_add = list(set(group_ids) - set(user.groups_id.ids))
  70. groups_to_remove = list(set(user.groups_id.ids) - set(group_ids))
  71. to_add = [(4, gr) for gr in groups_to_add]
  72. to_remove = [(3, gr) for gr in groups_to_remove]
  73. groups = to_remove + to_add
  74. if groups:
  75. vals = {"groups_id": groups}
  76. super(ResUsers, user).write(vals)
  77. return True