jwutils: Add Auth

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2025-02-14 14:29:30 +01:00
commit 9b1650b58f
4 changed files with 141 additions and 0 deletions

View file

@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
from typing import Optional, Union
from ... import log
from ... import Config
from .. import Access
from .. import Auth as AuthBase
from .. import Group as GroupBase
from .. import User as UserBase
class Group(GroupBase): # export
def __init__(self, auth: AuthBase, name: str):
self.__name = name
self.__auth = auth
def _name(self) -> str:
return self.__name
class User(UserBase):
def __init__(self, auth: AuthBase, name: str):
self.__name = name
self.__auth = auth
self.__groups = None
def _name(self) -> str:
return self.__name
def _groups(self) -> list[Group]:
if self.__groups is None:
for name in conf['user.' + name + '.groups']:
ret[name] = Group(self, name)
self.__groups = ret
self.__groups = ret
class Auth(AuthBase): # export
def __init__(self, conf: Config):
self.__conf = conf
self.__users = None
self.__groups = None
self.__current_user = None
def _user(self, name_) -> User:
if self.__users is None:
ret: dict[str, User] = {}
for name in self.conf.entries('user'):
ret[name] = User(self, name)
self.__users = ret
return self.__users[name_]
def _access(self, what: str, access_type: Optional[Access]=None, who: Optional[Union[User|Group]]=None) -> bool:
slog(log.WARNING, f'Returning False for {access_type} access to resource {what} by {who}')
return False
def _current_user(self) -> User:
if self.__current_user is None:
self.__current_user = self._user(self.conf['current_user'])
return self.__current_user

View file

@ -0,0 +1,4 @@
TOPDIR = ../../../../..
include $(TOPDIR)/make/proj.mk
include $(JWBDIR)/make/py-mod.mk