policies/expression: migrate to raw python instead of jinja2 (#49)

* policies/expression: migrate to raw python instead of jinja2

* lib/expression: create base evaluator, custom subclass for policies

* core: rewrite propertymappings to use python

* providers/saml: update to new PropertyMappings

* sources/ldap: update to new PropertyMappings

* docs: update docs for new propertymappings

* root: remove jinja2

* root: re-add jinja to lock file as its implicitly required
This commit is contained in:
Jens L
2020-06-05 12:00:27 +02:00
committed by GitHub
parent 147212a5f9
commit 73116b9d1a
28 changed files with 322 additions and 190 deletions

View File

@ -2,13 +2,13 @@
from django.db import models
from django.utils.translation import gettext as _
from passbook.policies.expression.evaluator import Evaluator
from passbook.policies.expression.evaluator import PolicyEvaluator
from passbook.policies.models import Policy
from passbook.policies.types import PolicyRequest, PolicyResult
class ExpressionPolicy(Policy):
"""Jinja2-based Expression policy that allows Admins to write their own logic"""
"""Implement custom logic using python."""
expression = models.TextField()
@ -16,12 +16,12 @@ class ExpressionPolicy(Policy):
def passes(self, request: PolicyRequest) -> PolicyResult:
"""Evaluate and render expression. Returns PolicyResult(false) on error."""
evaluator = Evaluator()
evaluator = PolicyEvaluator(self.name)
evaluator.set_policy_request(request)
return evaluator.evaluate(self.expression)
def save(self, *args, **kwargs):
Evaluator().validate(self.expression)
PolicyEvaluator(self.name).validate(self.expression)
return super().save(*args, **kwargs)
class Meta: