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.

125 lines
4.8 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 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 AccessModel(models.AbstractModel):
  25. _name = 'muk_security.mixins.access'
  26. _description = 'Access Mixin'
  27. #----------------------------------------------------------
  28. # Database
  29. #----------------------------------------------------------
  30. permission_read = fields.Boolean(
  31. compute='_compute_permissions_read',
  32. search='_search_permission_read',
  33. string="Read Access")
  34. permission_create = fields.Boolean(
  35. compute='_compute_permissions_create',
  36. search='_search_permission_create',
  37. string="Create Access")
  38. permission_write = fields.Boolean(
  39. compute='_compute_permissions_write',
  40. search='_search_permission_write',
  41. string="Write Access")
  42. permission_unlink = fields.Boolean(
  43. compute='_compute_permissions_unlink',
  44. search='_search_permission_unlink',
  45. string="Delete Access")
  46. #----------------------------------------------------------
  47. # Function
  48. #----------------------------------------------------------
  49. @api.multi
  50. def check_access(self, operation, raise_exception=False):
  51. try:
  52. access_right = self.check_access_rights(operation, raise_exception)
  53. access_rule = self.check_access_rule(operation) is None
  54. return access_right and access_rule
  55. except AccessError:
  56. if raise_exception:
  57. raise
  58. return False
  59. #----------------------------------------------------------
  60. # Search
  61. #----------------------------------------------------------
  62. @api.model
  63. def _search_permission_read(self, operator, operand):
  64. records = self.search([]).filtered(lambda r: r.check_access('read') == True)
  65. if operator == '=' and operand:
  66. return [('id', 'in', records.mapped('id'))]
  67. return [('id', 'not in', records.mapped('id'))]
  68. @api.model
  69. def _search_permission_create(self, operator, operand):
  70. records = self.search([]).filtered(lambda r: r.check_access('create') == True)
  71. if operator == '=' and operand:
  72. return [('id', 'in', records.mapped('id'))]
  73. return [('id', 'not in', records.mapped('id'))]
  74. @api.model
  75. def _search_permission_write(self, operator, operand):
  76. records = self.search([]).filtered(lambda r: r.check_access('write') == True)
  77. if operator == '=' and operand:
  78. return [('id', 'in', records.mapped('id'))]
  79. return [('id', 'not in', records.mapped('id'))]
  80. @api.model
  81. def _search_permission_unlink(self, operator, operand):
  82. records = self.search([]).filtered(lambda r: r.check_access('unlink') == True)
  83. if operator == '=' and operand:
  84. return [('id', 'in', records.mapped('id'))]
  85. return [('id', 'not in', records.mapped('id'))]
  86. #----------------------------------------------------------
  87. # Read, View
  88. #----------------------------------------------------------
  89. @api.multi
  90. def _compute_permissions_read(self):
  91. for record in self:
  92. record.update({'permission_read': record.check_access('read')})
  93. @api.multi
  94. def _compute_permissions_create(self):
  95. for record in self:
  96. record.update({'permission_create': record.check_access('create')})
  97. @api.multi
  98. def _compute_permissions_write(self):
  99. for record in self:
  100. record.update({'permission_write': record.check_access('write')})
  101. @api.multi
  102. def _compute_permissions_unlink(self):
  103. for record in self:
  104. record.update({'permission_unlink': record.check_access('unlink')})