From d4627a2f3a879766b1f26c86a4220676172d3526 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 30 Jun 2020 15:32:00 +0100 Subject: [PATCH] Add service_has_permission decorator Similar to how we can restrict routes to only users with a given permission, this new decorator lets us restrict routes to services that have a given permission. Having this is a decorator is cleaner than putting if statements inside the endpoint code. --- app/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/utils.py b/app/utils.py index 01f85de3b..0c23d43a9 100644 --- a/app/utils.py +++ b/app/utils.py @@ -82,6 +82,18 @@ def user_has_permissions(*permissions, **permission_kwargs): return wrap +def service_has_permission(permission): + from app import current_service + def wrap(func): + @wraps(func) + def wrap_func(*args, **kwargs): + if not current_service or not current_service.has_permission(permission): + abort(403) + return func(*args, **kwargs) + return wrap_func + return wrap + + def user_is_gov_user(f): @wraps(f) def wrapped(*args, **kwargs):