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.

135 lines
5.3 KiB

7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  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 logging
  20. from odoo import _
  21. from odoo import models, api, fields
  22. from odoo.exceptions import AccessError
  23. _logger = logging.getLogger(__name__)
  24. class BaseModelAccess(models.AbstractModel):
  25. _name = 'muk_security.access'
  26. _description = "MuK Access Model"
  27. _inherit = 'muk_utils.model'
  28. #----------------------------------------------------------
  29. # Database
  30. #----------------------------------------------------------
  31. permission_read = fields.Boolean(
  32. compute='_compute_permissions',
  33. search='_search_permission_read',
  34. string="Read Access")
  35. permission_create = fields.Boolean(
  36. compute='_compute_permissions',
  37. search='_search_permission_create',
  38. string="Create Access")
  39. permission_write = fields.Boolean(
  40. compute='_compute_permissions',
  41. search='_search_permission_write',
  42. string="Write Access")
  43. permission_unlink = fields.Boolean(
  44. compute='_compute_permissions',
  45. search='_search_permission_unlink',
  46. string="Delete Access")
  47. #----------------------------------------------------------
  48. # Function
  49. #----------------------------------------------------------
  50. @api.model
  51. def check_access_rights(self, operation, raise_exception=True):
  52. return super(BaseModelAccess, self).check_access_rights(operation, raise_exception)
  53. @api.multi
  54. def check_access_rule(self, operation):
  55. return super(BaseModelAccess, self).check_access_rule(operation)
  56. @api.model
  57. def _apply_ir_rules(self, query, mode='read'):
  58. return super(BaseModelAccess, self)._apply_ir_rules(query, mode)
  59. @api.model
  60. def check_field_access_rights(self, operation, fields):
  61. return super(BaseModelAccess, self).check_field_access_rights(operation, fields)
  62. @api.multi
  63. def check_access(self, operation, raise_exception=False):
  64. try:
  65. access_right = self.check_access_rights(operation, raise_exception)
  66. access_rule = self.check_access_rule(operation) == None
  67. access = access_right and access_rule
  68. if not access and raise_exception:
  69. raise AccessError(_("This operation is forbidden!"))
  70. return access
  71. except AccessError:
  72. if raise_exception:
  73. raise AccessError(_("This operation is forbidden!"))
  74. return False
  75. #----------------------------------------------------------
  76. # Search
  77. #----------------------------------------------------------
  78. @api.model
  79. def _search_permission_read(self, operator, operand):
  80. records = self.search([]).filtered(lambda r: r.check_access('read') == True)
  81. if operator == '=' and operand:
  82. return [('id', 'in', records.mapped('id'))]
  83. return [('id', 'not in', records.mapped('id'))]
  84. @api.model
  85. def _search_permission_create(self, operator, operand):
  86. records = self.search([]).filtered(lambda r: r.check_access('create') == True)
  87. if operator == '=' and operand:
  88. return [('id', 'in', records.mapped('id'))]
  89. return [('id', 'not in', records.mapped('id'))]
  90. @api.model
  91. def _search_permission_write(self, operator, operand):
  92. records = self.search([]).filtered(lambda r: r.check_access('write') == True)
  93. if operator == '=' and operand:
  94. return [('id', 'in', records.mapped('id'))]
  95. return [('id', 'not in', records.mapped('id'))]
  96. @api.model
  97. def _search_permission_unlink(self, operator, operand):
  98. records = self.search([]).filtered(lambda r: r.check_access('unlink') == True)
  99. if operator == '=' and operand:
  100. return [('id', 'in', records.mapped('id'))]
  101. return [('id', 'not in', records.mapped('id'))]
  102. #----------------------------------------------------------
  103. # Read, View
  104. #----------------------------------------------------------
  105. @api.multi
  106. def _compute_permissions(self):
  107. for record in self:
  108. record.update({
  109. 'permission_read': record.check_access('read'),
  110. 'permission_create': record.check_access('create'),
  111. 'permission_write': record.check_access('write'),
  112. 'permission_unlink': record.check_access('unlink'),
  113. })