zih-ldap 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zih_ldap-0.1.0/LICENSE +7 -0
- zih_ldap-0.1.0/PKG-INFO +19 -0
- zih_ldap-0.1.0/README.md +3 -0
- zih_ldap-0.1.0/pyproject.toml +46 -0
- zih_ldap-0.1.0/zih_ldap/__init__.py +0 -0
- zih_ldap-0.1.0/zih_ldap/conftest.py +35 -0
- zih_ldap-0.1.0/zih_ldap/connection.py +141 -0
- zih_ldap-0.1.0/zih_ldap/models.py +665 -0
- zih_ldap-0.1.0/zih_ldap/pydantic_validators.py +41 -0
- zih_ldap-0.1.0/zih_ldap/test_models.py +172 -0
zih_ldap-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2025, TUD Dresden University of Technology
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the " Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
zih_ldap-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: zih-ldap
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Wrapper integrating LDAP and Pydantic for TU Dresden
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Ansgar Burchardt
|
|
7
|
+
Author-email: ansgar.burchardt@tu-dresden.de
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Dist: ldap3 (>=2.9.1,<3.0.0)
|
|
12
|
+
Requires-Dist: pydantic (>=2.11.5,<3.0.0)
|
|
13
|
+
Project-URL: Repository, https://gitlab.hrz.tu-chemnitz.de/zih/sde/zih-ldap
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# zih_ldap
|
|
17
|
+
|
|
18
|
+
Wrapper integrating LDAP and Pydantic for TU Dresden
|
|
19
|
+
|
zih_ldap-0.1.0/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "zih-ldap"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Wrapper integrating LDAP and Pydantic for TU Dresden"
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Ansgar Burchardt", email = "ansgar.burchardt@tu-dresden.de"},
|
|
7
|
+
{name = "Julius Metz", email = "julius.metz@tu-dresden.de"},
|
|
8
|
+
]
|
|
9
|
+
license = {text = "MIT"}
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Operating System :: OS Independent",
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"ldap3 (>=2.9.1,<3.0.0)",
|
|
18
|
+
"pydantic (>=2.11.5,<3.0.0)",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
repository = "https://gitlab.hrz.tu-chemnitz.de/zih/sde/zih-ldap"
|
|
23
|
+
|
|
24
|
+
[tool.poetry]
|
|
25
|
+
|
|
26
|
+
[tool.poetry.group.dev.dependencies]
|
|
27
|
+
mypy = "^1.16.0"
|
|
28
|
+
black = "^25.1.0"
|
|
29
|
+
types-ldap3 = "^2.9.13.20250516"
|
|
30
|
+
pytest = "^8.4.0"
|
|
31
|
+
pydantic-settings = "^2.9.1"
|
|
32
|
+
ruff = "^0.11.13"
|
|
33
|
+
|
|
34
|
+
[tool.black]
|
|
35
|
+
line-length = 79
|
|
36
|
+
|
|
37
|
+
[tool.mypy]
|
|
38
|
+
strict = true
|
|
39
|
+
packages = ["src"]
|
|
40
|
+
plugins = ["pydantic.mypy"]
|
|
41
|
+
check_untyped_defs = true
|
|
42
|
+
enable_error_code = "explicit-override"
|
|
43
|
+
|
|
44
|
+
[build-system]
|
|
45
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
46
|
+
build-backend = "poetry.core.masonry.api"
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""pytest session fixtures"""
|
|
2
|
+
|
|
3
|
+
import tomllib
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
from pydantic import SecretStr
|
|
7
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Settings(BaseSettings):
|
|
11
|
+
"""Settings for tests"""
|
|
12
|
+
|
|
13
|
+
ldap_address: str
|
|
14
|
+
ldap_user_bind_dn: str
|
|
15
|
+
ldap_password: SecretStr
|
|
16
|
+
ldap_port: int = 636
|
|
17
|
+
|
|
18
|
+
model_config = SettingsConfigDict(env_prefix="TEST_CONF_")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def pytest_addoption(parser: pytest.Parser) -> None:
|
|
22
|
+
|
|
23
|
+
parser.addoption("--config", action="store", default="./test_config.toml")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.fixture(scope="session")
|
|
27
|
+
def external_settings(
|
|
28
|
+
request: pytest.FixtureRequest,
|
|
29
|
+
) -> Settings:
|
|
30
|
+
config = request.config.getoption("config", default=None)
|
|
31
|
+
config_dict = {}
|
|
32
|
+
if config:
|
|
33
|
+
with open(request.config.getoption("config"), "rb") as fp:
|
|
34
|
+
config_dict = tomllib.load(fp)
|
|
35
|
+
return Settings.model_validate(config_dict)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""ldap connection"""
|
|
2
|
+
|
|
3
|
+
import ssl
|
|
4
|
+
from typing import Any, Callable, Literal, TypeVar
|
|
5
|
+
|
|
6
|
+
from ldap3 import SAFE_RESTARTABLE, SCHEMA, Connection, Server, Tls
|
|
7
|
+
from pydantic import BaseModel
|
|
8
|
+
|
|
9
|
+
ModelT = TypeVar("ModelT", bound=BaseModel)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def new_ldap_connection(
|
|
13
|
+
url: str, user_bind_dn: str, password: str, port: int = 636
|
|
14
|
+
) -> "LDAPConnection":
|
|
15
|
+
"""return new ldap3 connection"""
|
|
16
|
+
|
|
17
|
+
def connect() -> Connection:
|
|
18
|
+
tls_config = Tls(
|
|
19
|
+
validate=ssl.CERT_REQUIRED,
|
|
20
|
+
version=ssl.PROTOCOL_TLSv1_2,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
server = Server(
|
|
24
|
+
url,
|
|
25
|
+
port=port,
|
|
26
|
+
use_ssl=True,
|
|
27
|
+
tls=tls_config,
|
|
28
|
+
get_info=SCHEMA,
|
|
29
|
+
)
|
|
30
|
+
return Connection(
|
|
31
|
+
server,
|
|
32
|
+
user_bind_dn,
|
|
33
|
+
password,
|
|
34
|
+
receive_timeout=60,
|
|
35
|
+
auto_bind="TLS_BEFORE_BIND",
|
|
36
|
+
client_strategy=SAFE_RESTARTABLE,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
return LDAPConnection(connect)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class LDAPException(Exception):
|
|
43
|
+
"""ldap exception"""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class MultipleObjectsReturned(LDAPException):
|
|
47
|
+
"""one expected but multiple objects returned"""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ObjectDoesNotExist(LDAPException):
|
|
51
|
+
"""one expected but nothing found"""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class LDAPConnection:
|
|
55
|
+
"""ldap connection"""
|
|
56
|
+
|
|
57
|
+
def __init__(
|
|
58
|
+
self,
|
|
59
|
+
connect: Callable[[], Connection],
|
|
60
|
+
) -> None:
|
|
61
|
+
self._connect = connect
|
|
62
|
+
self._connection_singleton: Connection | None = None
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def _connection(self) -> Connection:
|
|
66
|
+
"""ldap3 connection"""
|
|
67
|
+
if self._connection_singleton is None:
|
|
68
|
+
self._connection_singleton = self._connect()
|
|
69
|
+
return self._connection_singleton
|
|
70
|
+
|
|
71
|
+
@staticmethod
|
|
72
|
+
def fix_result(entry: dict[str, Any]) -> dict[str, str | list[Any]]:
|
|
73
|
+
"""fix result"""
|
|
74
|
+
return {
|
|
75
|
+
key: value if isinstance(value, list) else [value]
|
|
76
|
+
for key, value in entry["attributes"].items()
|
|
77
|
+
} | {"dn": entry["dn"]}
|
|
78
|
+
|
|
79
|
+
def get(
|
|
80
|
+
self,
|
|
81
|
+
filter_term: str,
|
|
82
|
+
search_base: str,
|
|
83
|
+
attributes_model: type[ModelT],
|
|
84
|
+
search_scope: Literal["LEVEL", "SUBTREE"] = "LEVEL",
|
|
85
|
+
ignored_attributes: list[str] | None = None,
|
|
86
|
+
) -> ModelT:
|
|
87
|
+
"""Get one object from LDAP by filter term.
|
|
88
|
+
|
|
89
|
+
raise:
|
|
90
|
+
MultipleObjectsReturned - if more than one element found
|
|
91
|
+
ObjectDoesNotExist - if no element was found
|
|
92
|
+
"""
|
|
93
|
+
finds = self.filter(
|
|
94
|
+
filter_term,
|
|
95
|
+
search_base,
|
|
96
|
+
attributes_model,
|
|
97
|
+
search_scope,
|
|
98
|
+
ignored_attributes,
|
|
99
|
+
)
|
|
100
|
+
if len(finds) > 1:
|
|
101
|
+
raise MultipleObjectsReturned(
|
|
102
|
+
"Got multiple results from ldap, but only one was expected!"
|
|
103
|
+
)
|
|
104
|
+
if len(finds) == 0:
|
|
105
|
+
raise ObjectDoesNotExist(
|
|
106
|
+
"No object was found, but one was expected!"
|
|
107
|
+
)
|
|
108
|
+
return finds[0]
|
|
109
|
+
|
|
110
|
+
def filter(
|
|
111
|
+
self,
|
|
112
|
+
filter_term: str,
|
|
113
|
+
search_base: str,
|
|
114
|
+
attributes_model: type[ModelT],
|
|
115
|
+
search_scope: Literal["BASE", "LEVEL", "SUBTREE"] = "LEVEL",
|
|
116
|
+
ignored_attributes: list[str] | None = None,
|
|
117
|
+
) -> list[ModelT]:
|
|
118
|
+
"""query all elements for given python ldap3 search term"""
|
|
119
|
+
if ignored_attributes is None:
|
|
120
|
+
ignored_attributes = []
|
|
121
|
+
ignored_attributes.append("dn")
|
|
122
|
+
|
|
123
|
+
_, result, response, _ = self._connection.search(
|
|
124
|
+
search_base,
|
|
125
|
+
filter_term,
|
|
126
|
+
search_scope,
|
|
127
|
+
attributes=[
|
|
128
|
+
attr
|
|
129
|
+
for attr in attributes_model.model_json_schema(
|
|
130
|
+
mode="serialization"
|
|
131
|
+
)["properties"]
|
|
132
|
+
if attr not in ignored_attributes
|
|
133
|
+
],
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
if result["result"] not in (0, 4) or response is None:
|
|
137
|
+
raise LDAPException(f"LDAP search failed: {result['description']}")
|
|
138
|
+
return [
|
|
139
|
+
attributes_model.model_validate(self.fix_result(entry))
|
|
140
|
+
for entry in response
|
|
141
|
+
]
|
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
"""Type classes used by the ldap module"""
|
|
2
|
+
|
|
3
|
+
from datetime import date, datetime
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from typing import (
|
|
6
|
+
Annotated,
|
|
7
|
+
Callable,
|
|
8
|
+
ClassVar,
|
|
9
|
+
Generic,
|
|
10
|
+
Literal,
|
|
11
|
+
Type,
|
|
12
|
+
TypeVar,
|
|
13
|
+
cast,
|
|
14
|
+
overload,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
from ldap3 import SUBTREE
|
|
18
|
+
from ldap3.utils.conv import escape_filter_chars
|
|
19
|
+
from pydantic import AliasPath, BaseModel, BeforeValidator, Field
|
|
20
|
+
|
|
21
|
+
from .connection import LDAPConnection
|
|
22
|
+
from .pydantic_validators import (
|
|
23
|
+
active_to_bool,
|
|
24
|
+
group_dn_to_name,
|
|
25
|
+
string_to_date,
|
|
26
|
+
to_str,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class SearchBase(str, Enum):
|
|
31
|
+
"""ldap search base"""
|
|
32
|
+
|
|
33
|
+
USERS = "ou=users,dc=tu-dresden,dc=de"
|
|
34
|
+
STRUCTURES = "ou=structures,dc=tu-dresden,dc=de"
|
|
35
|
+
FACILITY = "ou=facilities,dc=tu-dresden,dc=de"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class LdapUserBase(BaseModel):
|
|
39
|
+
"""basic model for an LDAP user"""
|
|
40
|
+
|
|
41
|
+
uid: Annotated[
|
|
42
|
+
str,
|
|
43
|
+
Field(
|
|
44
|
+
validation_alias=AliasPath("uid", 0),
|
|
45
|
+
serialization_alias="uid",
|
|
46
|
+
),
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class LdapUserBulk(LdapUserBase):
|
|
51
|
+
"""model for the LDAP user search to get full names"""
|
|
52
|
+
|
|
53
|
+
last_name: Annotated[
|
|
54
|
+
str,
|
|
55
|
+
Field(
|
|
56
|
+
validation_alias=AliasPath("sn", 0),
|
|
57
|
+
serialization_alias="sn",
|
|
58
|
+
),
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
first_name: Annotated[
|
|
62
|
+
str | None,
|
|
63
|
+
Field(
|
|
64
|
+
validation_alias=AliasPath("givenName", 0),
|
|
65
|
+
serialization_alias="givenName",
|
|
66
|
+
),
|
|
67
|
+
] = None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class Gender(str, Enum):
|
|
71
|
+
"""gender of a User"""
|
|
72
|
+
|
|
73
|
+
UNKNOWN = "0"
|
|
74
|
+
MALE = "1"
|
|
75
|
+
FEMALE = "2"
|
|
76
|
+
DIVERSE = "3"
|
|
77
|
+
NOT_SPECIFIED = "9"
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class LdapUser(LdapUserBulk):
|
|
81
|
+
"""ldap user"""
|
|
82
|
+
|
|
83
|
+
cn: Annotated[
|
|
84
|
+
str,
|
|
85
|
+
Field(
|
|
86
|
+
validation_alias=AliasPath("cn", 0),
|
|
87
|
+
serialization_alias="cn",
|
|
88
|
+
),
|
|
89
|
+
]
|
|
90
|
+
title: Annotated[
|
|
91
|
+
str | None,
|
|
92
|
+
Field(
|
|
93
|
+
validation_alias=AliasPath("title", 0),
|
|
94
|
+
serialization_alias="title",
|
|
95
|
+
),
|
|
96
|
+
] = None
|
|
97
|
+
displayName: Annotated[
|
|
98
|
+
str,
|
|
99
|
+
Field(
|
|
100
|
+
validation_alias=AliasPath("displayName", 0),
|
|
101
|
+
serialization_alias="displayName",
|
|
102
|
+
),
|
|
103
|
+
]
|
|
104
|
+
zihGender: Annotated[
|
|
105
|
+
Gender | None,
|
|
106
|
+
BeforeValidator(to_str),
|
|
107
|
+
Field(
|
|
108
|
+
validation_alias=AliasPath("zihGender", 0),
|
|
109
|
+
serialization_alias="zihGender",
|
|
110
|
+
),
|
|
111
|
+
] = None
|
|
112
|
+
email: Annotated[
|
|
113
|
+
str | None,
|
|
114
|
+
Field(
|
|
115
|
+
validation_alias=AliasPath("mail", 0),
|
|
116
|
+
serialization_alias="mail",
|
|
117
|
+
),
|
|
118
|
+
] = None
|
|
119
|
+
edu_person_principal_name: Annotated[
|
|
120
|
+
str,
|
|
121
|
+
Field(
|
|
122
|
+
validation_alias=AliasPath("eduPersonPrincipalName", 0),
|
|
123
|
+
serialization_alias="eduPersonPrincipalName",
|
|
124
|
+
),
|
|
125
|
+
]
|
|
126
|
+
affiliations: Annotated[
|
|
127
|
+
list[str],
|
|
128
|
+
Field(alias="eduPersonScopedAffiliation"),
|
|
129
|
+
]
|
|
130
|
+
zihState: Annotated[
|
|
131
|
+
bool,
|
|
132
|
+
BeforeValidator(active_to_bool),
|
|
133
|
+
Field(
|
|
134
|
+
validation_alias=AliasPath("zihState", 0),
|
|
135
|
+
serialization_alias="zihState",
|
|
136
|
+
),
|
|
137
|
+
]
|
|
138
|
+
zihStateLastChanged: Annotated[
|
|
139
|
+
datetime,
|
|
140
|
+
Field(
|
|
141
|
+
validation_alias=AliasPath("zihStateLastChanged", 0),
|
|
142
|
+
serialization_alias="zihStateLastChanged",
|
|
143
|
+
),
|
|
144
|
+
]
|
|
145
|
+
eduPersonPrimaryOrgUnitDN: Annotated[
|
|
146
|
+
str | None,
|
|
147
|
+
Field(
|
|
148
|
+
validation_alias=AliasPath("eduPersonPrimaryOrgUnitDN", 0),
|
|
149
|
+
serialization_alias="eduPersonPrimaryOrgUnitDN",
|
|
150
|
+
),
|
|
151
|
+
] = None
|
|
152
|
+
eduPersonOrgUnitDN: list[str]
|
|
153
|
+
o: Annotated[
|
|
154
|
+
str,
|
|
155
|
+
Field(
|
|
156
|
+
validation_alias=AliasPath("o", 0),
|
|
157
|
+
serialization_alias="o",
|
|
158
|
+
),
|
|
159
|
+
]
|
|
160
|
+
ou: list[str]
|
|
161
|
+
groupmemberOf: list[str]
|
|
162
|
+
idm_groups: Annotated[
|
|
163
|
+
list[str],
|
|
164
|
+
BeforeValidator(group_dn_to_name),
|
|
165
|
+
Field(alias="zihGroupmemberOf"),
|
|
166
|
+
]
|
|
167
|
+
proxyAddresses: list[str]
|
|
168
|
+
zihProxyAddresses: list[str]
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
ModelT = TypeVar("ModelT", bound=LdapUserBase)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class LdapUserManager:
|
|
175
|
+
"""ldap user manager"""
|
|
176
|
+
|
|
177
|
+
def __init__(self, connection: LDAPConnection) -> None:
|
|
178
|
+
self.connection = connection
|
|
179
|
+
|
|
180
|
+
# you can't use default parameters for a TypeVar, so we have to use this
|
|
181
|
+
# beautiful @overload construct found below
|
|
182
|
+
# https://github.com/python/mypy/issues/3737
|
|
183
|
+
# thank you mypy
|
|
184
|
+
# since 2017...
|
|
185
|
+
@overload
|
|
186
|
+
def get(self, uid: str, *, get_inactive: bool = False) -> LdapUser: ...
|
|
187
|
+
|
|
188
|
+
@overload
|
|
189
|
+
def get(
|
|
190
|
+
self,
|
|
191
|
+
uid: str,
|
|
192
|
+
attributes_model: type[ModelT],
|
|
193
|
+
*,
|
|
194
|
+
get_inactive: bool = False,
|
|
195
|
+
) -> ModelT: ...
|
|
196
|
+
|
|
197
|
+
def get( # type: ignore
|
|
198
|
+
self,
|
|
199
|
+
uid: str,
|
|
200
|
+
attributes_model: type[LdapUserBase] = LdapUser,
|
|
201
|
+
*,
|
|
202
|
+
get_inactive: bool = False,
|
|
203
|
+
):
|
|
204
|
+
"""get LdapUser via uid"""
|
|
205
|
+
escaped_uid = escape_filter_chars(uid)
|
|
206
|
+
state_filter = "" if get_inactive else "(zihState=active)"
|
|
207
|
+
filter_term = (
|
|
208
|
+
f"(&(objectClass=zihAccount){state_filter}(uid={escaped_uid}))"
|
|
209
|
+
)
|
|
210
|
+
ldap_user = self.connection.get(
|
|
211
|
+
filter_term, SearchBase.USERS.value, attributes_model
|
|
212
|
+
)
|
|
213
|
+
return ldap_user
|
|
214
|
+
|
|
215
|
+
@overload
|
|
216
|
+
def filter(
|
|
217
|
+
self, search_term: str, *, get_inactive: bool = False
|
|
218
|
+
) -> list[LdapUser]: ...
|
|
219
|
+
|
|
220
|
+
@overload
|
|
221
|
+
def filter(
|
|
222
|
+
self,
|
|
223
|
+
search_term: str,
|
|
224
|
+
attributes_model: type[ModelT],
|
|
225
|
+
*,
|
|
226
|
+
get_inactive: bool = False,
|
|
227
|
+
) -> list[ModelT]: ...
|
|
228
|
+
|
|
229
|
+
def filter( # type: ignore
|
|
230
|
+
self,
|
|
231
|
+
search_term,
|
|
232
|
+
attributes_model=LdapUser,
|
|
233
|
+
*,
|
|
234
|
+
get_inactive: bool = False,
|
|
235
|
+
):
|
|
236
|
+
"""get ldap users via python ldap3 search term"""
|
|
237
|
+
state_filter = "" if get_inactive else "(zihState=active)"
|
|
238
|
+
filter_term = f"(&(objectClass=zihAccount){state_filter}{search_term})"
|
|
239
|
+
return self.connection.filter(
|
|
240
|
+
filter_term, SearchBase.USERS.value, attributes_model
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
@overload
|
|
244
|
+
def get_bulk(
|
|
245
|
+
self, uids: set[str], *, get_inactive: bool = False
|
|
246
|
+
) -> dict[str, LdapUserBulk]: ...
|
|
247
|
+
|
|
248
|
+
@overload
|
|
249
|
+
def get_bulk(
|
|
250
|
+
self,
|
|
251
|
+
uids: set[str],
|
|
252
|
+
attributes_model: type[ModelT],
|
|
253
|
+
*,
|
|
254
|
+
get_inactive: bool = False,
|
|
255
|
+
) -> dict[str, ModelT]: ...
|
|
256
|
+
|
|
257
|
+
def get_bulk( # type: ignore
|
|
258
|
+
self,
|
|
259
|
+
uids,
|
|
260
|
+
attributes_model=LdapUserBulk,
|
|
261
|
+
*,
|
|
262
|
+
get_inactive: bool = False,
|
|
263
|
+
):
|
|
264
|
+
"""get ldap users via bulk query"""
|
|
265
|
+
uid_chunks = [
|
|
266
|
+
list(uids)[i : i + 500] for i in range(0, len(uids), 500)
|
|
267
|
+
]
|
|
268
|
+
ldap_users = []
|
|
269
|
+
for uid_chunk in uid_chunks:
|
|
270
|
+
uid_filter = "".join(
|
|
271
|
+
f"(uid={escape_filter_chars(uid)})" for uid in uid_chunk
|
|
272
|
+
)
|
|
273
|
+
state_filter = "" if get_inactive else "(zihState=active)"
|
|
274
|
+
filter_term = (
|
|
275
|
+
f"(&(objectClass=zihAccount){state_filter}(|{uid_filter}))"
|
|
276
|
+
)
|
|
277
|
+
ldap_users.extend(
|
|
278
|
+
self.connection.filter(
|
|
279
|
+
filter_term,
|
|
280
|
+
SearchBase.USERS.value,
|
|
281
|
+
attributes_model,
|
|
282
|
+
)
|
|
283
|
+
)
|
|
284
|
+
return {user.uid: user for user in ldap_users}
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
class LdapOrgUnitBase(BaseModel):
|
|
288
|
+
"""base ldap orgunit"""
|
|
289
|
+
|
|
290
|
+
dn: str
|
|
291
|
+
ou: Annotated[
|
|
292
|
+
str,
|
|
293
|
+
Field(
|
|
294
|
+
validation_alias=AliasPath("ou", 0),
|
|
295
|
+
serialization_alias="ou",
|
|
296
|
+
),
|
|
297
|
+
]
|
|
298
|
+
|
|
299
|
+
_get_language: ClassVar[
|
|
300
|
+
Literal["de", "en"] | Callable[[], Literal["de", "en"]]
|
|
301
|
+
] = "de"
|
|
302
|
+
|
|
303
|
+
@classmethod
|
|
304
|
+
def override_get_language(
|
|
305
|
+
cls,
|
|
306
|
+
get_language: Literal["de", "en"] | Callable[[], Literal["de", "en"]],
|
|
307
|
+
) -> None:
|
|
308
|
+
cls._get_language = get_language
|
|
309
|
+
|
|
310
|
+
def get_language(self) -> Literal["de", "en"]:
|
|
311
|
+
if isinstance(self._get_language, str):
|
|
312
|
+
return self._get_language
|
|
313
|
+
return self._get_language()
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
class LdapOrgUnit(LdapOrgUnitBase):
|
|
317
|
+
"""ldap orgunit"""
|
|
318
|
+
|
|
319
|
+
zihCostCenter: Annotated[
|
|
320
|
+
str,
|
|
321
|
+
Field(
|
|
322
|
+
validation_alias=AliasPath("zihCostCenter", 0),
|
|
323
|
+
serialization_alias="zihCostCenter",
|
|
324
|
+
),
|
|
325
|
+
]
|
|
326
|
+
zihExpiryDate: Annotated[
|
|
327
|
+
date | None,
|
|
328
|
+
BeforeValidator(string_to_date),
|
|
329
|
+
Field(
|
|
330
|
+
validation_alias=AliasPath("zihExpiryDate", 0),
|
|
331
|
+
serialization_alias="zihExpiryDate",
|
|
332
|
+
),
|
|
333
|
+
] = None
|
|
334
|
+
zihStartDate: Annotated[
|
|
335
|
+
date | None,
|
|
336
|
+
BeforeValidator(string_to_date),
|
|
337
|
+
Field(
|
|
338
|
+
validation_alias=AliasPath("zihStartDate", 0),
|
|
339
|
+
serialization_alias="zihStartDate",
|
|
340
|
+
),
|
|
341
|
+
] = None
|
|
342
|
+
zihState: Annotated[
|
|
343
|
+
Literal["false", "true", "deleted"] | None,
|
|
344
|
+
Field(
|
|
345
|
+
validation_alias=AliasPath("zihState", 0),
|
|
346
|
+
serialization_alias="zihState",
|
|
347
|
+
),
|
|
348
|
+
] = None
|
|
349
|
+
zihSupervisorOfStructureDN: list[str] | None = None
|
|
350
|
+
zihOrganisationName: Annotated[
|
|
351
|
+
str,
|
|
352
|
+
Field(
|
|
353
|
+
validation_alias=AliasPath("zihOrganisationName", 0),
|
|
354
|
+
serialization_alias="zihOrganisationName",
|
|
355
|
+
),
|
|
356
|
+
]
|
|
357
|
+
zihOrganisationNameDeLong: Annotated[
|
|
358
|
+
str | None,
|
|
359
|
+
Field(
|
|
360
|
+
validation_alias=AliasPath("zihOrganisationNameDeLong", 0),
|
|
361
|
+
serialization_alias="zihOrganisationNameDeLong",
|
|
362
|
+
),
|
|
363
|
+
] = None
|
|
364
|
+
zihOrganisationNameEnLong: Annotated[
|
|
365
|
+
str | None,
|
|
366
|
+
Field(
|
|
367
|
+
validation_alias=AliasPath("zihOrganisationNameEnLong", 0),
|
|
368
|
+
serialization_alias="zihOrganisationNameEnLong",
|
|
369
|
+
),
|
|
370
|
+
] = None
|
|
371
|
+
description: Annotated[
|
|
372
|
+
str | None,
|
|
373
|
+
Field(
|
|
374
|
+
validation_alias=AliasPath("description", 0),
|
|
375
|
+
serialization_alias="description",
|
|
376
|
+
),
|
|
377
|
+
] = None
|
|
378
|
+
|
|
379
|
+
@property
|
|
380
|
+
def translated_name(self) -> str:
|
|
381
|
+
"""Get translated organisation name if existent"""
|
|
382
|
+
name = self.zihOrganisationName
|
|
383
|
+
if self.zihOrganisationNameDeLong:
|
|
384
|
+
name = self.zihOrganisationNameDeLong
|
|
385
|
+
if self.get_language() == "en" and self.zihOrganisationNameEnLong:
|
|
386
|
+
name = self.zihOrganisationNameEnLong
|
|
387
|
+
return name
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
OrgUnitModelT = TypeVar("OrgUnitModelT", bound=LdapOrgUnitBase)
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
class LdapOrgUnitManager:
|
|
394
|
+
"""ldap OrgUnit manager"""
|
|
395
|
+
|
|
396
|
+
def __init__(self, connection: LDAPConnection) -> None:
|
|
397
|
+
self.connection = connection
|
|
398
|
+
|
|
399
|
+
@overload
|
|
400
|
+
def get(self, ou_of_orgunit: str) -> LdapOrgUnit: ...
|
|
401
|
+
|
|
402
|
+
@overload
|
|
403
|
+
def get(
|
|
404
|
+
self,
|
|
405
|
+
ou_of_orgunit: str,
|
|
406
|
+
attributes_model: type[OrgUnitModelT],
|
|
407
|
+
) -> OrgUnitModelT: ...
|
|
408
|
+
|
|
409
|
+
def get(self, ou_of_orgunit, attributes_model=LdapOrgUnit): # type: ignore
|
|
410
|
+
"""get LdapOrgUnit via ou"""
|
|
411
|
+
escaped_ou_of_orgunit = escape_filter_chars(ou_of_orgunit)
|
|
412
|
+
org_unit = self.connection.get(
|
|
413
|
+
f"(ou={escaped_ou_of_orgunit})",
|
|
414
|
+
SearchBase.STRUCTURES.value,
|
|
415
|
+
attributes_model,
|
|
416
|
+
SUBTREE,
|
|
417
|
+
)
|
|
418
|
+
return org_unit
|
|
419
|
+
|
|
420
|
+
@overload
|
|
421
|
+
def filter(self, search_term: str) -> list[LdapOrgUnit]: ...
|
|
422
|
+
|
|
423
|
+
@overload
|
|
424
|
+
def filter(
|
|
425
|
+
self,
|
|
426
|
+
search_term: str,
|
|
427
|
+
attributes_model: type[OrgUnitModelT],
|
|
428
|
+
) -> list[OrgUnitModelT]: ...
|
|
429
|
+
|
|
430
|
+
def filter(self, searchterm, attributes_model=LdapOrgUnit): # type: ignore
|
|
431
|
+
"""get ldap OrgUnit via python ldap3 searchterm"""
|
|
432
|
+
return self.connection.filter(
|
|
433
|
+
searchterm,
|
|
434
|
+
SearchBase.STRUCTURES.value,
|
|
435
|
+
attributes_model,
|
|
436
|
+
SUBTREE,
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
FacilityType = Literal[
|
|
441
|
+
"Institution", "Macro", "Micro", "Building", "Floor", "Room"
|
|
442
|
+
]
|
|
443
|
+
LdapFacilityUnitT = TypeVar("LdapFacilityUnitT", bound="LdapFacilityUnit")
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
class LdapFacilityUnitManager(Generic[LdapFacilityUnitT]):
|
|
447
|
+
"""ldap FacilityUnit manager"""
|
|
448
|
+
|
|
449
|
+
def __init__(
|
|
450
|
+
self, facility_type: FacilityType, connection: LDAPConnection
|
|
451
|
+
) -> None:
|
|
452
|
+
self.connection = connection
|
|
453
|
+
self.facility_type = facility_type
|
|
454
|
+
|
|
455
|
+
@property
|
|
456
|
+
def facility_model(self) -> Type["LdapFacilityUnit"]:
|
|
457
|
+
"""return facility model"""
|
|
458
|
+
facility_models: dict[FacilityType, Type[LdapFacilityUnit]] = {
|
|
459
|
+
"Institution": LdapFacilityInstitution,
|
|
460
|
+
"Macro": LdapFacilityMacroLocation,
|
|
461
|
+
"Micro": LdapFacilityMicroLocation,
|
|
462
|
+
"Building": LdapFacilityBuilding,
|
|
463
|
+
"Floor": LdapFacilityFloor,
|
|
464
|
+
"Room": LdapFacilityRoom,
|
|
465
|
+
}
|
|
466
|
+
return facility_models[self.facility_type]
|
|
467
|
+
|
|
468
|
+
def get(self, facility_id: str) -> LdapFacilityUnitT:
|
|
469
|
+
"""get LdapFacilityUnit via facility_id"""
|
|
470
|
+
escaped_facility_id = escape_filter_chars(facility_id)
|
|
471
|
+
facility = self.connection.get(
|
|
472
|
+
f"(&(zihFacilityID={escaped_facility_id})(zihFacilityType={self.facility_type}))",
|
|
473
|
+
SearchBase.FACILITY.value,
|
|
474
|
+
self.facility_model,
|
|
475
|
+
SUBTREE,
|
|
476
|
+
)
|
|
477
|
+
return cast(LdapFacilityUnitT, facility)
|
|
478
|
+
|
|
479
|
+
def filter(self, filterterm: str) -> list[LdapFacilityUnitT]:
|
|
480
|
+
"""get ldap LdapFacilityUnit via python ldap3 search term"""
|
|
481
|
+
return cast(
|
|
482
|
+
list[LdapFacilityUnitT],
|
|
483
|
+
self.connection.filter(
|
|
484
|
+
f"(&{filterterm} (zihFacilityType={self.facility_type}))",
|
|
485
|
+
SearchBase.FACILITY.value,
|
|
486
|
+
self.facility_model,
|
|
487
|
+
SUBTREE,
|
|
488
|
+
),
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
def all(self) -> list[LdapFacilityUnitT]:
|
|
492
|
+
"""get all ldap LdapFacilityUnit via of this type"""
|
|
493
|
+
return cast(
|
|
494
|
+
list[LdapFacilityUnitT],
|
|
495
|
+
self.connection.filter(
|
|
496
|
+
f"(zihFacilityType={self.facility_type})",
|
|
497
|
+
SearchBase.FACILITY.value,
|
|
498
|
+
self.facility_model,
|
|
499
|
+
SUBTREE,
|
|
500
|
+
),
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
class LdapFacilityUnit(BaseModel):
|
|
505
|
+
"""Base class for all facility unites"""
|
|
506
|
+
|
|
507
|
+
zihFacilityID: Annotated[
|
|
508
|
+
str,
|
|
509
|
+
Field(
|
|
510
|
+
validation_alias=AliasPath("zihFacilityID", 0),
|
|
511
|
+
serialization_alias="zihFacilityID",
|
|
512
|
+
),
|
|
513
|
+
]
|
|
514
|
+
zihFacilityType: Annotated[
|
|
515
|
+
FacilityType,
|
|
516
|
+
Field(
|
|
517
|
+
validation_alias=AliasPath("zihFacilityType", 0),
|
|
518
|
+
serialization_alias="zihFacilityType",
|
|
519
|
+
),
|
|
520
|
+
]
|
|
521
|
+
zihFacilityName: Annotated[
|
|
522
|
+
str,
|
|
523
|
+
Field(
|
|
524
|
+
validation_alias=AliasPath("zihFacilityName", 0),
|
|
525
|
+
serialization_alias="zihFacilityName",
|
|
526
|
+
),
|
|
527
|
+
]
|
|
528
|
+
zihStateLastChanged: Annotated[
|
|
529
|
+
datetime,
|
|
530
|
+
Field(
|
|
531
|
+
validation_alias=AliasPath("zihStateLastChanged", 0),
|
|
532
|
+
serialization_alias="zihStateLastChanged",
|
|
533
|
+
),
|
|
534
|
+
]
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
class LdapFacilityInstitution(LdapFacilityUnit):
|
|
538
|
+
"""ldap facility unit of a institution"""
|
|
539
|
+
|
|
540
|
+
object_list: ClassVar[list["LdapFacilityInstitution"]] = []
|
|
541
|
+
|
|
542
|
+
zihFacilityType: Annotated[
|
|
543
|
+
Literal["Institution"],
|
|
544
|
+
Field(
|
|
545
|
+
validation_alias=AliasPath("zihFacilityType", 0),
|
|
546
|
+
serialization_alias="zihFacilityType",
|
|
547
|
+
),
|
|
548
|
+
]
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
def newLdapFacilityInstitutionManager(
|
|
552
|
+
connection: LDAPConnection,
|
|
553
|
+
) -> LdapFacilityUnitManager["LdapFacilityInstitution"]:
|
|
554
|
+
return LdapFacilityUnitManager(
|
|
555
|
+
facility_type="Institution", connection=connection
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
class LdapFacilityMacroLocation(LdapFacilityUnit):
|
|
560
|
+
"""ldap facility unit of a macrolocation"""
|
|
561
|
+
|
|
562
|
+
zihFacilityType: Annotated[
|
|
563
|
+
Literal["Macro"],
|
|
564
|
+
Field(
|
|
565
|
+
validation_alias=AliasPath("zihFacilityType", 0),
|
|
566
|
+
serialization_alias="zihFacilityType",
|
|
567
|
+
),
|
|
568
|
+
]
|
|
569
|
+
|
|
570
|
+
|
|
571
|
+
def newLdapFacilityMacroLocationManager(
|
|
572
|
+
connection: LDAPConnection,
|
|
573
|
+
) -> LdapFacilityUnitManager["LdapFacilityMacroLocation"]:
|
|
574
|
+
return LdapFacilityUnitManager(
|
|
575
|
+
facility_type="Macro", connection=connection
|
|
576
|
+
)
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
class LdapFacilityMicroLocation(LdapFacilityUnit):
|
|
580
|
+
"""ldap facility unit of a microlocation"""
|
|
581
|
+
|
|
582
|
+
zihFacilityType: Annotated[
|
|
583
|
+
Literal["Micro"],
|
|
584
|
+
Field(
|
|
585
|
+
validation_alias=AliasPath("zihFacilityType", 0),
|
|
586
|
+
serialization_alias="zihFacilityType",
|
|
587
|
+
),
|
|
588
|
+
]
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
def newLdapFacilityMicroLocationManager(
|
|
592
|
+
connection: LDAPConnection,
|
|
593
|
+
) -> LdapFacilityUnitManager["LdapFacilityMicroLocation"]:
|
|
594
|
+
return LdapFacilityUnitManager(
|
|
595
|
+
facility_type="Micro", connection=connection
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
class LdapFacilityBuilding(LdapFacilityUnit):
|
|
600
|
+
"""ldap facility unit of a building"""
|
|
601
|
+
|
|
602
|
+
zihFacilityType: Annotated[
|
|
603
|
+
Literal["Building"],
|
|
604
|
+
Field(
|
|
605
|
+
validation_alias=AliasPath("zihFacilityType", 0),
|
|
606
|
+
serialization_alias="zihFacilityType",
|
|
607
|
+
),
|
|
608
|
+
]
|
|
609
|
+
zihFacilityShortName: Annotated[
|
|
610
|
+
str | None,
|
|
611
|
+
Field(
|
|
612
|
+
validation_alias=AliasPath("zihFacilityShortName", 0),
|
|
613
|
+
serialization_alias="zihFacilityShortName",
|
|
614
|
+
),
|
|
615
|
+
] = None
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
def newLdapFacilityBuildingManager(
|
|
619
|
+
connection: LDAPConnection,
|
|
620
|
+
) -> LdapFacilityUnitManager["LdapFacilityBuilding"]:
|
|
621
|
+
return LdapFacilityUnitManager(
|
|
622
|
+
facility_type="Building", connection=connection
|
|
623
|
+
)
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
class LdapFacilityFloor(LdapFacilityUnit):
|
|
627
|
+
"""ldap facility unit of a floor"""
|
|
628
|
+
|
|
629
|
+
zihFacilityType: Annotated[
|
|
630
|
+
Literal["Floor"],
|
|
631
|
+
Field(
|
|
632
|
+
validation_alias=AliasPath("zihFacilityType", 0),
|
|
633
|
+
serialization_alias="zihFacilityType",
|
|
634
|
+
),
|
|
635
|
+
]
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
def newLdapFacilityFloorManager(
|
|
639
|
+
connection: LDAPConnection,
|
|
640
|
+
) -> LdapFacilityUnitManager["LdapFacilityFloor"]:
|
|
641
|
+
return LdapFacilityUnitManager(
|
|
642
|
+
facility_type="Floor", connection=connection
|
|
643
|
+
)
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
class LdapFacilityRoom(LdapFacilityUnit):
|
|
647
|
+
"""ldap facility unit of a room"""
|
|
648
|
+
|
|
649
|
+
zihFacilityType: Annotated[
|
|
650
|
+
Literal["Room"],
|
|
651
|
+
Field(
|
|
652
|
+
validation_alias=AliasPath("zihFacilityType", 0),
|
|
653
|
+
serialization_alias="zihFacilityType",
|
|
654
|
+
),
|
|
655
|
+
]
|
|
656
|
+
zihFacilityRoomTypeID: Annotated[
|
|
657
|
+
str,
|
|
658
|
+
Field(validation_alias=AliasPath("zihFacilityRoomTypeID", 0)),
|
|
659
|
+
]
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
def newLdapFacilityRoomManager(
|
|
663
|
+
connection: LDAPConnection,
|
|
664
|
+
) -> LdapFacilityUnitManager["LdapFacilityRoom"]:
|
|
665
|
+
return LdapFacilityUnitManager(facility_type="Room", connection=connection)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""validators for pydantic"""
|
|
2
|
+
|
|
3
|
+
from datetime import date, datetime
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from ldap3.utils.dn import parse_dn
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def active_to_bool(value: Any) -> bool:
|
|
10
|
+
"""make string state to bool"""
|
|
11
|
+
match value:
|
|
12
|
+
case "active":
|
|
13
|
+
return True
|
|
14
|
+
case "inactive":
|
|
15
|
+
return False
|
|
16
|
+
case _:
|
|
17
|
+
raise ValueError(f"unknown state '{value}'")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def group_dn_to_name(distinguished_names: list[str]) -> list[str]:
|
|
21
|
+
"""convert group-dn to just it's name (cn) part"""
|
|
22
|
+
return [
|
|
23
|
+
value
|
|
24
|
+
for group_dn in distinguished_names
|
|
25
|
+
for component, value, _ in parse_dn(group_dn)
|
|
26
|
+
if component == "cn"
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def string_to_date(value: str | None) -> date | None:
|
|
31
|
+
"""parse a "20120101" string to a date"""
|
|
32
|
+
if not value:
|
|
33
|
+
return None
|
|
34
|
+
return datetime.strptime(value, "%Y%m%d").date()
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def to_str(value: Any) -> str | None:
|
|
38
|
+
"""convert to str needed for mypy"""
|
|
39
|
+
if value is None:
|
|
40
|
+
return value
|
|
41
|
+
return str(value)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""test ldap"""
|
|
2
|
+
|
|
3
|
+
from datetime import date
|
|
4
|
+
from typing import ClassVar
|
|
5
|
+
from unittest import TestCase
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from typing_extensions import override
|
|
9
|
+
|
|
10
|
+
from src.conftest import Settings
|
|
11
|
+
|
|
12
|
+
from .connection import LDAPConnection, ObjectDoesNotExist, new_ldap_connection
|
|
13
|
+
from .models import (
|
|
14
|
+
LdapOrgUnitManager,
|
|
15
|
+
LdapUserManager,
|
|
16
|
+
newLdapFacilityBuildingManager,
|
|
17
|
+
newLdapFacilityFloorManager,
|
|
18
|
+
newLdapFacilityInstitutionManager,
|
|
19
|
+
newLdapFacilityMacroLocationManager,
|
|
20
|
+
newLdapFacilityMicroLocationManager,
|
|
21
|
+
newLdapFacilityRoomManager,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@pytest.fixture(scope="class")
|
|
26
|
+
def external_connection(
|
|
27
|
+
request: pytest.FixtureRequest, external_settings: Settings
|
|
28
|
+
) -> None:
|
|
29
|
+
"""make connection"""
|
|
30
|
+
request.cls.external_connection = new_ldap_connection(
|
|
31
|
+
external_settings.ldap_address,
|
|
32
|
+
external_settings.ldap_user_bind_dn,
|
|
33
|
+
external_settings.ldap_password.get_secret_value(),
|
|
34
|
+
port=external_settings.ldap_port,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@pytest.mark.usefixtures("external_connection")
|
|
39
|
+
class TestLDAP(TestCase):
|
|
40
|
+
"""test LDAP
|
|
41
|
+
|
|
42
|
+
Some simple tests against the productive ldap via LDAPConnection
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
external_connection: ClassVar[LDAPConnection]
|
|
46
|
+
|
|
47
|
+
@override
|
|
48
|
+
def setUp(
|
|
49
|
+
self,
|
|
50
|
+
) -> None:
|
|
51
|
+
super().setUp()
|
|
52
|
+
self.user_manager = LdapUserManager(self.external_connection)
|
|
53
|
+
self.ou_manager = LdapOrgUnitManager(self.external_connection)
|
|
54
|
+
|
|
55
|
+
def test_get_user_by_uid(self) -> None:
|
|
56
|
+
"""get user by uid"""
|
|
57
|
+
|
|
58
|
+
expected = self.user_manager.get("teal858d")
|
|
59
|
+
self.assertIsNotNone(expected)
|
|
60
|
+
assert expected
|
|
61
|
+
self.assertEqual(
|
|
62
|
+
expected.displayName, "Alleskönner, Testobjekt (SSP-Tester)"
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
def test_try_get_user_with_wrong_uid(self) -> None:
|
|
66
|
+
"""try get user with wrong uid"""
|
|
67
|
+
|
|
68
|
+
with self.assertRaises(ObjectDoesNotExist):
|
|
69
|
+
self.user_manager.get("gdjfgjsdfjksdh")
|
|
70
|
+
|
|
71
|
+
def test_try_get_user_with_escape(self) -> None:
|
|
72
|
+
"""try get user with uid that needs to be escaped"""
|
|
73
|
+
|
|
74
|
+
with self.assertRaises(ObjectDoesNotExist):
|
|
75
|
+
self.user_manager.get("teal858d)")
|
|
76
|
+
|
|
77
|
+
def test_get_users_by_searchterm(self) -> None:
|
|
78
|
+
"""get users by searchterm"""
|
|
79
|
+
expected = self.user_manager.filter("(sn=Schmidt)")
|
|
80
|
+
self.assertNotEqual(len(expected), 0)
|
|
81
|
+
# https://github.com/pylint-dev/astroid/issues/1015
|
|
82
|
+
# pylint: disable=not-an-iterable
|
|
83
|
+
for user in expected:
|
|
84
|
+
self.assertEqual(user.last_name, "Schmidt")
|
|
85
|
+
|
|
86
|
+
def test_get_users_by_bulk_request(self) -> None:
|
|
87
|
+
"""get users by a set of uids"""
|
|
88
|
+
usernames = {"teal858d", "tete554d"}
|
|
89
|
+
users = self.user_manager.get_bulk(usernames)
|
|
90
|
+
self.assertNotEqual(users, {})
|
|
91
|
+
self.assertCountEqual(usernames, users.keys())
|
|
92
|
+
|
|
93
|
+
def test_get_orgunit_by_ou(self) -> None:
|
|
94
|
+
"""get orgUnit by ou"""
|
|
95
|
+
|
|
96
|
+
expected = self.ou_manager.get("10000009")
|
|
97
|
+
self.assertIsNotNone(expected)
|
|
98
|
+
assert expected
|
|
99
|
+
self.assertEqual(expected.zihExpiryDate, date(9999, 12, 31))
|
|
100
|
+
|
|
101
|
+
def test_try_get_orgunit_with_wrong_ou(self) -> None:
|
|
102
|
+
"""try get orgunit with wrong ou"""
|
|
103
|
+
with self.assertRaises(ObjectDoesNotExist):
|
|
104
|
+
self.ou_manager.get("1000sgfsggdfgd dgdg 0009")
|
|
105
|
+
|
|
106
|
+
def test_try_get_orgunit_with_escape(self) -> None:
|
|
107
|
+
"""try get orgunit with ou that needs to be escaped"""
|
|
108
|
+
|
|
109
|
+
with self.assertRaises(ObjectDoesNotExist):
|
|
110
|
+
self.ou_manager.get("10000009)")
|
|
111
|
+
|
|
112
|
+
def test_get_orgunit_by_searchterm(self) -> None:
|
|
113
|
+
"""get orgunit by searchterm"""
|
|
114
|
+
expected = self.ou_manager.filter("(zihExpiryDate=99991231)")
|
|
115
|
+
self.assertNotEqual(len(expected), 0)
|
|
116
|
+
# https://github.com/pylint-dev/astroid/issues/1015
|
|
117
|
+
# pylint: disable=not-an-iterable
|
|
118
|
+
for org_unit in expected:
|
|
119
|
+
self.assertEqual(org_unit.zihExpiryDate, date(9999, 12, 31))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@pytest.mark.usefixtures("external_connection")
|
|
123
|
+
class TestLDAPFacilities(TestCase):
|
|
124
|
+
"""test LDAP facilities"""
|
|
125
|
+
|
|
126
|
+
external_connection: ClassVar[LDAPConnection]
|
|
127
|
+
|
|
128
|
+
def test_get_institution_by_id(self) -> None:
|
|
129
|
+
"""get institution by id"""
|
|
130
|
+
expected = newLdapFacilityInstitutionManager(
|
|
131
|
+
self.external_connection
|
|
132
|
+
).get("TU")
|
|
133
|
+
self.assertEqual(
|
|
134
|
+
expected.zihFacilityName, "Technische Universität Dresden"
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def test_get_macro_location_by_id(self) -> None:
|
|
138
|
+
"""get macro location by id"""
|
|
139
|
+
expected = newLdapFacilityMacroLocationManager(
|
|
140
|
+
self.external_connection
|
|
141
|
+
).get("TU-DD")
|
|
142
|
+
self.assertEqual(expected.zihFacilityName, "Dresden")
|
|
143
|
+
|
|
144
|
+
def test_get_micro_location_by_id(self) -> None:
|
|
145
|
+
"""get micro location by id"""
|
|
146
|
+
expected = newLdapFacilityMicroLocationManager(
|
|
147
|
+
self.external_connection
|
|
148
|
+
).get("TU-DD-200")
|
|
149
|
+
self.assertEqual(expected.zihFacilityName, "Hörsaalzentrum")
|
|
150
|
+
|
|
151
|
+
def test_get_building_by_id(self) -> None:
|
|
152
|
+
"""get building by id"""
|
|
153
|
+
expected = newLdapFacilityBuildingManager(
|
|
154
|
+
self.external_connection
|
|
155
|
+
).get("TU-DD-200-1361")
|
|
156
|
+
self.assertEqual(
|
|
157
|
+
expected.zihFacilityName, "HSZ Hörsaalzentrum, Bergstr. 64"
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
def test_get_floor_by_id(self) -> None:
|
|
161
|
+
"""get floor by id"""
|
|
162
|
+
expected = newLdapFacilityFloorManager(self.external_connection).get(
|
|
163
|
+
"TU-DD-200-1361-01"
|
|
164
|
+
)
|
|
165
|
+
self.assertEqual(expected.zihFacilityName, "01")
|
|
166
|
+
|
|
167
|
+
def test_get_room_by_id(self) -> None:
|
|
168
|
+
"""get room by id"""
|
|
169
|
+
expected = newLdapFacilityRoomManager(self.external_connection).get(
|
|
170
|
+
"TU-DD-200-1361-01-0140"
|
|
171
|
+
)
|
|
172
|
+
self.assertEqual(expected.zihFacilityName, "114")
|