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.

102 lines
3.4 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2017 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import os
  20. import hashlib
  21. import logging
  22. import itertools
  23. from odoo import _, SUPERUSER_ID
  24. from odoo import models, api, fields
  25. from odoo.exceptions import AccessError
  26. from odoo.addons.muk_security.tools.security import NoSecurityUid
  27. _logger = logging.getLogger(__name__)
  28. class LockingModel(models.AbstractModel):
  29. _name = 'muk_security.mixins.locking'
  30. _description = 'Locking Mixin'
  31. #----------------------------------------------------------
  32. # Database
  33. #----------------------------------------------------------
  34. locked_by = fields.Many2one(
  35. comodel_name='res.users',
  36. string="Locked by")
  37. is_locked = fields.Boolean(
  38. compute='_compute_locked',
  39. string="Locked")
  40. is_lock_editor = fields.Boolean(
  41. compute='_compute_locked',
  42. string="Editor")
  43. #----------------------------------------------------------
  44. # Locking
  45. #----------------------------------------------------------
  46. @api.multi
  47. def lock(self):
  48. self.write({'locked_by': self.env.uid})
  49. @api.multi
  50. def unlock(self):
  51. self.write({'locked_by': None})
  52. @api.model
  53. def _check_lock_user(self, uid):
  54. return uid in (self.env.uid, SUPERUSER_ID) or isinstance(self.env.uid, NoSecurityUid)
  55. @api.multi
  56. def check_lock(self, *largs, **kwargs):
  57. for record in self:
  58. if record.locked_by.exists() and not self._check_lock_user(record.locked_by.id):
  59. raise AccessError(_("The record (%s [%s]) is locked, by an other user.") % (record._description, record.id))
  60. #----------------------------------------------------------
  61. # Read, View
  62. #----------------------------------------------------------
  63. @api.depends('locked_by')
  64. def _compute_locked(self):
  65. for record in self:
  66. if record.locked_by.exists():
  67. record.update({'is_locked': True, 'is_lock_editor': record.locked_by.id == record.env.uid})
  68. else:
  69. record.update({'is_locked': False, 'is_lock_editor': False})
  70. #----------------------------------------------------------
  71. # Create, Update, Delete
  72. #----------------------------------------------------------
  73. @api.multi
  74. def write(self, vals):
  75. self.check_lock()
  76. return super(BaseModelLocking, self).write(vals)
  77. @api.multi
  78. def unlink(self):
  79. self.check_lock()
  80. return super(BaseModelLocking, self).unlink()