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.5 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_editor(self, lock_uid):
  54. return lock_uid in (self.env.uid, SUPERUSER_ID) or isinstance(self.env.uid, NoSecurityUid)
  55. @api.multi
  56. def check_lock(self):
  57. for record in self:
  58. if record.locked_by.exists() and not self._check_lock_editor(record.locked_by.id):
  59. message = _("The record (%s [%s]) is locked, by an other user.")
  60. raise AccessError(message % (record._description, record.id))
  61. #----------------------------------------------------------
  62. # Read, View
  63. #----------------------------------------------------------
  64. @api.depends('locked_by')
  65. def _compute_locked(self):
  66. for record in self:
  67. if record.locked_by.exists():
  68. record.update({'is_locked': True, 'is_lock_editor': record.locked_by.id == record.env.uid})
  69. else:
  70. record.update({'is_locked': False, 'is_lock_editor': False})
  71. #----------------------------------------------------------
  72. # Create, Update, Delete
  73. #----------------------------------------------------------
  74. @api.multi
  75. def _write(self, vals):
  76. self.check_lock()
  77. return super(LockingModel, self)._write(vals)
  78. @api.multi
  79. def unlink(self):
  80. self.check_lock()
  81. return super(LockingModel, self).unlink()