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.

96 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 ABF OSIELL <http://osiell.com>
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. import datetime
  5. import logging
  6. from odoo import api, fields, models
  7. from odoo import SUPERUSER_ID
  8. _logger = logging.getLogger(__name__)
  9. class ResUsersRole(models.Model):
  10. _name = 'res.users.role'
  11. _inherits = {'res.groups': 'group_id'}
  12. _description = "User role"
  13. group_id = fields.Many2one(
  14. 'res.groups', required=True, ondelete='cascade',
  15. readonly=True, string=u"Associated group")
  16. line_ids = fields.One2many(
  17. 'res.users.role.line', 'role_id', string=u"Users")
  18. user_ids = fields.One2many(
  19. 'res.users', string=u"Users", 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 update_users(self):
  41. """Update all the users concerned by the roles identified by `ids`."""
  42. users = self.mapped('user_ids')
  43. users.set_groups_from_roles()
  44. return True
  45. @api.model
  46. def cron_update_users(self):
  47. logging.info(u"Update user roles")
  48. self.search([]).update_users()
  49. class ResUsersRoleLine(models.Model):
  50. _name = 'res.users.role.line'
  51. _description = 'Users associated to a role'
  52. role_id = fields.Many2one(
  53. 'res.users.role', string=u"Role", ondelete='cascade')
  54. user_id = fields.Many2one(
  55. 'res.users', string=u"User", domain=[('id', '!=', SUPERUSER_ID)])
  56. date_from = fields.Date(u"From")
  57. date_to = fields.Date(u"To")
  58. is_enabled = fields.Boolean(u"Enabled", compute='_compute_is_enabled')
  59. @api.multi
  60. @api.depends('date_from', 'date_to')
  61. def _compute_is_enabled(self):
  62. today = datetime.date.today()
  63. for role_line in self:
  64. role_line.is_enabled = True
  65. if role_line.date_from:
  66. date_from = fields.Date.from_string(role_line.date_from)
  67. if date_from > today:
  68. role_line.is_enabled = False
  69. if role_line.date_to:
  70. date_to = fields.Date.from_string(role_line.date_to)
  71. if today > date_to:
  72. role_line.is_enabled = False
  73. @api.multi
  74. def unlink(self):
  75. users = self.mapped('user_id')
  76. res = super(ResUsersRoleLine, self).unlink()
  77. users.set_groups_from_roles()
  78. return res