auth.Auth.User + .Group: Derive from AuthEntity
User and Group share now enough properties to tie them closer together. The number of these properties will likely grow: email-address, nested groups and others. This commit introduces AuthEntity as a common base class to make that happen. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
7595bfc21e
commit
d73d921885
3 changed files with 52 additions and 54 deletions
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import abc
|
||||
|
||||
from enum import Enum, Flag, auto
|
||||
from typing import TYPE_CHECKING, Optional, Self, Union
|
||||
from typing import TYPE_CHECKING, Self, Sequence
|
||||
|
||||
from ..log import ERR
|
||||
from ..misc import load_object
|
||||
|
|
@ -22,27 +22,11 @@ class ProjectFlags(Flag): # export
|
|||
Contributing = auto()
|
||||
Active = auto()
|
||||
|
||||
class Group: # export
|
||||
|
||||
def __repr__(self):
|
||||
return f'Group({self.name})'
|
||||
class AuthEntity(abc.ABC): # export
|
||||
|
||||
@abc.abstractmethod
|
||||
def _name(self) -> str:
|
||||
pass
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name()
|
||||
|
||||
class User: # export
|
||||
|
||||
def __repr__(self):
|
||||
return f'User({self.name})'
|
||||
|
||||
@abc.abstractmethod
|
||||
def _name(self) -> str:
|
||||
raise NotImplementedError
|
||||
...
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
|
|
@ -55,22 +39,35 @@ class User: # export
|
|||
def display_name(self) -> str:
|
||||
return self._display_name()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _groups(self) -> list[Group]:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def groups(self) -> list[Group]:
|
||||
return self._groups()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _email(self) -> str:
|
||||
raise NotImplementedError
|
||||
return 'no-email@example.com'
|
||||
|
||||
@property
|
||||
def email(self) -> str:
|
||||
return self._email()
|
||||
|
||||
class Group(AuthEntity): # export
|
||||
|
||||
def __repr__(self):
|
||||
return f'Group({self.name})'
|
||||
|
||||
class User(AuthEntity): # export
|
||||
|
||||
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
|
||||
|
|
@ -86,7 +83,7 @@ class Auth(abc.ABC): # export
|
|||
|
||||
def __init__(self, conf: Config):
|
||||
self.__conf = conf
|
||||
self.__base_user_by_email: Optional[dict[str, User]] = None
|
||||
self.__base_user_by_email: dict[str, User] | None = None
|
||||
|
||||
@property
|
||||
def conf(self):
|
||||
|
|
@ -94,21 +91,21 @@ class Auth(abc.ABC): # export
|
|||
|
||||
@abc.abstractmethod
|
||||
def _access(
|
||||
self, what: str, access_type: Optional[Access], who: User | Group | None
|
||||
self, what: str, access_type: Access | None, who: User | Group | None
|
||||
) -> bool:
|
||||
raise NotImplementedError
|
||||
...
|
||||
|
||||
def access(
|
||||
self,
|
||||
what: str,
|
||||
access_type: Optional[Access] = None,
|
||||
who: Optional[Union[User, Group]] = None
|
||||
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:
|
||||
raise NotImplementedError
|
||||
...
|
||||
|
||||
@property
|
||||
def current_user(self) -> User:
|
||||
|
|
@ -116,14 +113,14 @@ class Auth(abc.ABC): # export
|
|||
|
||||
@abc.abstractmethod
|
||||
def _user(self, name) -> User:
|
||||
raise NotImplementedError
|
||||
...
|
||||
|
||||
def user(self, name) -> User:
|
||||
return self._user(name)
|
||||
|
||||
@abc.abstractmethod
|
||||
def _users(self) -> dict[str, User]:
|
||||
raise NotImplementedError
|
||||
...
|
||||
|
||||
def _user_by_email(self, email: str) -> User:
|
||||
if self.__base_user_by_email is None:
|
||||
|
|
@ -142,8 +139,8 @@ class Auth(abc.ABC): # export
|
|||
return self._users()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
raise NotImplementedError
|
||||
def _projects(self, name, flags: ProjectFlags) -> Sequence[str]:
|
||||
...
|
||||
|
||||
def projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
def projects(self, name, flags: ProjectFlags) -> Sequence[str]:
|
||||
return self._projects(name, flags)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...log import WARNING, slog
|
||||
from ..Auth import Access
|
||||
|
|
@ -10,6 +10,8 @@ from ..Auth import ProjectFlags
|
|||
from ..Auth import User as UserBase
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Sequence
|
||||
|
||||
from ...Config import Config
|
||||
|
||||
class Group(GroupBase): # export
|
||||
|
|
@ -27,7 +29,7 @@ class User(UserBase): # export
|
|||
self.__name = name
|
||||
self.__conf = conf
|
||||
self.__auth = auth
|
||||
self.__groups: Optional[list[GroupBase]] = None
|
||||
self.__groups: list[GroupBase] | None = None
|
||||
self.__email: str = conf['email']
|
||||
|
||||
@property
|
||||
|
|
@ -37,7 +39,7 @@ class User(UserBase): # export
|
|||
def _name(self) -> str:
|
||||
return self.__name
|
||||
|
||||
def _groups(self) -> list[GroupBase]:
|
||||
def _groups(self) -> Sequence[GroupBase]:
|
||||
if self.__groups is None:
|
||||
name: str = ''
|
||||
ret: list[GroupBase] = []
|
||||
|
|
@ -53,10 +55,10 @@ class Auth(AuthBase): # export
|
|||
|
||||
def __init__(self, conf: Config):
|
||||
super().__init__(conf)
|
||||
self.___users: Optional[dict[str, UserBase]] = None
|
||||
self.___users: dict[str, UserBase] | None = None
|
||||
self.__groups = None
|
||||
self.__current_user: UserBase | None = None
|
||||
self.__user_by_email: Optional[dict[str, UserBase]] = None
|
||||
self.__user_by_email: dict[str, UserBase] | None = None
|
||||
|
||||
@property
|
||||
def __users(self) -> dict[str, UserBase]:
|
||||
|
|
@ -97,5 +99,5 @@ class Auth(AuthBase): # export
|
|||
self.__user_by_email = ret
|
||||
return self.__user_by_email[email]
|
||||
|
||||
def _projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
def _projects(self, name, flags: ProjectFlags) -> Sequence[str]:
|
||||
return []
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import ldap # type: ignore[import-untyped]
|
||||
|
||||
|
|
@ -13,6 +13,8 @@ from ..Auth import ProjectFlags
|
|||
from ..Auth import User as UserBase
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Sequence
|
||||
|
||||
from ...Config import Config
|
||||
|
||||
class Group(GroupBase): # export
|
||||
|
|
@ -32,12 +34,12 @@ class User(UserBase):
|
|||
self.__name = name
|
||||
self.__cn = cn
|
||||
self.__email = email
|
||||
self.__groups: Optional[list[GroupBase]] = None
|
||||
self.__groups: list[GroupBase] | None = None
|
||||
|
||||
def _name(self) -> str:
|
||||
return self.__name
|
||||
|
||||
def _groups(self) -> list[GroupBase]:
|
||||
def _groups(self) -> Sequence[GroupBase]:
|
||||
raise NotImplementedError
|
||||
|
||||
def _email(self) -> str:
|
||||
|
|
@ -50,8 +52,8 @@ class Auth(AuthBase): # export
|
|||
|
||||
def __init__(self, conf: Config):
|
||||
super().__init__(conf)
|
||||
self.___users: Optional[dict[str, UserBase]] = None
|
||||
self.___user_by_email: Optional[dict[str, User]] = None
|
||||
self.___users: dict[str, UserBase] | None = None
|
||||
self.___user_by_email: dict[str, User] | None = None
|
||||
self.__groups = None
|
||||
self.__current_user: User | None = None
|
||||
self.__user_base_dn = conf['user_base_dn']
|
||||
|
|
@ -108,10 +110,7 @@ class Auth(AuthBase): # export
|
|||
return self.___user_by_email # type: ignore # We are sure that ___user_by_email is not None at this point
|
||||
|
||||
def _access(
|
||||
self,
|
||||
what: str,
|
||||
access_type: Optional[Access],
|
||||
who: UserBase | GroupBase | None
|
||||
self, what: str, access_type: Access | None, who: UserBase | GroupBase | None
|
||||
) -> bool: # type: ignore
|
||||
slog(
|
||||
WARNING,
|
||||
|
|
@ -135,7 +134,7 @@ class Auth(AuthBase): # export
|
|||
def _users(self) -> dict[str, UserBase]:
|
||||
return self.__users
|
||||
|
||||
def _projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
def _projects(self, name, flags: ProjectFlags) -> Sequence[str]:
|
||||
if flags & ProjectFlags.Contributing:
|
||||
# TODO: Ask LDAP
|
||||
slog(
|
||||
|
|
|
|||
Loading…
Reference in a new issue