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.

103 lines
3.1 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 openerp 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. _defaults = { # pylint: disable=attribute-deprecated
  20. 'category_id': api.model(
  21. lambda cls: cls.env.ref(
  22. 'base_user_role.ir_module_category_role').id),
  23. }
  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(u"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. 'res.users.role', string=u"Role", ondelete='cascade')
  60. user_id = fields.Many2one(
  61. 'res.users', string=u"User")
  62. date_from = fields.Date(u"From")
  63. date_to = fields.Date(u"To")
  64. is_enabled = fields.Boolean(u"Enabled", compute='_compute_is_enabled')
  65. @api.multi
  66. @api.depends('date_from', 'date_to')
  67. def _compute_is_enabled(self):
  68. today = datetime.date.today()
  69. for role_line in self:
  70. role_line.is_enabled = True
  71. if role_line.date_from:
  72. date_from = fields.Date.from_string(role_line.date_from)
  73. if date_from > today:
  74. role_line.is_enabled = False
  75. if role_line.date_to:
  76. date_to = fields.Date.from_string(role_line.date_to)
  77. if today > date_to:
  78. role_line.is_enabled = False
  79. @api.multi
  80. def unlink(self):
  81. users = self.mapped('user_id')
  82. res = super(ResUsersRoleLine, self).unlink()
  83. users.set_groups_from_roles(force=True)
  84. return res