models.py 1.21 KB
Newer Older
Guewen Baconnier's avatar
Guewen Baconnier committed
1
2
3
4
# -*- coding: utf-8 -*-
# © 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

5
from odoo import models, api, exceptions
Guewen Baconnier's avatar
Guewen Baconnier committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


@api.multi
def check_access_rule_all(self, operations=None):
    """Verifies that the operation given by ``operations`` is allowed for the
    user according to ir.rules.

    If ``operations`` is empty, it returns the result for all actions.

   :param operation: a list of ``read``, ``create``, ``write``, ``unlink``
   :return: {operation: access} (access is a boolean)
    """
    if operations is None:
        operations = ['read', 'create', 'write', 'unlink']
    result = {}
    for operation in operations:
22
23
24
25
26
27
        if self.is_transient() and not self.ids:
            # If we call check_access_rule() without id, it will try to run a
            # SELECT without ID which will crash, so we just blindly allow the
            # operations
            result[operation] = True
            continue
Guewen Baconnier's avatar
Guewen Baconnier committed
28
29
30
31
32
33
34
35
36
37
        try:
            self.check_access_rule(operation)
        except exceptions.AccessError:
            result[operation] = False
        else:
            result[operation] = True
    return result


models.BaseModel.check_access_rule_all = check_access_rule_all