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.

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