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.

123 lines
4.4 KiB

  1. .. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
  2. :alt: License: AGPL-3
  3. =====================
  4. Restrict field access
  5. =====================
  6. This module was written to help developers restricting access to fields in a
  7. secure and flexible manner on record level.
  8. If you're not a developer, this module is not for you as you need to write code
  9. in order to actually use it.
  10. Usage
  11. =====
  12. To use this module, you need to inherit this mixin for the model whose fields
  13. you want to restrict, and implement at least the following methods to do
  14. something useful:
  15. .. code:: python
  16. class ResPartner(models.Model):
  17. # inherit from the mixin
  18. _inherit = ['restrict.field.access.mixin', 'res.partner']
  19. _name = 'res.partner'
  20. @api.multi
  21. def _restrict_field_access_get_field_whitelist(self, action='read'):
  22. # return a whitelist (or a blacklist) of fields, depending on the
  23. # action passed
  24. whitelist = [
  25. 'name', 'parent_id', 'is_company', 'firstname', 'lastname',
  26. 'infix', 'initials',
  27. ] + super(ResPartner, self)\
  28. ._restrict_field_access_get_field_whitelist(action=action)
  29. if action == 'read':
  30. whitelist.extend(['section_id', 'user_id'])
  31. return whitelist
  32. @api.multi
  33. def _restrict_field_access_is_field_accessible(self, field_name,
  34. action='read'):
  35. # in case the whitelist is not enough, you can also decide for
  36. # specific records if an action can be carried out on it or not
  37. result = super(ResPartner, self)\
  38. ._restrict_field_access_is_field_accessible(
  39. field_name, action=action)
  40. if result or not self:
  41. return result
  42. return all(this.section_id in self.env.user.section_ids or
  43. this.user_id == self.env.user
  44. for this in self)
  45. @api.multi
  46. @api.onchange('section_id', 'user_id')
  47. @api.depends('section_id', 'user_id')
  48. def _compute_restrict_field_access(self):
  49. # if your decision depends on other fields, you probably need to
  50. # override this function in order to attach the correct onchange/
  51. # depends decorators
  52. return super(ResPartner, self)._compute_restrict_field_access()
  53. @api.model
  54. def _restrict_field_access_inject_restrict_field_access_domain(
  55. self, domain):
  56. # you also might want to decide with a domain expression which
  57. # records are visible in the first place
  58. domain[:] = expression.AND([
  59. domain,
  60. [
  61. '|',
  62. ('section_id', 'in', self.env.user.section_ids.ids),
  63. ('user_id', '=', self.env.user.id),
  64. ],
  65. ])
  66. The example code here will allow only reading a few fields for partners of
  67. which the current user is neither the sales person nor in this partner's sales
  68. team.
  69. Read the comments of the mixin, that's part of the documentation. Also have a
  70. look at the tests, that's another example on how to use this code.
  71. For further information, please visit:
  72. * https://www.odoo.com/forum/help-1
  73. Known issues / Roadmap
  74. ======================
  75. * the code contains some TODOs which should be done
  76. Bug Tracker
  77. ===========
  78. Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
  79. In case of trouble, please check there if your issue has already been reported.
  80. If you spotted it first, help us smashing it by providing a detailed and welcomed feedback
  81. `here <https://github.com/OCA/server-tools/issues/new?body=module:%20base_mixin_restrict_field_access%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
  82. Credits
  83. =======
  84. Contributors
  85. ------------
  86. * Holger Brunn <hbrunn@therp.nl>
  87. Maintainer
  88. ----------
  89. .. image:: https://odoo-community.org/logo.png
  90. :alt: Odoo Community Association
  91. :target: https://odoo-community.org
  92. This module is maintained by the OCA.
  93. OCA, or the Odoo Community Association, is a nonprofit organization whose
  94. mission is to support the collaborative development of Odoo features and
  95. promote its widespread use.
  96. To contribute to this module, please visit https://odoo-community.org.