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.

106 lines
3.3 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. _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', required=True, ondelete='cascade',
  14. readonly=True, string="Associated group")
  15. line_ids = fields.One2many(
  16. comodel_name='res.users.role.line',
  17. inverse_name='role_id', string="Role lines")
  18. user_ids = fields.One2many(
  19. comodel_name='res.users', string="Users list",
  20. compute='_compute_user_ids')
  21. group_category_id = fields.Many2one(
  22. related='group_id.category_id',
  23. default=lambda cls: cls.env.ref(
  24. 'base_user_role.ir_module_category_role').id,
  25. string="Associated category",
  26. help="Associated group's category")
  27. @api.multi
  28. @api.depends('line_ids.user_id')
  29. def _compute_user_ids(self):
  30. for role in self:
  31. role.user_ids = role.line_ids.mapped('user_id')
  32. @api.model
  33. def create(self, vals):
  34. new_record = super(ResUsersRole, self).create(vals)
  35. new_record.update_users()
  36. return new_record
  37. @api.multi
  38. def write(self, vals):
  39. res = super(ResUsersRole, self).write(vals)
  40. self.update_users()
  41. return res
  42. @api.multi
  43. def unlink(self):
  44. users = self.mapped('user_ids')
  45. res = super(ResUsersRole, self).unlink()
  46. users.set_groups_from_roles(force=True)
  47. return res
  48. @api.multi
  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",
  63. ondelete='cascade')
  64. user_id = fields.Many2one(
  65. comodel_name='res.users', required=True, string="User",
  66. domain=[('id', '!=', SUPERUSER_ID)], ondelete='cascade')
  67. date_from = fields.Date("From")
  68. date_to = fields.Date("To")
  69. is_enabled = fields.Boolean("Enabled", compute='_compute_is_enabled')
  70. @api.multi
  71. @api.depends('date_from', 'date_to')
  72. def _compute_is_enabled(self):
  73. today = datetime.date.today()
  74. for role_line in self:
  75. role_line.is_enabled = True
  76. if role_line.date_from:
  77. date_from = role_line.date_from
  78. if date_from > today:
  79. role_line.is_enabled = False
  80. if role_line.date_to:
  81. date_to = role_line.date_to
  82. if today > date_to:
  83. role_line.is_enabled = False
  84. @api.multi
  85. def unlink(self):
  86. users = self.mapped('user_id')
  87. res = super(ResUsersRoleLine, self).unlink()
  88. users.set_groups_from_roles(force=True)
  89. return res