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. from odoo.exceptions import ValidationError
  7. _logger = logging.getLogger(__name__)
  8. class ResUsersRole(models.Model):
  9. _name = "res.users.role"
  10. _inherits = {"res.groups": "group_id"}
  11. _description = "User role"
  12. group_id = fields.Many2one(
  13. comodel_name="res.groups",
  14. required=True,
  15. ondelete="cascade",
  16. readonly=True,
  17. string="Associated group",
  18. )
  19. line_ids = fields.One2many(
  20. comodel_name="res.users.role.line", inverse_name="role_id", string="Role lines"
  21. )
  22. user_ids = fields.One2many(
  23. comodel_name="res.users", string="Users list", compute="_compute_user_ids"
  24. )
  25. group_category_id = fields.Many2one(
  26. related="group_id.category_id",
  27. default=lambda cls: cls.env.ref("base_user_role.ir_module_category_role").id,
  28. string="Associated category",
  29. help="Associated group's category",
  30. readonly=False,
  31. )
  32. @api.depends("line_ids.user_id")
  33. def _compute_user_ids(self):
  34. for role in self:
  35. role.user_ids = role.line_ids.mapped("user_id")
  36. @api.model
  37. def create(self, vals):
  38. new_record = super(ResUsersRole, self).create(vals)
  39. new_record.update_users()
  40. return new_record
  41. def write(self, vals):
  42. res = super(ResUsersRole, self).write(vals)
  43. self.update_users()
  44. return res
  45. def unlink(self):
  46. users = self.mapped("user_ids")
  47. res = super(ResUsersRole, self).unlink()
  48. users.set_groups_from_roles(force=True)
  49. return res
  50. def update_users(self):
  51. """Update all the users concerned by the roles identified by `ids`."""
  52. users = self.mapped("user_ids")
  53. users.set_groups_from_roles()
  54. return True
  55. @api.model
  56. def cron_update_users(self):
  57. logging.info("Update user roles")
  58. self.search([]).update_users()
  59. class ResUsersRoleLine(models.Model):
  60. _name = "res.users.role.line"
  61. _description = "Users associated to a role"
  62. role_id = fields.Many2one(
  63. comodel_name="res.users.role", required=True, string="Role", ondelete="cascade"
  64. )
  65. user_id = fields.Many2one(
  66. comodel_name="res.users",
  67. required=True,
  68. string="User",
  69. domain=[("id", "!=", SUPERUSER_ID)],
  70. ondelete="cascade",
  71. )
  72. date_from = fields.Date("From")
  73. date_to = fields.Date("To")
  74. is_enabled = fields.Boolean("Enabled", compute="_compute_is_enabled")
  75. company_id = fields.Many2one(
  76. "res.company", "Company", default=lambda self: self.env.user.company_id
  77. )
  78. @api.constrains("user_id", "company_id")
  79. def _check_company(self):
  80. for record in self:
  81. if (
  82. record.company_id
  83. and record.company_id != record.user_id.company_id
  84. and record.company_id not in record.user_id.company_ids
  85. ):
  86. raise ValidationError(
  87. _('User "{}" does not have access to the company "{}"').format(
  88. record.user_id.name, record.company_id.name
  89. )
  90. )
  91. @api.depends("date_from", "date_to")
  92. def _compute_is_enabled(self):
  93. today = datetime.date.today()
  94. for role_line in self:
  95. role_line.is_enabled = True
  96. if role_line.date_from:
  97. date_from = role_line.date_from
  98. if date_from > today:
  99. role_line.is_enabled = False
  100. if role_line.date_to:
  101. date_to = role_line.date_to
  102. if today > date_to:
  103. role_line.is_enabled = False
  104. def unlink(self):
  105. users = self.mapped("user_id")
  106. res = super(ResUsersRoleLine, self).unlink()
  107. users.set_groups_from_roles(force=True)
  108. return res