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.

125 lines
3.9 KiB

  1. # Copyright 2014 ABF OSIELL <http://osiell.com>
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. import datetime
  4. import logging
  5. from odoo import SUPERUSER_ID, api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. class ResUsersRole(models.Model):
  8. _name = "res.users.role"
  9. _inherits = {"res.groups": "group_id"}
  10. _description = "User role"
  11. group_id = fields.Many2one(
  12. comodel_name="res.groups",
  13. required=True,
  14. ondelete="cascade",
  15. readonly=True,
  16. string="Associated group",
  17. )
  18. line_ids = fields.One2many(
  19. comodel_name="res.users.role.line", inverse_name="role_id", string="Role lines"
  20. )
  21. user_ids = fields.One2many(
  22. comodel_name="res.users", string="Users list", compute="_compute_user_ids"
  23. )
  24. group_category_id = fields.Many2one(
  25. related="group_id.category_id",
  26. default=lambda cls: cls.env.ref("base_user_role.ir_module_category_role").id,
  27. string="Associated category",
  28. help="Associated group's category",
  29. readonly=False,
  30. )
  31. comment = fields.Html(
  32. string="Internal Notes",
  33. )
  34. @api.depends("line_ids.user_id")
  35. def _compute_user_ids(self):
  36. for role in self:
  37. role.user_ids = role.line_ids.mapped("user_id")
  38. @api.model
  39. def create(self, vals):
  40. new_record = super(ResUsersRole, self).create(vals)
  41. new_record.update_users()
  42. return new_record
  43. def write(self, vals):
  44. # Workaround to solve issue with broken code in odoo that clear the cache
  45. # during the write: see odoo/addons/base/models/res_users.py#L226
  46. groups_vals = {}
  47. for field in self.group_id._fields:
  48. if field in vals:
  49. groups_vals[field] = vals.pop(field)
  50. if groups_vals:
  51. self.group_id.write(groups_vals)
  52. res = super(ResUsersRole, self).write(vals)
  53. self.update_users()
  54. return res
  55. def unlink(self):
  56. users = self.mapped("user_ids")
  57. res = super(ResUsersRole, self).unlink()
  58. users.set_groups_from_roles(force=True)
  59. return res
  60. def update_users(self):
  61. """Update all the users concerned by the roles identified by `ids`."""
  62. users = self.mapped("user_ids")
  63. users.set_groups_from_roles()
  64. return True
  65. @api.model
  66. def cron_update_users(self):
  67. logging.info("Update user roles")
  68. self.search([]).update_users()
  69. class ResUsersRoleLine(models.Model):
  70. _name = "res.users.role.line"
  71. _description = "Users associated to a role"
  72. role_id = fields.Many2one(
  73. comodel_name="res.users.role", required=True, string="Role", ondelete="cascade"
  74. )
  75. user_id = fields.Many2one(
  76. comodel_name="res.users",
  77. required=True,
  78. string="User",
  79. domain=[("id", "!=", SUPERUSER_ID)],
  80. ondelete="cascade",
  81. )
  82. date_from = fields.Date("From")
  83. date_to = fields.Date("To")
  84. is_enabled = fields.Boolean("Enabled", compute="_compute_is_enabled")
  85. _sql_constraints = [
  86. (
  87. "user_role_uniq",
  88. "unique (user_id,role_id)",
  89. "Roles can be assigned to a user only once at a time",
  90. )
  91. ]
  92. @api.depends("date_from", "date_to")
  93. def _compute_is_enabled(self):
  94. today = datetime.date.today()
  95. for role_line in self:
  96. role_line.is_enabled = True
  97. if role_line.date_from:
  98. date_from = role_line.date_from
  99. if date_from > today:
  100. role_line.is_enabled = False
  101. if role_line.date_to:
  102. date_to = role_line.date_to
  103. if today > date_to:
  104. role_line.is_enabled = False
  105. def unlink(self):
  106. users = self.mapped("user_id")
  107. res = super(ResUsersRoleLine, self).unlink()
  108. users.set_groups_from_roles(force=True)
  109. return res