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.

112 lines
3.6 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. from odoo.exceptions import ValidationError
  9. _logger = logging.getLogger(__name__)
  10. class ResUsersRole(models.Model):
  11. _name = 'res.users.role'
  12. _inherits = {'res.groups': 'group_id'}
  13. _description = "User role"
  14. group_id = fields.Many2one(
  15. 'res.groups', required=True, ondelete='cascade',
  16. readonly=True, string=u"Associated group")
  17. line_ids = fields.One2many(
  18. 'res.users.role.line', 'role_id', string=u"Users")
  19. user_ids = fields.One2many(
  20. 'res.users', string=u"Users", 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. @api.multi
  26. @api.depends('line_ids.user_id')
  27. def _compute_user_ids(self):
  28. for role in self:
  29. role.user_ids = role.line_ids.mapped('user_id')
  30. @api.model
  31. def create(self, vals):
  32. new_record = super(ResUsersRole, self).create(vals)
  33. new_record.update_users()
  34. return new_record
  35. @api.multi
  36. def write(self, vals):
  37. res = super(ResUsersRole, self).write(vals)
  38. self.update_users()
  39. return res
  40. @api.multi
  41. def update_users(self):
  42. """Update all the users concerned by the roles identified by `ids`."""
  43. users = self.mapped('user_ids')
  44. users.set_groups_from_roles()
  45. return True
  46. @api.model
  47. def cron_update_users(self):
  48. logging.info(u"Update user roles")
  49. self.search([]).update_users()
  50. class ResUsersRoleLine(models.Model):
  51. _name = 'res.users.role.line'
  52. _description = 'Users associated to a role'
  53. role_id = fields.Many2one(
  54. 'res.users.role', required=True, string=u"Role", ondelete='cascade')
  55. user_id = fields.Many2one(
  56. 'res.users', required=True, string=u"User",
  57. domain=[('id', '!=', SUPERUSER_ID)], ondelete='cascade')
  58. date_from = fields.Date(u"From")
  59. date_to = fields.Date(u"To")
  60. is_enabled = fields.Boolean(u"Enabled", compute='_compute_is_enabled')
  61. company_id = fields.Many2one(
  62. 'res.company', 'Company',
  63. default=lambda self: self.env.user.company_id)
  64. @api.multi
  65. @api.constrains('user_id', 'company_id')
  66. def _check_company(self):
  67. for record in self:
  68. if (record.company_id and
  69. record.company_id != record.user_id.company_id and
  70. record.company_id not in record.user_id.company_ids):
  71. raise ValidationError(
  72. _('User "{}" does not have access to the company "{}"')
  73. .format(record.user_id.name, record.company_id.name))
  74. @api.multi
  75. @api.depends('date_from', 'date_to')
  76. def _compute_is_enabled(self):
  77. today = datetime.date.today()
  78. for role_line in self:
  79. role_line.is_enabled = True
  80. if role_line.date_from:
  81. date_from = fields.Date.from_string(role_line.date_from)
  82. if date_from > today:
  83. role_line.is_enabled = False
  84. if role_line.date_to:
  85. date_to = fields.Date.from_string(role_line.date_to)
  86. if today > date_to:
  87. role_line.is_enabled = False
  88. @api.multi
  89. def unlink(self):
  90. users = self.mapped('user_id')
  91. res = super(ResUsersRoleLine, self).unlink()
  92. users.set_groups_from_roles()
  93. return res