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.

104 lines
3.2 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="Role lines")
  17. user_ids = fields.One2many(
  18. comodel_name='res.users', string="Users list",
  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. string="Associated category",
  25. help="Associated group's category")
  26. @api.multi
  27. @api.depends('line_ids.user_id')
  28. def _compute_user_ids(self):
  29. for role in self:
  30. role.user_ids = role.line_ids.mapped('user_id')
  31. @api.model
  32. def create(self, vals):
  33. new_record = super(ResUsersRole, self).create(vals)
  34. new_record.update_users()
  35. return new_record
  36. @api.multi
  37. def write(self, vals):
  38. res = super(ResUsersRole, self).write(vals)
  39. self.update_users()
  40. return res
  41. @api.multi
  42. def unlink(self):
  43. users = self.mapped('user_ids')
  44. res = super(ResUsersRole, self).unlink()
  45. users.set_groups_from_roles(force=True)
  46. return res
  47. @api.multi
  48. def update_users(self):
  49. """Update all the users concerned by the roles identified by `ids`."""
  50. users = self.mapped('user_ids')
  51. users.set_groups_from_roles()
  52. return True
  53. @api.model
  54. def cron_update_users(self):
  55. logging.info("Update user roles")
  56. self.search([]).update_users()
  57. class ResUsersRoleLine(models.Model):
  58. _name = 'res.users.role.line'
  59. _description = 'Users associated to a role'
  60. role_id = fields.Many2one(
  61. comodel_name='res.users.role', string="Role",
  62. ondelete='cascade')
  63. user_id = fields.Many2one(
  64. comodel_name='res.users', string="User")
  65. date_from = fields.Date("From")
  66. date_to = fields.Date("To")
  67. is_enabled = fields.Boolean("Enabled", compute='_compute_is_enabled')
  68. @api.multi
  69. @api.depends('date_from', 'date_to')
  70. def _compute_is_enabled(self):
  71. today = datetime.date.today()
  72. for role_line in self:
  73. role_line.is_enabled = True
  74. if role_line.date_from:
  75. date_from = role_line.date_from
  76. if date_from > today:
  77. role_line.is_enabled = False
  78. if role_line.date_to:
  79. date_to = role_line.date_to
  80. if today > date_to:
  81. role_line.is_enabled = False
  82. @api.multi
  83. def unlink(self):
  84. users = self.mapped('user_id')
  85. res = super(ResUsersRoleLine, self).unlink()
  86. users.set_groups_from_roles(force=True)
  87. return res