summaryrefslogtreecommitdiff
path: root/odoo/api.py
diff options
context:
space:
mode:
authorRaphael Collet <rco@odoo.com>2019-05-31 09:54:23 +0000
committerRaphael Collet <rco@odoo.com>2019-07-04 09:24:23 +0000
commit1e6c3bec2c5ca621344b09e58dbed535e80fb343 (patch)
treeaa570f410f4121d102cd4b5c3d363a9aa21b40e4 /odoo/api.py
parent541012c9ea7548190eb93d3534bae32d16d76ae8 (diff)
[ADD] api: flag `su` on environments
The flag defines a "superuser mode" on environments, which allows to bypass access rights without changing the current user id.
Diffstat (limited to 'odoo/api.py')
-rw-r--r--odoo/api.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/odoo/api.py b/odoo/api.py
index bf7e6b3fbc1..e0358bba202 100644
--- a/odoo/api.py
+++ b/odoo/api.py
@@ -750,7 +750,8 @@ class Environment(Mapping):
- :attr:`cr`, the current database cursor;
- :attr:`uid`, the current user id;
- - :attr:`context`, the current context dictionary.
+ - :attr:`context`, the current context dictionary;
+ - :attr:`su`, whether in superuser mode.
It provides access to the registry by implementing a mapping from model
names to new api models. It also holds a cache for records, and a data
@@ -782,9 +783,11 @@ class Environment(Mapping):
"""
cls._local.environments = Environments()
- def __new__(cls, cr, uid, context):
+ def __new__(cls, cr, uid, context, su=False):
+ if uid == SUPERUSER_ID:
+ su = True
assert context is not None
- args = (cr, uid, context)
+ args = (cr, uid, context, su)
# if env already exists, return it
env, envs = None, cls.envs
@@ -794,10 +797,11 @@ class Environment(Mapping):
# otherwise create environment, and add it in the set
self = object.__new__(cls)
- self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context))
+ args = (cr, uid, frozendict(context), su)
+ self.cr, self.uid, self.context, self.su = self.args = args
self.registry = Registry(cr.dbname)
self.cache = envs.cache
- self._cache_key = (cr, uid)
+ self._cache_key = (cr, uid, su)
self._protected = StackMap() # {field: ids, ...}
self.dirty = defaultdict(set) # {record: set(field_name), ...}
self.all = envs
@@ -833,17 +837,19 @@ class Environment(Mapping):
def __hash__(self):
return object.__hash__(self)
- def __call__(self, cr=None, user=None, context=None):
+ def __call__(self, cr=None, user=None, context=None, su=None):
""" Return an environment based on ``self`` with modified parameters.
:param cr: optional database cursor to change the current cursor
:param user: optional user/user id to change the current user
:param context: optional context dictionary to change the current context
+ :param su: optional boolean to change the superuser mode
"""
cr = self.cr if cr is None else cr
uid = self.uid if user is None else int(user)
context = self.context if context is None else context
- return Environment(cr, uid, context)
+ su = (user is None and self.su) if su is None else su
+ return Environment(cr, uid, context, su)
def ref(self, xml_id, raise_if_not_found=True):
""" return the record corresponding to the given ``xml_id`` """
@@ -852,7 +858,7 @@ class Environment(Mapping):
@property
def user(self):
""" return the current user (as an instance) """
- return self(user=SUPERUSER_ID)['res.users'].browse(self.uid)
+ return self(su=True)['res.users'].browse(self.uid)
@property
def company(self):