[acls] Allows decorators to be used as acls (fct=acl.public).

This commit is contained in:
Maxime Alves LIRMM 2020-09-30 10:12:24 +02:00 committed by Joël Maïzi
parent 79210e503e
commit b89e03746f
2 changed files with 12 additions and 9 deletions

View File

@ -5,20 +5,21 @@ from starlette.authentication import UnauthenticatedUser
""" Base ACL module that contains generic functions for domains ACL
"""
def connected(func):
def public(*args, **kwargs) -> bool:
"Unlimited access"
return True
def connected(fct=public):
""" Decorator that checks if the user object of the request has been set
"""
@wraps(func)
@wraps(fct)
def caller(req, *args, **kwargs):
if (not hasattr(req, 'user')
or type(req.user) == UnauthenticatedUser
or not hasattr(req.user, 'is_authenticated')):
return False
return func(req, **{**kwargs, **req.path_params})
return fct(req, **{**kwargs, **req.path_params})
return caller
def public(*args, **kwargs) -> bool:
"Unlimited access"
return True

View File

@ -3,7 +3,7 @@ from functools import wraps
import importlib
import sys
from typing import Callable, List, Tuple, Dict, Generator
from types import ModuleType
from types import ModuleType, FunctionType
from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT,
PRODUCTION, DOMAINS)
@ -137,7 +137,9 @@ def api_acls(request):
for domain in app.d_acl.keys():
res[domain] = {}
for acl_name, fct in app.d_acl[domain].items():
print( fct(request) )
res[domain][acl_name] = fct(request)
ok = fct(request)
if isinstance(ok, FunctionType):
ok = fct()(request)
res[domain][acl_name] = ok
return res