diff --git a/halfapi/app.py b/halfapi/app.py index f05c197..3049b84 100644 --- a/halfapi/app.py +++ b/halfapi/app.py @@ -16,8 +16,8 @@ from halfapi.conf import HOST, PORT, DB_NAME, SECRET, PRODUCTION, DOMAINS, DOMAI from halfapi.lib.jwt_middleware import JWTAuthenticationBackend from halfapi.lib.responses import * -from halfapi.lib.routes import gen_starlette_routes -from halfapi.lib.schemas import get_api_routes, schema_json +from halfapi.lib.routes import gen_starlette_routes, api_routes +from halfapi.lib.schemas import get_api_routes, schema_json, get_acls """ @@ -34,7 +34,8 @@ if not PRODUCTION: ORJSONResponse({'user':request.user.json}) if type(request.user) != UnauthenticatedUser else ORJSONResponse({'user': None})), - Route('/halfapi/schema', schema_json) + Route('/halfapi/schema', schema_json), + Route('/halfapi/acls', get_acls) ] for domain, m_domain in DOMAINSDICT.items(): @@ -42,6 +43,12 @@ for domain, m_domain in DOMAINSDICT.items(): routes.append(route) +d_api = {} +d_acl = {} +for domain, m_domain in DOMAINSDICT.items(): + d_api[domain], d_acl[domain] = api_routes(m_domain) + + application = Starlette( debug=not PRODUCTION, routes=routes, diff --git a/halfapi/lib/domain.py b/halfapi/lib/domain.py index f24893b..4d1de4b 100644 --- a/halfapi/lib/domain.py +++ b/halfapi/lib/domain.py @@ -107,7 +107,6 @@ def gen_router_routes(m_router, path=None): - def gen_domain_routes(domain): m_router = importlib.import_module('.routers', domain) diff --git a/halfapi/lib/routes.py b/halfapi/lib/routes.py index f6ca052..15dc790 100644 --- a/halfapi/lib/routes.py +++ b/halfapi/lib/routes.py @@ -101,18 +101,22 @@ def api_routes(m_dom: ModuleType) -> Generator: """ m_dom_acl = importlib.import_module('.acl', m_dom.__name__) - - def pop_acl(r): - if 'acl' in r.keys(): - r.pop('acl') - return r + d_acls = {} def str_acl(params): l_params = [] + access = None + for param in params: if 'acl' not in param.keys(): continue - l_params.append({'acl': param['acl'].__name__}) + + l_params.append(param.copy()) + l_params[-1]['acl'] = param['acl'].__name__ + + if param['acl'] not in d_acls.keys(): + d_acls[param['acl'].__name__] = param['acl'] + return l_params d_res = {} @@ -124,4 +128,16 @@ def api_routes(m_dom: ModuleType) -> Generator: continue d_res[path][verb] = str_acl(d_route[verb]['params']) - return d_res + return d_res, d_acls + + +def api_acls(request): + from .. import app + res = {} + 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) + + return res diff --git a/halfapi/lib/schemas.py b/halfapi/lib/schemas.py index 4f158f6..0fc03ae 100644 --- a/halfapi/lib/schemas.py +++ b/halfapi/lib/schemas.py @@ -1,8 +1,9 @@ from types import ModuleType from typing import Dict from ..conf import DOMAINSDICT -from .routes import gen_starlette_routes, api_routes +from .routes import gen_starlette_routes from .responses import * +from .jwt_middleware import UnauthenticatedUser, JWTUser from starlette.schemas import SchemaGenerator from starlette.routing import Router SCHEMAS = SchemaGenerator( @@ -38,11 +39,9 @@ async def get_api_routes(request, *args, **kwargs): description: Returns the current API routes description (HalfAPI 0.2.1) as a JSON object """ - #TODO: LADOC - d_api = {} - for domain, m_domain in DOMAINSDICT.items(): - d_api[domain] = api_routes(m_domain) - return ORJSONResponse(d_api) + from .. import app + + return ORJSONResponse(app.d_api) async def schema_json(request, *args, **kwargs): @@ -73,3 +72,16 @@ def schema_dict_dom(m_domain: ModuleType) -> Dict: routes = [ elt for elt in gen_starlette_routes(m_domain) ] return SCHEMAS.get_schema(routes=routes) + + +async def get_acls(request, *args, **kwargs): + """ + responses: + 200: + description: A dictionnary of the domains and their acls, with the + result of the acls functions + """ + + from .routes import api_acls + return ORJSONResponse(api_acls(request)) +