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.

122 lines
3.8 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. @api.depends("line_ids.user_id")
  32. def _compute_user_ids(self):
  33. for role in self:
  34. role.user_ids = role.line_ids.mapped("user_id")
  35. @api.model
  36. def create(self, vals):
  37. new_record = super(ResUsersRole, self).create(vals)
  38. new_record.update_users()
  39. return new_record
  40. def write(self, vals):
  41. # Workaround to solve issue with broken code in odoo that clear the cache
  42. # during the write: see odoo/addons/base/models/res_users.py#L226
  43. groups_vals = {}
  44. for field in self.group_id._fields:
  45. if field in vals:
  46. groups_vals[field] = vals.pop(field)
  47. if groups_vals:
  48. self.group_id.write(groups_vals)
  49. res = super(ResUsersRole, self).write(vals)
  50. self.update_users()
  51. return res
  52. def unlink(self):
  53. users = self.mapped("user_ids")
  54. res = super(ResUsersRole, self).unlink()
  55. users.set_groups_from_roles(force=True)
  56. return res
  57. def update_users(self):
  58. """Update all the users concerned by the roles identified by `ids`."""
  59. users = self.mapped("user_ids")
  60. users.set_groups_from_roles()
  61. return True
  62. @api.model
  63. def cron_update_users(self):
  64. logging.info("Update user roles")
  65. self.search([]).update_users()
  66. class ResUsersRoleLine(models.Model):
  67. _name = "res.users.role.line"
  68. _description = "Users associated to a role"
  69. role_id = fields.Many2one(
  70. comodel_name="res.users.role", required=True, string="Role", ondelete="cascade"
  71. )
  72. user_id = fields.Many2one(
  73. comodel_name="res.users",
  74. required=True,
  75. string="User",
  76. domain=[("id", "!=", SUPERUSER_ID)],
  77. ondelete="cascade",
  78. )
  79. date_from = fields.Date("From")
  80. date_to = fields.Date("To")
  81. is_enabled = fields.Boolean("Enabled", compute="_compute_is_enabled")
  82. _sql_constraints = [
  83. (
  84. "user_role_uniq",
  85. "unique (user_id,role_id)",
  86. "Roles can be assigned to a user only once at a time",
  87. )
  88. ]
  89. @api.depends("date_from", "date_to")
  90. def _compute_is_enabled(self):
  91. today = datetime.date.today()
  92. for role_line in self:
  93. role_line.is_enabled = True
  94. if role_line.date_from:
  95. date_from = role_line.date_from
  96. if date_from > today:
  97. role_line.is_enabled = False
  98. if role_line.date_to:
  99. date_to = role_line.date_to
  100. if today > date_to:
  101. role_line.is_enabled = False
  102. def unlink(self):
  103. users = self.mapped("user_id")
  104. res = super(ResUsersRoleLine, self).unlink()
  105. users.set_groups_from_roles(force=True)
  106. return res