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.

94 lines
2.9 KiB

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