Issue Fetching Custom Context Field in Record Rule Domain Evaluation (Tryton 7.2.9)

I’m working on Tryton 7.2.9 and have a Record Rule whose domain uses:

<field name="domain" pyson="1" eval="
  [
    ('employee_delegate', 'in', [Get(Eval('context', {}), 'egob_employee', -1)]),
    ('state', '=', 'in_progress'),
  ]
"/>

in my res.user extension I add:

@classmethod
def __setup__(cls):
    super().__setup__()
    cls._context_fields.insert(0, "egob_employee")

When I debug, however, Get(Eval('context', {}), 'egob_employee', -1) always evaluates to -1, even though egob_employee is correctly set in the context. As a result, no records ever match.

You could try this:

class User(metaclass=PoolMeta):
    __name__ = 'res.user'

    egob_employee = fields.Boolean("Egob Employee")

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls._preferences_fields.append('egob_employee')

    @classmethod
    def _get_preferences(cls, user, context_only=False):
        preferences = super()._get_preferences(user, context_only=context_only)
        if user.egob_employee:
            preferences['egob_employee'] = user.egob_employee
        return preferences

You can not base record rule on the context, it would be too easy to by pass as the caller can forge the context.

If you want to write record rules based on specific value, you must extend _get_context method of ir.rule to set the content you want to use. You also need to update the cache keys from _get_cache_key to be sure that different contexts have different cache key.

You may have a look at modules/timesheet/ir.py to see how employees is added to the rules of some models.