Remove user from evaluation context of rule domain

Closes #12581
This commit is contained in:
Cédric Krier committed 2023-09-24 13:35:39 +02:00
1 parent d87effab7b
commit 0c8303994e
6 files changed
+40 -95

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* Remove user from evaluation context of rule domain
* Allow customizing context per model for rule
* Use multiple jobs to dump and restore a test database
* Support database cache as template for tests
+17 -30
View File
@@ -6,13 +6,11 @@ from sql import Literal
from trytond.cache import Cache
from trytond.i18n import gettext
from trytond.model import (
Check, EvalEnvironment, Index, ModelSQL, ModelView, fields)
from trytond.model import Check, Index, ModelSQL, ModelView, fields
from trytond.model.exceptions import ValidationError
from trytond.pool import Pool
from trytond.pyson import PYSONDecoder
from trytond.transaction import (
Transaction, inactive_records, without_check_access)
from trytond.transaction import Transaction, inactive_records
class DomainError(ValidationError):
@@ -108,7 +106,6 @@ class Rule(ModelSQL, ModelView):
required=True, ondelete="CASCADE")
domain = fields.Char('Domain', required=True,
help="Domain is evaluated with a PYSON context containing:"
'\n- "user" as the current user'
'\n- "groups" as list of ids from the current user')
_domain_get_cache = Cache('ir_rule.domain_get', context=False)
@@ -151,22 +148,18 @@ class Rule(ModelSQL, ModelView):
@classmethod
def _get_context(cls, model_name):
User = Pool().get('res.user')
transaction = Transaction()
user_id = transaction.user
with transaction.set_context(_datetime=None), \
without_check_access(), \
transaction.set_user(0):
user = EvalEnvironment(User(user_id), User)
pool = Pool()
User = pool.get('res.user')
return {
'user': user,
'groups': User.get_groups()
}
@classmethod
def _get_cache_key(cls, model_name):
pool = Pool()
User = pool.get('res.user')
# _datetime value will be added to the domain
return (Transaction().user, Transaction().context.get('_datetime'))
return (Transaction().context.get('_datetime'), User.get_groups())
@classmethod
def get(cls, model_name, mode='read'):
@@ -175,16 +168,17 @@ class Rule(ModelSQL, ModelView):
RuleGroup = pool.get('ir.rule.group')
Model = pool.get('ir.model')
RuleGroup_Group = pool.get('ir.rule.group-res.group')
User_Group = pool.get('res.user-res.group')
User = pool.get('res.user')
rule_table = cls.__table__()
rule_group = RuleGroup.__table__()
rule_group_group = RuleGroup_Group.__table__()
user_group = User_Group.user_group_all_table()
model = Model.__table__()
transaction = Transaction()
assert mode in cls.modes
groups = User.get_groups()
model_names = []
model2field = defaultdict(list)
@@ -205,25 +199,19 @@ class Rule(ModelSQL, ModelView):
update_model_names(pool.get(model_name))
cursor = transaction.connection.cursor()
user_id = transaction.user
# root user above constraint
if user_id == 0:
if transaction.user == 0:
return {}, {}
cursor.execute(*rule_table.join(rule_group,
condition=rule_group.id == rule_table.rule_group
).join(model,
condition=rule_group.model == model.id
).join(rule_group_group, 'LEFT',
condition=rule_group_group.rule_group == rule_group.id
).select(rule_table.id,
where=(model.model.in_(model_names))
& (getattr(rule_group, 'perm_%s' % mode) == Literal(True))
& (rule_group.id.in_(
rule_group_group.join(
user_group,
condition=(rule_group_group.group
== user_group.group)
).select(rule_group_group.rule_group,
where=user_group.user == user_id)
)
& (rule_group_group.group.in_(groups or [-1])
| (rule_group.default_p == Literal(True))
| (rule_group.global_p == Literal(True))
)))
@@ -232,13 +220,12 @@ class Rule(ModelSQL, ModelView):
# Test if there is no rule_group that have no rule
cursor.execute(*rule_group.join(model,
condition=rule_group.model == model.id
).join(rule_group_group, 'LEFT',
condition=rule_group_group.rule_group == rule_group.id
).select(rule_group.id,
where=(model.model.in_(model_names))
& ~rule_group.id.in_(rule_table.select(rule_table.rule_group))
& rule_group.id.in_(rule_group_group.join(user_group,
condition=rule_group_group.group == user_group.group
).select(rule_group_group.rule_group,
where=user_group.user == user_id))))
& rule_group_group.group.in_(groups or [-1])))
no_rules = cursor.fetchone()
clause = defaultdict(lambda: ['OR'])
+1
View File
@@ -33,6 +33,7 @@ def register():
ir.ModelButtonClick,
ir.RuleGroup,
ir.RuleGroupGroup,
ir.Rule,
ir.SequenceType,
ir.SequenceTypeGroup,
ir.Export,
+19 -51
View File
@@ -5,6 +5,7 @@ from trytond.cache import Cache
from trytond.model import DeactivableMixin, ModelSQL, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction
class UIMenu(metaclass=PoolMeta):
@@ -22,25 +23,6 @@ class UIMenuGroup(ModelSQL):
group = fields.Many2One('res.group', 'Group', ondelete='CASCADE',
required=True)
@classmethod
def create(cls, vlist):
res = super(UIMenuGroup, cls).create(vlist)
# Restart the cache on the domain_get method
Pool().get('ir.rule')._domain_get_cache.clear()
return res
@classmethod
def write(cls, records, values, *args):
super(UIMenuGroup, cls).write(records, values, *args)
# Restart the cache on the domain_get method
Pool().get('ir.rule')._domain_get_cache.clear()
@classmethod
def delete(cls, records):
super(UIMenuGroup, cls).delete(records)
# Restart the cache on the domain_get method
Pool().get('ir.rule')._domain_get_cache.clear()
class ActionGroup(ModelSQL):
"Action - Group"
@@ -58,8 +40,6 @@ class ActionGroup(ModelSQL):
if vals.get('action'):
vals['action'] = Action.get_action_id(vals['action'])
res = super(ActionGroup, cls).create(vlist)
# Restart the cache on the domain_get method
Pool().get('ir.rule')._domain_get_cache.clear()
return res
@classmethod
@@ -73,14 +53,6 @@ class ActionGroup(ModelSQL):
values['action'] = Action.get_action_id(values['action'])
args.extend((records, values))
super(ActionGroup, cls).write(*args)
# Restart the cache on the domain_get method
Pool().get('ir.rule')._domain_get_cache.clear()
@classmethod
def delete(cls, records):
super(ActionGroup, cls).delete(records)
# Restart the cache on the domain_get method
Pool().get('ir.rule')._domain_get_cache.clear()
class Action(metaclass=PoolMeta):
@@ -237,6 +209,24 @@ class RuleGroupGroup(ModelSQL):
required=True)
class Rule(metaclass=PoolMeta):
__name__ = 'ir.rule'
@classmethod
def _get_context(cls, model_name):
context = super()._get_context(model_name)
if model_name in {'res.user.warning', 'res.user.application'}:
context['user_id'] = Transaction().user
return context
@classmethod
def _get_cache_key(cls, model_name):
key = super()._get_cache_key(model_name)
if model_name in {'res.user.warning', 'res.user.application'}:
key = (*key, Transaction().user)
return key
class SequenceType(metaclass=PoolMeta):
__name__ = 'ir.sequence.type'
groups = fields.Many2Many('ir.sequence.type-res.group', 'sequence_type',
@@ -252,28 +242,6 @@ class SequenceTypeGroup(ModelSQL):
group = fields.Many2One('res.group', 'User Groups',
ondelete='CASCADE', required=True)
@classmethod
def delete(cls, records):
Rule = Pool().get('ir.rule')
super(SequenceTypeGroup, cls).delete(records)
# Restart the cache on the domain_get method of ir.rule
Rule._domain_get_cache.clear()
@classmethod
def create(cls, vlist):
Rule = Pool().get('ir.rule')
res = super(SequenceTypeGroup, cls).create(vlist)
# Restart the cache on the domain_get method of ir.rule
Rule._domain_get_cache.clear()
return res
@classmethod
def write(cls, records, values, *args):
Rule = Pool().get('ir.rule')
super(SequenceTypeGroup, cls).write(records, values, *args)
# Restart the cache on the domain_get method
Rule._domain_get_cache.clear()
class Export(metaclass=PoolMeta):
__name__ = 'ir.export'
-9
View File
@@ -414,9 +414,6 @@ class User(avatar_mixin(100, 'login'), DeactivableMixin, ModelSQL, ModelView):
if cls.__name__ in cache:
for user in all_users:
cache[cls.__name__].pop(user.id, None)
# Restart the cache for domain_get method
pool = Pool()
pool.get('ir.rule')._domain_get_cache.clear()
# Restart the cache
ModelView._fields_view_get_cache.clear()
@@ -1018,8 +1015,6 @@ class UserGroup(ModelSQL):
def create(cls, vlist):
records = super().create(vlist)
pool = Pool()
# Restart the cache on the domain_get method
pool.get('ir.rule')._domain_get_cache.clear()
# Restart the cache for get_groups
pool.get('res.user')._get_groups_cache.clear()
# Restart the cache for view
@@ -1030,8 +1025,6 @@ class UserGroup(ModelSQL):
def write(cls, groups, values, *args):
super().write(groups, values, *args)
pool = Pool()
# Restart the cache on the domain_get method
pool.get('ir.rule')._domain_get_cache.clear()
# Restart the cache for get_groups
pool.get('res.user')._get_groups_cache.clear()
# Restart the cache for view
@@ -1041,8 +1034,6 @@ class UserGroup(ModelSQL):
def delete(cls, groups):
super().delete(groups)
pool = Pool()
# Restart the cache on the domain_get method
pool.get('ir.rule')._domain_get_cache.clear()
# Restart the cache for get_groups
pool.get('res.user')._get_groups_cache.clear()
# Restart the cache for view
+2 -5
View File
@@ -111,8 +111,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="global_p" eval="True"/>
</record>
<record model="ir.rule" id="rule_user_warning1">
<field name="domain"
eval="[('user', '=', Eval('user', {}).get('id', -1))]" pyson="1"/>
<field name="domain" eval="[('user', '=', Eval('user_id', -1))]" pyson="1"/>
<field name="rule_group" ref="rule_group_user_warning"/>
</record>
@@ -136,9 +135,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="default_p" eval="True"/>
</record>
<record model="ir.rule" id="rule_user_application1">
<field name="domain"
eval="[('user', '=', Eval('user', {}).get('id', -1))]"
pyson="1"/>
<field name="domain" eval="[('user', '=', Eval('user_id', -1))]" pyson="1"/>
<field name="rule_group" ref="rule_group_user_application"/>
</record>