jw-python/src/python/jw/util/auth/Auth.py

155 lines
3.6 KiB
Python
Raw Normal View History

from __future__ import annotations
import abc
from enum import Enum, Flag, auto
from typing import TYPE_CHECKING, Self, Sequence
from ..log import ERR
from ..misc import load_object
if TYPE_CHECKING:
from ..Config import Config
class Access(Enum): # export
Read = auto()
Modify = auto()
Create = auto()
Delete = auto()
class ProjectFlags(Flag): # export
NoFlags = auto()
Contributing = auto()
Active = auto()
class AuthEntity(abc.ABC): # export
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
@abc.abstractmethod
def _name(self) -> str:
...
@property
def name(self) -> str:
return self._name()
def _display_name(self) -> str:
return self._name()
@property
def display_name(self) -> str:
return self._display_name()
def _email(self) -> str:
return 'no-email@example.com'
@property
def email(self) -> str:
return self._email()
class Group(AuthEntity): # export
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
def __repr__(self):
return f'Group({self.name})'
class User(AuthEntity): # export
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
def __repr__(self):
return f'User({self.name})'
@abc.abstractmethod
def _groups(self) -> Sequence[Group]:
...
@property
def groups(self) -> Sequence[Group]:
return self._groups()
@abc.abstractmethod
def _email(self) -> str:
...
class Auth(abc.ABC): # export
@classmethod
def load(cls, conf: Config, tp: str = '') -> Self:
if tp == '':
val = conf.get('type')
if val is None:
msg = 'No type specified in auth configuration'
conf.dump(ERR, msg)
raise Exception(msg)
tp = val
return load_object(f'jwutils.auth.{tp}.Auth', Auth, 'Auth', conf)
def __init__(self, conf: Config):
self.__conf = conf
self.__base_user_by_email: dict[str, User] | None = None
@property
def conf(self):
return self.__conf
@abc.abstractmethod
def _access(
self, what: str, access_type: Access | None, who: User | Group | None
) -> bool:
...
def access(
self,
what: str,
access_type: Access | None = None,
who: User | Group | None = None
) -> bool:
return self._access(what, access_type, who)
@abc.abstractmethod
def _current_user(self) -> User:
...
@property
def current_user(self) -> User:
return self._current_user()
@abc.abstractmethod
def _user(self, name) -> User:
...
def user(self, name) -> User:
return self._user(name)
@abc.abstractmethod
def _users(self) -> dict[str, User]:
...
def _user_by_email(self, email: str) -> User:
if self.__base_user_by_email is None:
ret: dict[str, User] = dict()
users = self._users()
for user in users.values():
ret[user.email] = user
self.__base_user_by_email = ret
return self.__base_user_by_email[email]
def user_by_email(self, email) -> User:
return self._user_by_email(email)
@property
def users(self) -> dict[str, User]:
return self._users()
@abc.abstractmethod
def _projects(self, name, flags: ProjectFlags) -> Sequence[str]:
...
def projects(self, name, flags: ProjectFlags) -> Sequence[str]:
return self._projects(name, flags)