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.

117 lines
3.5 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. res = super(ResUsersRole, self).write(vals)
  45. self.update_users()
  46. return res
  47. def unlink(self):
  48. users = self.mapped("user_ids")
  49. res = super(ResUsersRole, self).unlink()
  50. users.set_groups_from_roles(force=True)
  51. return res
  52. def update_users(self):
  53. """Update all the users concerned by the roles identified by `ids`."""
  54. users = self.mapped("user_ids")
  55. users.set_groups_from_roles()
  56. return True
  57. @api.model
  58. def cron_update_users(self):
  59. logging.info("Update user roles")
  60. self.search([]).update_users()
  61. class ResUsersRoleLine(models.Model):
  62. _name = "res.users.role.line"
  63. _description = "Users associated to a role"
  64. role_id = fields.Many2one(
  65. comodel_name="res.users.role", required=True, string="Role", ondelete="cascade"
  66. )
  67. user_id = fields.Many2one(
  68. comodel_name="res.users",
  69. required=True,
  70. string="User",
  71. domain=[("id", "!=", SUPERUSER_ID)],
  72. ondelete="cascade",
  73. )
  74. date_from = fields.Date("From")
  75. date_to = fields.Date("To")
  76. is_enabled = fields.Boolean("Enabled", compute="_compute_is_enabled")
  77. _sql_constraints = [
  78. (
  79. "user_role_uniq",
  80. "unique (user_id,role_id)",
  81. "Roles can be assigned to a user only once at a time",
  82. )
  83. ]
  84. @api.depends("date_from", "date_to")
  85. def _compute_is_enabled(self):
  86. today = datetime.date.today()
  87. for role_line in self:
  88. role_line.is_enabled = True
  89. if role_line.date_from:
  90. date_from = role_line.date_from
  91. if date_from > today:
  92. role_line.is_enabled = False
  93. if role_line.date_to:
  94. date_to = role_line.date_to
  95. if today > date_to:
  96. role_line.is_enabled = False
  97. def unlink(self):
  98. users = self.mapped("user_id")
  99. res = super(ResUsersRoleLine, self).unlink()
  100. users.set_groups_from_roles(force=True)
  101. return res