Fix fallout of mypy --enable-error-code=ignore-without-code check. The multi_key_dict class had several type issues: the keys() and values() methods could implicitly return None when a key type was specified but not found, pyright flagged the conditional items() redefinition, and six test call sites carried bare "# type: ignore" comments. Add from __future__ import annotations, a _keys_from_dict() helper, and proper parameter/return type annotations to keys(), values(), and items(). Consolidate the conditional Python 2/3 items() into a single function with runtime dispatch. Remove all six bare type-ignores from multi_key_dict.py and annotate the remaining pre-existing ones in other files with specific error codes required by ignore-without-code. Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0 Signed-off-by: Jan Lindemann <jan@janware.com>
198 lines
6 KiB
Python
198 lines
6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, override
|
|
|
|
import ldap # type: ignore[import-untyped]
|
|
|
|
from ...ldap import bind
|
|
from ...log import DEBUG, ERR, WARNING, slog
|
|
from ..Auth import Access
|
|
from ..Auth import Auth as AuthBase
|
|
from ..Auth import Entity as EntityBase
|
|
from ..Auth import Group as Group # Needed by jwutils utility layer
|
|
from ..Auth import Kind, ProjectFlags
|
|
from ..Auth import User as User # Needed by jwutils utility layer
|
|
|
|
if TYPE_CHECKING:
|
|
from typing import Mapping, Sequence
|
|
|
|
from ...Config import Config
|
|
|
|
class Entity(EntityBase):
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
auth: AuthBase,
|
|
name: str,
|
|
cn: str,
|
|
email: str,
|
|
kind: Kind,
|
|
**kwargs,
|
|
):
|
|
super().__init__(**kwargs)
|
|
self.__auth = auth
|
|
self.__name = name
|
|
self.__cn = cn
|
|
self.__email = email
|
|
self.__groups: list[Entity] | None = None
|
|
self.__kind = kind
|
|
|
|
@override
|
|
def _name(self) -> str:
|
|
return self.__name
|
|
|
|
@override
|
|
def _groups(self) -> Sequence[Entity]:
|
|
raise NotImplementedError
|
|
|
|
@override
|
|
def _email(self) -> str:
|
|
return self.__email
|
|
|
|
@override
|
|
def _display_name(self) -> str:
|
|
return self.__cn
|
|
|
|
@override
|
|
def _projects(self, flags: ProjectFlags) -> Sequence[str]:
|
|
if flags & ProjectFlags.Contributing:
|
|
# TODO: Ask LDAP
|
|
slog(
|
|
WARNING,
|
|
'Querying LDAP for projects a user contributes to is not '
|
|
'implemented, ignoring'
|
|
)
|
|
return []
|
|
|
|
@override
|
|
def _kind(self) -> Kind:
|
|
return self.__kind
|
|
|
|
class Auth(AuthBase): # export
|
|
|
|
def __init__(self, conf: Config, *args, **kwargs):
|
|
super().__init__(conf, *args, **kwargs)
|
|
self.___users: dict[str, EntityBase] | None = None
|
|
self.___user_by_email: dict[str, EntityBase] | None = None
|
|
self.__groups = None
|
|
self.__current_user: Entity | None = None
|
|
self.__user_base_dn = conf['user_base_dn']
|
|
self.__conn = self.__bind()
|
|
self.__dummy = AuthBase.load(conf, tp = 'dummy')
|
|
|
|
def __bind(self):
|
|
return bind(self.conf)
|
|
|
|
@property
|
|
def __users(self) -> dict[str, EntityBase]:
|
|
if self.___users is None:
|
|
ret: dict[str, EntityBase] = {}
|
|
ret_by_email: dict[str, EntityBase] = {}
|
|
for res in self.__conn.find(
|
|
self.__user_base_dn,
|
|
ldap.SCOPE_SUBTREE, # pyright: ignore[reportAttributeAccessIssue]
|
|
"objectClass=inetOrgPerson",
|
|
('uid', 'cn', 'uidNumber', 'mail', 'maildrop')):
|
|
try:
|
|
if 'displayName' in res[1]:
|
|
cn = res[1]['displayName'][0].decode('utf-8')
|
|
else:
|
|
cn = res[1]['cn'][0].decode('utf-8')
|
|
uid = res[1]['uid'][0].decode('utf-8')
|
|
res[1]['uidNumber'][0].decode('utf-8')
|
|
emails = []
|
|
#for attr in ['mail', 'maildrop']:
|
|
for attr in ['mail']:
|
|
if attr in res[1]:
|
|
for entry in res[1][attr]:
|
|
emails.append(entry.decode('utf-8'))
|
|
if not emails:
|
|
slog(DEBUG, f'No email for user "{uid}", skipping')
|
|
continue
|
|
user = self._create_entity(
|
|
auth = self,
|
|
name = uid,
|
|
cn = cn,
|
|
email = emails[0],
|
|
kind = Kind.User,
|
|
)
|
|
ret[uid] = user
|
|
for email in emails:
|
|
ret_by_email[email] = user
|
|
except Exception as e:
|
|
slog(WARNING, f'Exception {e}')
|
|
raise
|
|
continue
|
|
for dummy_user in self.__dummy.users.values():
|
|
ret[dummy_user.name] = dummy_user
|
|
self.___users = ret
|
|
self.___user_by_email = ret_by_email
|
|
return self.___users
|
|
|
|
@property
|
|
def __user_by_email(self) -> dict[str, Entity]:
|
|
if self.___user_by_email is None:
|
|
self.__users
|
|
return self.___user_by_email # type: ignore[return-value] # We are sure that ___user_by_email is not None at this point
|
|
|
|
def _create_entity(
|
|
self,
|
|
*,
|
|
auth: Auth,
|
|
name: str,
|
|
cn: str,
|
|
email: str,
|
|
kind: Kind,
|
|
**kwargs,
|
|
):
|
|
return Entity(
|
|
auth = auth,
|
|
name = name,
|
|
cn = cn,
|
|
email = email,
|
|
kind = kind,
|
|
**kwargs,
|
|
)
|
|
|
|
@override
|
|
def _access(
|
|
self, what: str, access_type: Access | None, who: EntityBase | None
|
|
) -> bool: # type: ignore[return-value]
|
|
slog(
|
|
WARNING,
|
|
f'Returning False for {access_type} access to resource {what} by {who}'
|
|
)
|
|
return False
|
|
|
|
@override
|
|
def _entity(self, name: str, kind: Kind) -> EntityBase:
|
|
if kind != Kind.User:
|
|
raise NotImplementedError(
|
|
'Support for retrieving groups is not implemented'
|
|
)
|
|
try:
|
|
return self.__users[name]
|
|
except:
|
|
slog(ERR, f'No such user: "{name}"')
|
|
raise
|
|
|
|
@override
|
|
def _entity_by_email(self, email: str, kind: Kind) -> Entity:
|
|
if kind != Kind.User:
|
|
raise NotImplementedError(
|
|
'Support for retrieving groups by email is not implemented'
|
|
)
|
|
return self.__user_by_email[email]
|
|
|
|
@override
|
|
def _current_entity(self) -> Entity:
|
|
raise NotImplementedError
|
|
|
|
@override
|
|
def _entities(self, kind: Kind) -> Mapping[str, EntityBase]:
|
|
if kind != Kind.User:
|
|
raise NotImplementedError(
|
|
'Support for retrieving all groups is not implemented'
|
|
)
|
|
return self.__users
|