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.

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