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.

102 lines
3.1 KiB

  1. # Copyright 2014 ABF OSIELL <http://osiell.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import datetime
  4. import logging
  5. from odoo import 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', required=True, ondelete='cascade',
  13. readonly=True, string="Associated group")
  14. line_ids = fields.One2many(
  15. comodel_name='res.users.role.line',
  16. inverse_name='role_id', string="Users")
  17. user_ids = fields.One2many(
  18. comodel_name='res.users', string="Users",
  19. compute='_compute_user_ids')
  20. group_category_id = fields.Many2one(
  21. related='group_id.category_id',
  22. default=lambda cls: cls.env.ref(
  23. 'base_user_role.ir_module_category_role').id)
  24. @api.multi
  25. @api.depends('line_ids.user_id')
  26. def _compute_user_ids(self):
  27. for role in self:
  28. role.user_ids = role.line_ids.mapped('user_id')
  29. @api.model
  30. def create(self, vals):
  31. new_record = super(ResUsersRole, self).create(vals)
  32. new_record.update_users()
  33. return new_record
  34. @api.multi
  35. def write(self, vals):
  36. res = super(ResUsersRole, self).write(vals)
  37. self.update_users()
  38. return res
  39. @api.multi
  40. def unlink(self):
  41. users = self.mapped('user_ids')
  42. res = super(ResUsersRole, self).unlink()
  43. users.set_groups_from_roles(force=True)
  44. return res
  45. @api.multi
  46. def update_users(self):
  47. """Update all the users concerned by the roles identified by `ids`."""
  48. users = self.mapped('user_ids')
  49. users.set_groups_from_roles()
  50. return True
  51. @api.model
  52. def cron_update_users(self):
  53. logging.info("Update user roles")
  54. self.search([]).update_users()
  55. class ResUsersRoleLine(models.Model):
  56. _name = 'res.users.role.line'
  57. _description = 'Users associated to a role'
  58. role_id = fields.Many2one(
  59. comodel_name='res.users.role', string="Role",
  60. ondelete='cascade')
  61. user_id = fields.Many2one(
  62. comodel_name='res.users', string="User")
  63. date_from = fields.Date("From")
  64. date_to = fields.Date("To")
  65. is_enabled = fields.Boolean("Enabled", compute='_compute_is_enabled')
  66. @api.multi
  67. @api.depends('date_from', 'date_to')
  68. def _compute_is_enabled(self):
  69. today = datetime.date.today()
  70. for role_line in self:
  71. role_line.is_enabled = True
  72. if role_line.date_from:
  73. date_from = fields.Date.from_string(role_line.date_from)
  74. if date_from > today:
  75. role_line.is_enabled = False
  76. if role_line.date_to:
  77. date_to = fields.Date.from_string(role_line.date_to)
  78. if today > date_to:
  79. role_line.is_enabled = False
  80. @api.multi
  81. def unlink(self):
  82. users = self.mapped('user_id')
  83. res = super(ResUsersRoleLine, self).unlink()
  84. users.set_groups_from_roles(force=True)
  85. return res