[halfdomain] add ability to specify a domain's acl module path in __acl__ attribute

This commit is contained in:
Maxime Alves LIRMM@home 2022-05-17 16:01:58 +02:00
parent 739ffc9afa
commit e70239433f
5 changed files with 21 additions and 7 deletions

View File

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

View File

@ -27,7 +27,7 @@ from .lib.domain_middleware import DomainMiddleware
from .logging import logger
class HalfDomain(Starlette):
def __init__(self, domain, router=None, app=None):
def __init__(self, domain, router=None, acl=None, app=None):
"""
Parameters:
domain (str): Module name (should be importable)
@ -53,7 +53,7 @@ class HalfDomain(Starlette):
self.m_router = importlib.import_module(self.router, domain)
self.m_acl = importlib.import_module(f'{domain}.acl')
self.m_acl = HalfDomain.m_acl(domain, acl)
self.config = { **app.config }
@ -85,11 +85,21 @@ class HalfDomain(Starlette):
]
)
@staticmethod
def m_acl(domain, acl=None):
""" Returns the imported acl module for the domain
"""
if (not acl):
acl = getattr('__acl__', domain, '.acl')
return importlib.import_module(acl, domain)
@staticmethod
def acls(domain):
""" Returns the ACLS constant for the given domain
"""
m_acl = importlib.import_module(f'{domain}.acl')
m_acl = HalfDomain.m_acl(domain)
try:
return getattr(m_acl, 'ACLS')
except AttributeError:
@ -98,7 +108,8 @@ class HalfDomain(Starlette):
@staticmethod
def acls_route(domain):
d_res = {}
m_acl = importlib.import_module(f'{domain}.acl')
m_acl = HalfDomain.m_acl(domain)
for acl_name, doc, order in HalfDomain.acls(domain):
fct = getattr(m_acl, acl_name)
d_res[acl_name] = {

View File

@ -122,7 +122,7 @@ class HalfAPI(Starlette):
domain_key = domain.get('name', key)
self.add_domain(domain_key, domain.get('router'), path)
self.add_domain(domain_key, domain.get('router'), domain.get('acl'), path)
schemas.append(self.__domains[domain_key].schema())
@ -237,13 +237,14 @@ class HalfAPI(Starlette):
def domains(self):
return self.__domains
def add_domain(self, name, router=None, path='/', config=None):
def add_domain(self, name, router=None, acl=None, path='/', config=None):
if config:
self.config['domain'][name] = config
self.__domains[name] = HalfDomain(
name,
router,
acl,
self
)

View File

@ -52,6 +52,7 @@ class TestDomain(TestCase):
self.halfapi_conf['domain'][self.DOMAIN] = {
'name': self.DOMAIN,
'router': self.ROUTERS,
'acl': self.ACL,
'prefix': False,
'enabled': True,
'config': {

View File

@ -6,6 +6,7 @@ class TestDummyDomain(TestDomain):
DOMAIN = __name__
ROUTERS = __routers__
ACL = '.acl'
def test_domain(self):
self.check_domain()