[routing] gives "keys" of the Acl row to the endpoint as kwargs

This commit is contained in:
Maxime Alves LIRMM 2020-09-18 11:47:43 +02:00
parent e8ed06a9b6
commit bcc4a3e9d8
2 changed files with 8 additions and 7 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
__version__ = '0.1.0' __version__ = '0.2.0'
def version(): def version():
return f'HalfAPI version:{__version__}' return f'HalfAPI version:{__version__}'

View File

@ -2,6 +2,7 @@
from functools import wraps from functools import wraps
import importlib import importlib
import sys import sys
from typing import Callable, List, Tuple
from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT, from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT,
PRODUCTION, DOMAINS) PRODUCTION, DOMAINS)
@ -33,13 +34,13 @@ def get_routes(domains=None):
""" """
def route_decorator(fct, acls_mod, acls): def route_decorator(fct: Callable, acls_mod, acls: List[Tuple]):
@wraps(fct) @wraps(fct)
async def caller(req: Request, *args, **kwargs): async def caller(req: Request, *args, **kwargs):
for acl_fct_name in acls: for acl_fct_name, keys in acls:
acl_fct = getattr(acls_mod, acl_fct_name) acl_fct = getattr(acls_mod, acl_fct_name)
if acl_fct(req, *args, **kwargs): if acl_fct(req, *args, **kwargs):
return await fct(req, *args, **kwargs) return await fct(req, *args, **{ **kwargs, **{'keys': keys} })
raise HTTPException(401) raise HTTPException(401)
@ -64,8 +65,8 @@ def get_routes(domains=None):
router=router['name']) as routes: router=router['name']) as routes:
for route in routes.select(): for route in routes.select():
fct_name = route.pop('fct_name') fct_name = route.pop('fct_name')
acls = [ list(elt.values()).pop() acls = [ (elt['acl_fct_name'], elt['keys'])
for elt in Acl(**route).select('acl_fct_name') ] for elt in Acl(**route).select('acl_fct_name', 'keys') ]
router_routes.append( router_routes.append(
Route(route['path'], Route(route['path'],
@ -81,4 +82,4 @@ def get_routes(domains=None):
app_routes.append(Mount('/{name}'.format(**domain), app_routes.append(Mount('/{name}'.format(**domain),
routes=domain_routes)) routes=domain_routes))
return app_routes return app_routes