Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

License: AGPL-3

Restrict field access

This module was written to help developers restricting access to fields in a secure and flexible manner on record level.

If you’re not a developer, this module is not for you as you need to write code in order to actually use it.

Usage

To use this module, you need to inherit this mixin for the model whose fields you want to restrict, and implement at least the following methods to do something useful:

class ResPartner(models.Model):
    # inherit from the mixin
    _inherit = ['restrict.field.access.mixin', 'res.partner']
    _name = 'res.partner'

    @api.multi
    def _restrict_field_access_get_field_whitelist(self, action='read'):
        # return a whitelist (or a blacklist) of fields, depending on the
        # action passed
        whitelist = [
            'name', 'parent_id', 'is_company', 'firstname', 'lastname',
            'infix', 'initials',
        ] + super(ResPartner, self)\
            ._restrict_field_access_get_field_whitelist(action=action)
        if action == 'read':
            whitelist.extend(['section_id', 'user_id'])
        return whitelist

    @api.multi
    def _restrict_field_access_is_field_accessible(self, field_name,
                                                   action='read'):
        # in case the whitelist is not enough, you can also decide for
        # specific records if an action can be carried out on it or not
        result = super(ResPartner, self)\
            ._restrict_field_access_is_field_accessible(
                field_name, action=action)
        if result or not self:
            return result
        return all(this.section_id in self.env.user.section_ids or
                   this.user_id == self.env.user
                   for this in self)

    @api.multi
    @api.onchange('section_id', 'user_id')
    @api.depends('section_id', 'user_id')
    def _compute_restrict_field_access(self):
        # if your decision depends on other fields, you probably need to
        # override this function in order to attach the correct onchange/
        # depends decorators
        return super(ResPartner, self)._compute_restrict_field_access()

    @api.model
    def _restrict_field_access_inject_restrict_field_access_domain(
            self, domain):
        # you also might want to decide with a domain expression which
        # records are visible in the first place
        domain[:] = expression.AND([
            domain,
            [
                '|',
                ('section_id', 'in', self.env.user.section_ids.ids),
                ('user_id', '=', self.env.user.id),
            ],
        ])

The example code here will allow only reading a few fields for partners of which the current user is neither the sales person nor in this partner’s sales team.

Read the comments of the mixin, that’s part of the documentation. Also have a look at the tests, that’s another example on how to use this code.

For further information, please visit:

Known issues / Roadmap

  • the code contains some TODOs which should be done

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed feedback here.

Credits

Contributors

Maintainer

Odoo Community Association

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.

Metadata

Release files for odoo8-addon-base-mixin-restrict-field-access 8.0.1.0.0.99.dev10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for odoo8-addon-base-mixin-restrict-field-access 8.0.1.0.0.99.dev10
File Interpreter ABI Platform
odoo8_addon_base_mixin_restrict_field_access-8.0.1.0.0.99.dev10-py2-none-any.whl Python 2 none any Details

Release files / odoo8_addon_base_mixin_restrict_field_access-8.0.1.0.0.99.dev10-py2-none-any.whl

Download URL odoo8_addon_base_mixin_restrict_field_access-8.0.1.0.0.99.dev10-py2-none-any.whl
Size 83.6 kB
Tags Python 2
SHA-256 checksum
How to use checksums
6607a993765b07834aa00eea4bf11e0b244623fe809c1a9ba4f902f2df211c8a
BLAKE2b-256 checksum
How to use checksums
62a017e73060b14c8fb2aa3048a47f7e50b9f863a6c61ce7f1c096fdcb0afb82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.5
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page