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.

106 lines
3.4 KiB

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