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.

89 lines
3.2 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(
  45. lambda rec: rec.is_enabled
  46. and (not rec.company_id or rec.company_id == rec.user_id.company_id)
  47. )
  48. def set_groups_from_roles(self, force=False):
  49. """Set (replace) the groups following the roles defined on users.
  50. If no role is defined on the user, its groups are let untouched unless
  51. the `force` parameter is `True`.
  52. """
  53. role_groups = {}
  54. # We obtain all the groups associated to each role first, so that
  55. # it is faster to compare later with each user's groups.
  56. for role in self.mapped("role_line_ids.role_id"):
  57. role_groups[role] = list(
  58. set(
  59. role.group_id.ids
  60. + role.implied_ids.ids
  61. + role.trans_implied_ids.ids
  62. )
  63. )
  64. for user in self:
  65. if not user.role_line_ids and not force:
  66. continue
  67. group_ids = []
  68. for role_line in user._get_enabled_roles():
  69. role = role_line.role_id
  70. group_ids += role_groups[role]
  71. group_ids = list(set(group_ids)) # Remove duplicates IDs
  72. groups_to_add = list(set(group_ids) - set(user.groups_id.ids))
  73. groups_to_remove = list(set(user.groups_id.ids) - set(group_ids))
  74. to_add = [(4, gr) for gr in groups_to_add]
  75. to_remove = [(3, gr) for gr in groups_to_remove]
  76. groups = to_remove + to_add
  77. if groups:
  78. vals = {"groups_id": groups}
  79. super(ResUsers, user).write(vals)
  80. return True