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.

124 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. )
  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. res = super(ResUsersRole, self).write(vals)
  42. self.update_users()
  43. return res
  44. def unlink(self):
  45. users = self.mapped("user_ids")
  46. res = super(ResUsersRole, self).unlink()
  47. users.set_groups_from_roles(force=True)
  48. return res
  49. def update_users(self):
  50. """Update all the users concerned by the roles identified by `ids`."""
  51. users = self.mapped("user_ids")
  52. users.set_groups_from_roles()
  53. return True
  54. @api.model
  55. def cron_update_users(self):
  56. logging.info("Update user roles")
  57. self.search([]).update_users()
  58. class ResUsersRoleLine(models.Model):
  59. _name = "res.users.role.line"
  60. _description = "Users associated to a role"
  61. role_id = fields.Many2one(
  62. comodel_name="res.users.role", required=True, string="Role", ondelete="cascade"
  63. )
  64. user_id = fields.Many2one(
  65. comodel_name="res.users",
  66. required=True,
  67. string="User",
  68. domain=[("id", "!=", SUPERUSER_ID)],
  69. ondelete="cascade",
  70. )
  71. date_from = fields.Date("From")
  72. date_to = fields.Date("To")
  73. is_enabled = fields.Boolean("Enabled", compute="_compute_is_enabled")
  74. company_id = fields.Many2one(
  75. "res.company", "Company", default=lambda self: self.env.user.company_id
  76. )
  77. @api.constrains("user_id", "company_id")
  78. def _check_company(self):
  79. for record in self:
  80. if (
  81. record.company_id
  82. and record.company_id != record.user_id.company_id
  83. and record.company_id not in record.user_id.company_ids
  84. ):
  85. raise ValidationError(
  86. _('User "{}" does not have access to the company "{}"').format(
  87. record.user_id.name, record.company_id.name
  88. )
  89. )
  90. @api.depends("date_from", "date_to")
  91. def _compute_is_enabled(self):
  92. today = datetime.date.today()
  93. for role_line in self:
  94. role_line.is_enabled = True
  95. if role_line.date_from:
  96. date_from = role_line.date_from
  97. if date_from > today:
  98. role_line.is_enabled = False
  99. if role_line.date_to:
  100. date_to = role_line.date_to
  101. if today > date_to:
  102. role_line.is_enabled = False
  103. def unlink(self):
  104. users = self.mapped("user_id")
  105. res = super(ResUsersRoleLine, self).unlink()
  106. users.set_groups_from_roles(force=True)
  107. return res