diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index c298d4056c..8b546b4014 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -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 diff --git a/trytond/trytond/ir/rule.py b/trytond/trytond/ir/rule.py index c89f1ed9a6..6e0f420c85 100644 --- a/trytond/trytond/ir/rule.py +++ b/trytond/trytond/ir/rule.py @@ -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']) diff --git a/trytond/trytond/res/__init__.py b/trytond/trytond/res/__init__.py index b3bc6a8a7c..8bf3da1a56 100644 --- a/trytond/trytond/res/__init__.py +++ b/trytond/trytond/res/__init__.py @@ -33,6 +33,7 @@ def register(): ir.ModelButtonClick, ir.RuleGroup, ir.RuleGroupGroup, + ir.Rule, ir.SequenceType, ir.SequenceTypeGroup, ir.Export, diff --git a/trytond/trytond/res/ir.py b/trytond/trytond/res/ir.py index 2b9965ae92..eed0e70e64 100644 --- a/trytond/trytond/res/ir.py +++ b/trytond/trytond/res/ir.py @@ -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' diff --git a/trytond/trytond/res/user.py b/trytond/trytond/res/user.py index e30ad3de46..616766845a 100644 --- a/trytond/trytond/res/user.py +++ b/trytond/trytond/res/user.py @@ -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 diff --git a/trytond/trytond/res/user.xml b/trytond/trytond/res/user.xml index eeb3f76504..d0ce76d9d9 100644 --- a/trytond/trytond/res/user.xml +++ b/trytond/trytond/res/user.xml @@ -111,8 +111,7 @@ this repository contains the full copyright notices and license terms. --> - + @@ -136,9 +135,7 @@ this repository contains the full copyright notices and license terms. --> - +