policies/expression: add pb_log function to debug

This commit is contained in:
Jens Langhammer 2020-05-14 15:48:08 +02:00
parent 74ddf70cb7
commit 776ad3cfbf
1 changed files with 7 additions and 1 deletions

View File

@ -46,6 +46,11 @@ class Evaluator:
"""Check if `user` is member of group with name `group_name`""" """Check if `user` is member of group with name `group_name`"""
return user.groups.filter(name=group_name).exists() return user.groups.filter(name=group_name).exists()
@staticmethod
def jinja2_log(message, **kwargs):
"""Output debug log to console"""
return LOGGER.debug("Expression log", _m=message, **kwargs)
def _get_expression_context( def _get_expression_context(
self, request: PolicyRequest, **kwargs self, request: PolicyRequest, **kwargs
) -> Dict[str, Any]: ) -> Dict[str, Any]:
@ -53,6 +58,7 @@ class Evaluator:
# update passbook/policies/expression/templates/policy/expression/form.html # update passbook/policies/expression/templates/policy/expression/form.html
# update docs/policies/expression/index.md # update docs/policies/expression/index.md
kwargs["pb_is_group_member"] = Evaluator.jinja2_func_is_group_member kwargs["pb_is_group_member"] = Evaluator.jinja2_func_is_group_member
kwargs["pb_log"] = Evaluator.jinja2_log
kwargs["pb_logger"] = get_logger() kwargs["pb_logger"] = get_logger()
if request.http_request: if request.http_request:
kwargs["pb_is_sso_flow"] = request.http_request.session.get( kwargs["pb_is_sso_flow"] = request.http_request.session.get(
@ -88,7 +94,7 @@ class Evaluator:
if isinstance(result, (list, tuple)) and len(result) == 2: if isinstance(result, (list, tuple)) and len(result) == 2:
return PolicyResult(*result) return PolicyResult(*result)
if result: if result:
return PolicyResult(result) return PolicyResult(bool(result))
return PolicyResult(False) return PolicyResult(False)
except UndefinedError as exc: except UndefinedError as exc:
return PolicyResult(False, str(exc)) return PolicyResult(False, str(exc))