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.

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