[routing] gives "keys" of the Acl row to the endpoint as kwargs
This commit is contained in:
parent
e8ed06a9b6
commit
bcc4a3e9d8
|
@ -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__}'
|
||||||
|
|
|
@ -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'],
|
||||||
|
|
Loading…
Reference in New Issue