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.

48 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. from odoo import _, api, models
  5. from odoo.exceptions import AccessError
  6. from .res_groups import IMMUTABLE
  7. class ResUsers(models.Model):
  8. _inherit = 'res.users'
  9. def _check_immutable(self):
  10. """ Check to see if the user being edited is Immutable and if so,
  11. make sure that the user performing the action has access
  12. """
  13. if self.has_group(IMMUTABLE):
  14. if not self.env.user.has_group(IMMUTABLE):
  15. raise AccessError(
  16. _('You do not have permission to alter an Immutable User')
  17. )
  18. @api.multi
  19. def write(self, vals):
  20. """ Override write to verify that there are no alterations to users
  21. whom are members of the `Immutable` group
  22. """
  23. for rec in self:
  24. rec._check_immutable()
  25. immutable = self.env.ref(IMMUTABLE)
  26. has_group = self.env.user.has_group(IMMUTABLE)
  27. if vals.get('in_group_%s' % immutable.id) and not has_group:
  28. raise AccessError(
  29. _('You must be a member of the `Immutable` group to grant '
  30. 'access to it')
  31. )
  32. return super(ResUsers, self).write(vals)
  33. @api.multi
  34. def unlink(self):
  35. """ Override unlink to verify that there are no deletions of users
  36. whom are members of the `Immutable` group
  37. """
  38. for rec in self:
  39. rec._check_immutable()
  40. return super(ResUsers, self).unlink()