glpi-python-client 0.6.5__py3-none-any.whl → 0.6.7__py3-none-any.whl
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.
- glpi_python_client/__init__.py +11 -1
- glpi_python_client/_async/clients/api/__init__.py +2 -0
- glpi_python_client/_async/clients/api/administration/__init__.py +3 -2
- glpi_python_client/_async/clients/api/administration/_group.py +261 -0
- glpi_python_client/_async/clients/api/administration/_user.py +214 -1
- glpi_python_client/_async/clients/client.py +2 -0
- glpi_python_client/_async/clients/commons/_constants.py +2 -0
- glpi_python_client/_async/clients/commons/_payloads.py +35 -1
- glpi_python_client/_sync/clients/api/__init__.py +2 -0
- glpi_python_client/_sync/clients/api/administration/__init__.py +3 -2
- glpi_python_client/_sync/clients/api/administration/_group.py +261 -0
- glpi_python_client/_sync/clients/api/administration/_user.py +214 -1
- glpi_python_client/_sync/clients/client.py +2 -0
- glpi_python_client/_sync/clients/commons/_constants.py +2 -0
- glpi_python_client/_sync/clients/commons/_payloads.py +35 -1
- glpi_python_client/models/__init__.py +10 -0
- glpi_python_client/models/api_schema/administration/__init__.py +17 -2
- glpi_python_client/models/api_schema/administration/_group.py +150 -0
- glpi_python_client/models/api_schema/administration/_user_authorization.py +48 -0
- {glpi_python_client-0.6.5.dist-info → glpi_python_client-0.6.7.dist-info}/METADATA +1 -1
- {glpi_python_client-0.6.5.dist-info → glpi_python_client-0.6.7.dist-info}/RECORD +23 -19
- {glpi_python_client-0.6.5.dist-info → glpi_python_client-0.6.7.dist-info}/WHEEL +1 -1
- {glpi_python_client-0.6.5.dist-info → glpi_python_client-0.6.7.dist-info}/licenses/LICENSE +0 -0
glpi_python_client/__init__.py
CHANGED
|
@@ -40,6 +40,7 @@ from glpi_python_client.models import (
|
|
|
40
40
|
DeleteDocument,
|
|
41
41
|
DeleteEntity,
|
|
42
42
|
DeleteFollowup,
|
|
43
|
+
DeleteGroup,
|
|
43
44
|
DeleteKBArticle,
|
|
44
45
|
DeleteKBArticleComment,
|
|
45
46
|
DeleteKBCategory,
|
|
@@ -60,6 +61,7 @@ from glpi_python_client.models import (
|
|
|
60
61
|
GetDocumentItem,
|
|
61
62
|
GetEntity,
|
|
62
63
|
GetFollowup,
|
|
64
|
+
GetGroup,
|
|
63
65
|
GetKBArticle,
|
|
64
66
|
GetKBArticleComment,
|
|
65
67
|
GetKBArticleRevision,
|
|
@@ -96,6 +98,7 @@ from glpi_python_client.models import (
|
|
|
96
98
|
PatchDocument,
|
|
97
99
|
PatchEntity,
|
|
98
100
|
PatchFollowup,
|
|
101
|
+
PatchGroup,
|
|
99
102
|
PatchKBArticle,
|
|
100
103
|
PatchKBArticleComment,
|
|
101
104
|
PatchKBCategory,
|
|
@@ -114,6 +117,7 @@ from glpi_python_client.models import (
|
|
|
114
117
|
PostDocument,
|
|
115
118
|
PostEntity,
|
|
116
119
|
PostFollowup,
|
|
120
|
+
PostGroup,
|
|
117
121
|
PostKBArticle,
|
|
118
122
|
PostKBArticleComment,
|
|
119
123
|
PostKBCategory,
|
|
@@ -126,6 +130,7 @@ from glpi_python_client.models import (
|
|
|
126
130
|
PostTimelineDocument,
|
|
127
131
|
PostUser,
|
|
128
132
|
TicketMarkdownOptions,
|
|
133
|
+
UserAuthorization,
|
|
129
134
|
)
|
|
130
135
|
from glpi_python_client.rsql import (
|
|
131
136
|
changed_since,
|
|
@@ -133,7 +138,7 @@ from glpi_python_client.rsql import (
|
|
|
133
138
|
date_window,
|
|
134
139
|
)
|
|
135
140
|
|
|
136
|
-
__version__ = "0.6.
|
|
141
|
+
__version__ = "0.6.7"
|
|
137
142
|
|
|
138
143
|
__all__ = [
|
|
139
144
|
"AsyncGlpiClient",
|
|
@@ -145,6 +150,7 @@ __all__ = [
|
|
|
145
150
|
"DeleteDocument",
|
|
146
151
|
"DeleteEntity",
|
|
147
152
|
"DeleteFollowup",
|
|
153
|
+
"DeleteGroup",
|
|
148
154
|
"DeleteKBArticle",
|
|
149
155
|
"DeleteKBArticleComment",
|
|
150
156
|
"DeleteKBCategory",
|
|
@@ -165,6 +171,7 @@ __all__ = [
|
|
|
165
171
|
"GetDocumentItem",
|
|
166
172
|
"GetEntity",
|
|
167
173
|
"GetFollowup",
|
|
174
|
+
"GetGroup",
|
|
168
175
|
"GetKBArticle",
|
|
169
176
|
"GetKBArticleComment",
|
|
170
177
|
"GetKBArticleRevision",
|
|
@@ -212,6 +219,7 @@ __all__ = [
|
|
|
212
219
|
"PatchDocument",
|
|
213
220
|
"PatchEntity",
|
|
214
221
|
"PatchFollowup",
|
|
222
|
+
"PatchGroup",
|
|
215
223
|
"PatchKBArticle",
|
|
216
224
|
"PatchKBArticleComment",
|
|
217
225
|
"PatchKBCategory",
|
|
@@ -230,6 +238,7 @@ __all__ = [
|
|
|
230
238
|
"PostDocument",
|
|
231
239
|
"PostEntity",
|
|
232
240
|
"PostFollowup",
|
|
241
|
+
"PostGroup",
|
|
233
242
|
"PostKBArticle",
|
|
234
243
|
"PostKBArticleComment",
|
|
235
244
|
"PostKBCategory",
|
|
@@ -242,6 +251,7 @@ __all__ = [
|
|
|
242
251
|
"PostTimelineDocument",
|
|
243
252
|
"PostUser",
|
|
244
253
|
"TicketMarkdownOptions",
|
|
254
|
+
"UserAuthorization",
|
|
245
255
|
"__version__",
|
|
246
256
|
"changed_since",
|
|
247
257
|
"created_between",
|
|
@@ -11,6 +11,7 @@ from __future__ import annotations
|
|
|
11
11
|
|
|
12
12
|
from glpi_python_client._async.clients.api.administration import (
|
|
13
13
|
EntityMixin,
|
|
14
|
+
GroupMixin,
|
|
14
15
|
UserMixin,
|
|
15
16
|
)
|
|
16
17
|
from glpi_python_client._async.clients.api.assets import ComputerMixin
|
|
@@ -49,6 +50,7 @@ __all__ = [
|
|
|
49
50
|
"DocumentMixin",
|
|
50
51
|
"EntityMixin",
|
|
51
52
|
"FollowupMixin",
|
|
53
|
+
"GroupMixin",
|
|
52
54
|
"KBArticleCommentMixin",
|
|
53
55
|
"KBArticleMixin",
|
|
54
56
|
"KBArticleRevisionMixin",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""GLPI ``/Administration`` mixins for the GLPI client.
|
|
2
2
|
|
|
3
|
-
The submodules expose the user and
|
|
3
|
+
The submodules expose the user, entity and group mixins used by this tree's
|
|
4
4
|
client -- :class:`glpi_python_client.AsyncGlpiClient` on the async
|
|
5
5
|
surface, :class:`glpi_python_client.GlpiClient` on the generated one.
|
|
6
6
|
"""
|
|
@@ -8,6 +8,7 @@ surface, :class:`glpi_python_client.GlpiClient` on the generated one.
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
10
|
from glpi_python_client._async.clients.api.administration._entity import EntityMixin
|
|
11
|
+
from glpi_python_client._async.clients.api.administration._group import GroupMixin
|
|
11
12
|
from glpi_python_client._async.clients.api.administration._user import UserMixin
|
|
12
13
|
|
|
13
|
-
__all__ = ["EntityMixin", "UserMixin"]
|
|
14
|
+
__all__ = ["EntityMixin", "GroupMixin", "UserMixin"]
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"""GLPI ``/Administration/Group`` mixin.
|
|
2
|
+
|
|
3
|
+
The mixin exposes search, fetch, create, update, and delete helpers for the
|
|
4
|
+
GLPI group resource. Every call sends the client's ``GLPI-Entity`` header
|
|
5
|
+
(and the profile and recursion headers that travel with it) except where a
|
|
6
|
+
search is told to skip it with ``skip_entity=True``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import AsyncIterator
|
|
12
|
+
|
|
13
|
+
from glpi_python_client._async.clients.commons._constants import GROUP_ENDPOINT, GlpiId
|
|
14
|
+
from glpi_python_client._async.clients.commons._payloads import (
|
|
15
|
+
ensure_v1_write_accepted,
|
|
16
|
+
)
|
|
17
|
+
from glpi_python_client._async.clients.commons._transport import TransportMixin
|
|
18
|
+
from glpi_python_client.models.api_schema.administration._group import (
|
|
19
|
+
DeleteGroup,
|
|
20
|
+
GetGroup,
|
|
21
|
+
PatchGroup,
|
|
22
|
+
PostGroup,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
_V1_GROUP_FEATURE_LABEL = "group recursion"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class GroupMixin(TransportMixin):
|
|
29
|
+
"""CRUD helpers for ``/Administration/Group``."""
|
|
30
|
+
|
|
31
|
+
async def search_groups(
|
|
32
|
+
self,
|
|
33
|
+
rsql_filter: str = "",
|
|
34
|
+
*,
|
|
35
|
+
limit: int = 50,
|
|
36
|
+
start: int = 0,
|
|
37
|
+
skip_entity: bool = False,
|
|
38
|
+
) -> list[GetGroup]:
|
|
39
|
+
"""Search GLPI groups with an optional RSQL filter.
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
rsql_filter : str, optional
|
|
44
|
+
Raw RSQL filter forwarded as the ``filter`` query parameter,
|
|
45
|
+
for example ``"name==Support"``. Empty by default.
|
|
46
|
+
limit : int, optional
|
|
47
|
+
Maximum number of records returned by the GLPI server.
|
|
48
|
+
start : int, optional
|
|
49
|
+
Zero-based offset of the first record returned.
|
|
50
|
+
skip_entity : bool, optional
|
|
51
|
+
When ``True`` the ``GLPI-Entity`` header is omitted so the
|
|
52
|
+
search spans every entity the caller has access to.
|
|
53
|
+
|
|
54
|
+
Returns
|
|
55
|
+
-------
|
|
56
|
+
list[GetGroup]
|
|
57
|
+
Groups matching the filter.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
params: dict[str, object] = {"limit": limit, "start": start}
|
|
61
|
+
if rsql_filter:
|
|
62
|
+
params["filter"] = rsql_filter
|
|
63
|
+
return await self._resource_list(
|
|
64
|
+
GROUP_ENDPOINT, GetGroup, params=params, skip_entity=skip_entity
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
async def iter_search_groups(
|
|
68
|
+
self,
|
|
69
|
+
rsql_filter: str = "",
|
|
70
|
+
*,
|
|
71
|
+
batch_size: int = 50,
|
|
72
|
+
skip_entity: bool = False,
|
|
73
|
+
) -> AsyncIterator[list[GetGroup]]:
|
|
74
|
+
"""Yield successive pages of GLPI groups until exhausted.
|
|
75
|
+
|
|
76
|
+
Pagination advances the ``start`` offset after each batch and stops
|
|
77
|
+
when the server returns fewer items than ``batch_size``.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
rsql_filter : str, optional
|
|
82
|
+
Raw RSQL filter forwarded as the ``filter`` query parameter.
|
|
83
|
+
batch_size : int, optional
|
|
84
|
+
Number of records requested per page (default 50).
|
|
85
|
+
skip_entity : bool, optional
|
|
86
|
+
When ``True`` the ``GLPI-Entity`` header is omitted so the
|
|
87
|
+
search spans every entity the caller has access to.
|
|
88
|
+
|
|
89
|
+
Yields
|
|
90
|
+
------
|
|
91
|
+
list[GetGroup]
|
|
92
|
+
One page of groups per iteration. The last yielded batch may
|
|
93
|
+
be shorter than ``batch_size``.
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
start = 0
|
|
97
|
+
while True:
|
|
98
|
+
batch = await self.search_groups(
|
|
99
|
+
rsql_filter,
|
|
100
|
+
limit=batch_size,
|
|
101
|
+
start=start,
|
|
102
|
+
skip_entity=skip_entity,
|
|
103
|
+
)
|
|
104
|
+
if batch:
|
|
105
|
+
yield batch
|
|
106
|
+
if len(batch) < batch_size:
|
|
107
|
+
break
|
|
108
|
+
start += batch_size
|
|
109
|
+
|
|
110
|
+
async def get_group(self, group_id: GlpiId) -> GetGroup:
|
|
111
|
+
"""Fetch one GLPI group by identifier.
|
|
112
|
+
|
|
113
|
+
Parameters
|
|
114
|
+
----------
|
|
115
|
+
group_id : GlpiId
|
|
116
|
+
Numeric identifier of the group to retrieve.
|
|
117
|
+
|
|
118
|
+
Returns
|
|
119
|
+
-------
|
|
120
|
+
GetGroup
|
|
121
|
+
Validated group payload.
|
|
122
|
+
|
|
123
|
+
Raises
|
|
124
|
+
------
|
|
125
|
+
GlpiStatusError
|
|
126
|
+
If the GLPI server returns a non-success HTTP status.
|
|
127
|
+
"""
|
|
128
|
+
|
|
129
|
+
return await self._resource_get(
|
|
130
|
+
f"{GROUP_ENDPOINT}/{group_id}",
|
|
131
|
+
GetGroup,
|
|
132
|
+
failure_message=f"Failed to get group {group_id}",
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
async def create_group(self, group: PostGroup) -> int:
|
|
136
|
+
"""Create one GLPI group.
|
|
137
|
+
|
|
138
|
+
Parameters
|
|
139
|
+
----------
|
|
140
|
+
group : PostGroup
|
|
141
|
+
Request body describing the group to create.
|
|
142
|
+
|
|
143
|
+
Returns
|
|
144
|
+
-------
|
|
145
|
+
int
|
|
146
|
+
Identifier assigned by the GLPI server.
|
|
147
|
+
|
|
148
|
+
Raises
|
|
149
|
+
------
|
|
150
|
+
GlpiStatusError
|
|
151
|
+
If the GLPI server returns a non-success HTTP status.
|
|
152
|
+
GlpiProtocolError
|
|
153
|
+
If the create response is missing the ``id`` field.
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
return await self._resource_create(
|
|
157
|
+
GROUP_ENDPOINT,
|
|
158
|
+
group,
|
|
159
|
+
failure_message="Failed to create group",
|
|
160
|
+
missing_message="GLPI group create response did not include an ID",
|
|
161
|
+
log_message_factory=lambda new_id: f"GLPI API created group {new_id}",
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
async def update_group(self, group_id: GlpiId, group: PatchGroup) -> None:
|
|
165
|
+
"""Update one GLPI group with a partial body.
|
|
166
|
+
|
|
167
|
+
Parameters
|
|
168
|
+
----------
|
|
169
|
+
group_id : GlpiId
|
|
170
|
+
Numeric identifier of the group to update.
|
|
171
|
+
group : PatchGroup
|
|
172
|
+
Partial request body.
|
|
173
|
+
|
|
174
|
+
Returns
|
|
175
|
+
-------
|
|
176
|
+
None
|
|
177
|
+
|
|
178
|
+
Raises
|
|
179
|
+
------
|
|
180
|
+
GlpiStatusError
|
|
181
|
+
If the GLPI server returns a non-success HTTP status.
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
await self._resource_update(
|
|
185
|
+
f"{GROUP_ENDPOINT}/{group_id}",
|
|
186
|
+
group,
|
|
187
|
+
failure_message=f"Failed to update group {group_id}",
|
|
188
|
+
log_message=f"GLPI API updated group {group_id}",
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
async def delete_group(
|
|
192
|
+
self, group_id: GlpiId, *, force: bool | None = None
|
|
193
|
+
) -> None:
|
|
194
|
+
"""Delete one GLPI group by identifier.
|
|
195
|
+
|
|
196
|
+
Parameters
|
|
197
|
+
----------
|
|
198
|
+
group_id : GlpiId
|
|
199
|
+
Numeric identifier of the group to delete.
|
|
200
|
+
force : bool | None, optional
|
|
201
|
+
When ``True`` the group is permanently deleted instead of
|
|
202
|
+
being moved to the trash.
|
|
203
|
+
|
|
204
|
+
Returns
|
|
205
|
+
-------
|
|
206
|
+
None
|
|
207
|
+
|
|
208
|
+
Raises
|
|
209
|
+
------
|
|
210
|
+
GlpiStatusError
|
|
211
|
+
If the GLPI server returns a non-success HTTP status.
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
await self._resource_delete(
|
|
215
|
+
f"{GROUP_ENDPOINT}/{group_id}",
|
|
216
|
+
failure_message=f"Failed to delete group {group_id}",
|
|
217
|
+
log_message=f"GLPI API deleted group {group_id}",
|
|
218
|
+
force=force,
|
|
219
|
+
delete_model_cls=DeleteGroup,
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
async def set_group_recursive(
|
|
223
|
+
self, group_id: GlpiId, recursive: bool = True
|
|
224
|
+
) -> None:
|
|
225
|
+
"""Set whether one group is visible in the sub-entities of its entity.
|
|
226
|
+
|
|
227
|
+
The GLPI v2 ``Group`` schema has no ``is_recursive`` property (a v2
|
|
228
|
+
create or update silently drops the key), so this helper writes the
|
|
229
|
+
flag through the legacy v1 API, which requires
|
|
230
|
+
``v1_base_url``/``v1_user_token`` to be configured (pointing at the
|
|
231
|
+
legacy ``apirest.php``).
|
|
232
|
+
|
|
233
|
+
Parameters
|
|
234
|
+
----------
|
|
235
|
+
group_id : int | str
|
|
236
|
+
Identifier of the group.
|
|
237
|
+
recursive : bool, optional
|
|
238
|
+
``True`` (the default) makes the group apply to the sub-entities
|
|
239
|
+
of its entity; ``False`` restricts it to its own entity.
|
|
240
|
+
|
|
241
|
+
Raises
|
|
242
|
+
------
|
|
243
|
+
RuntimeError
|
|
244
|
+
When no legacy v1 session is configured on the client.
|
|
245
|
+
GlpiStatusError
|
|
246
|
+
When the legacy API returns a non-success status.
|
|
247
|
+
GlpiProtocolError
|
|
248
|
+
When the legacy API answers that the update was refused.
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
v1 = self._require_v1_session(_V1_GROUP_FEATURE_LABEL)
|
|
252
|
+
payload = await v1.request_json(
|
|
253
|
+
"PUT",
|
|
254
|
+
f"Group/{group_id}",
|
|
255
|
+
json_body={"input": {"id": int(group_id), "is_recursive": int(recursive)}},
|
|
256
|
+
failure_message=f"Failed to set is_recursive on group {group_id}",
|
|
257
|
+
)
|
|
258
|
+
ensure_v1_write_accepted(payload, f"Setting is_recursive on group {group_id}")
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
__all__ = ["GroupMixin"]
|
|
@@ -11,14 +11,24 @@ from __future__ import annotations
|
|
|
11
11
|
from collections.abc import AsyncIterator
|
|
12
12
|
|
|
13
13
|
from glpi_python_client._async.clients.commons._constants import USER_ENDPOINT, GlpiId
|
|
14
|
+
from glpi_python_client._async.clients.commons._payloads import (
|
|
15
|
+
ensure_v1_write_accepted,
|
|
16
|
+
model_from_payload,
|
|
17
|
+
)
|
|
14
18
|
from glpi_python_client._async.clients.commons._transport import TransportMixin
|
|
15
|
-
from glpi_python_client._errors import GlpiValidationError
|
|
19
|
+
from glpi_python_client._errors import GlpiProtocolError, GlpiValidationError
|
|
16
20
|
from glpi_python_client.models.api_schema.administration._user import (
|
|
17
21
|
DeleteUser,
|
|
18
22
|
GetUser,
|
|
19
23
|
PatchUser,
|
|
20
24
|
PostUser,
|
|
21
25
|
)
|
|
26
|
+
from glpi_python_client.models.api_schema.administration._user_authorization import (
|
|
27
|
+
UserAuthorization,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
_V1_AUTHORIZATION_FEATURE_LABEL = "user authorizations"
|
|
31
|
+
_AUTHORIZATION_LIST_RANGE = "0-999"
|
|
22
32
|
|
|
23
33
|
|
|
24
34
|
class UserMixin(TransportMixin):
|
|
@@ -296,5 +306,208 @@ class UserMixin(TransportMixin):
|
|
|
296
306
|
delete_model_cls=DeleteUser,
|
|
297
307
|
)
|
|
298
308
|
|
|
309
|
+
async def list_user_authorizations(
|
|
310
|
+
self, user_id: GlpiId
|
|
311
|
+
) -> list[UserAuthorization]:
|
|
312
|
+
"""List the profile/entity authorizations of one user.
|
|
313
|
+
|
|
314
|
+
The GLPI v2 API has no route for a user's authorizations
|
|
315
|
+
(``Profile_User`` rows), so this helper reads them through the legacy
|
|
316
|
+
v1 API, which requires ``v1_base_url``/``v1_user_token`` to be
|
|
317
|
+
configured (pointing at the legacy ``apirest.php``).
|
|
318
|
+
|
|
319
|
+
Parameters
|
|
320
|
+
----------
|
|
321
|
+
user_id : int | str
|
|
322
|
+
Identifier of the user.
|
|
323
|
+
|
|
324
|
+
Returns
|
|
325
|
+
-------
|
|
326
|
+
list[UserAuthorization]
|
|
327
|
+
One entry per profile the user holds, with the entity it applies
|
|
328
|
+
to and whether it is recursive.
|
|
329
|
+
|
|
330
|
+
Raises
|
|
331
|
+
------
|
|
332
|
+
RuntimeError
|
|
333
|
+
When no legacy v1 session is configured on the client.
|
|
334
|
+
GlpiStatusError
|
|
335
|
+
When the legacy API returns a non-success status.
|
|
336
|
+
"""
|
|
337
|
+
|
|
338
|
+
v1 = self._require_v1_session(_V1_AUTHORIZATION_FEATURE_LABEL)
|
|
339
|
+
payload = await v1.request_json(
|
|
340
|
+
"GET",
|
|
341
|
+
f"User/{user_id}/Profile_User",
|
|
342
|
+
params={"range": _AUTHORIZATION_LIST_RANGE},
|
|
343
|
+
failure_message=f"Failed to list the authorizations of user {user_id}",
|
|
344
|
+
)
|
|
345
|
+
rows = payload if isinstance(payload, list) else []
|
|
346
|
+
return [
|
|
347
|
+
model_from_payload(
|
|
348
|
+
UserAuthorization, row, server_timezone=self.server_timezone
|
|
349
|
+
)
|
|
350
|
+
for row in rows
|
|
351
|
+
]
|
|
352
|
+
|
|
353
|
+
async def add_user_authorization(
|
|
354
|
+
self,
|
|
355
|
+
user_id: GlpiId,
|
|
356
|
+
*,
|
|
357
|
+
profile_id: GlpiId,
|
|
358
|
+
entity_id: GlpiId,
|
|
359
|
+
recursive: bool = False,
|
|
360
|
+
) -> int:
|
|
361
|
+
"""Give one user a profile in an entity.
|
|
362
|
+
|
|
363
|
+
The GLPI v2 API has no route for a user's authorizations, so this
|
|
364
|
+
helper creates the ``Profile_User`` row through the legacy v1 API,
|
|
365
|
+
which requires ``v1_base_url``/``v1_user_token`` to be configured.
|
|
366
|
+
|
|
367
|
+
Parameters
|
|
368
|
+
----------
|
|
369
|
+
user_id : int | str
|
|
370
|
+
Identifier of the user.
|
|
371
|
+
profile_id : int | str
|
|
372
|
+
Identifier of the profile to grant.
|
|
373
|
+
entity_id : int | str
|
|
374
|
+
Identifier of the entity the profile applies to.
|
|
375
|
+
recursive : bool, optional
|
|
376
|
+
Whether the profile also applies to the sub-entities of
|
|
377
|
+
``entity_id``. ``False`` by default.
|
|
378
|
+
|
|
379
|
+
Returns
|
|
380
|
+
-------
|
|
381
|
+
int
|
|
382
|
+
Identifier of the new authorization.
|
|
383
|
+
|
|
384
|
+
Raises
|
|
385
|
+
------
|
|
386
|
+
RuntimeError
|
|
387
|
+
When no legacy v1 session is configured on the client.
|
|
388
|
+
GlpiStatusError
|
|
389
|
+
When the legacy API returns a non-success status.
|
|
390
|
+
GlpiProtocolError
|
|
391
|
+
When the answer carries no authorization id.
|
|
392
|
+
"""
|
|
393
|
+
|
|
394
|
+
v1 = self._require_v1_session(_V1_AUTHORIZATION_FEATURE_LABEL)
|
|
395
|
+
payload = await v1.request_json(
|
|
396
|
+
"POST",
|
|
397
|
+
"Profile_User",
|
|
398
|
+
json_body={
|
|
399
|
+
"input": {
|
|
400
|
+
"users_id": int(user_id),
|
|
401
|
+
"profiles_id": int(profile_id),
|
|
402
|
+
"entities_id": int(entity_id),
|
|
403
|
+
"is_recursive": int(recursive),
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
failure_message=(
|
|
407
|
+
f"Failed to add profile {profile_id} in entity {entity_id} "
|
|
408
|
+
f"to user {user_id}"
|
|
409
|
+
),
|
|
410
|
+
)
|
|
411
|
+
new_id = payload.get("id") if isinstance(payload, dict) else None
|
|
412
|
+
if new_id is None or isinstance(new_id, bool):
|
|
413
|
+
raise GlpiProtocolError(
|
|
414
|
+
f"GLPI user authorization response missing id: {payload!r}"
|
|
415
|
+
)
|
|
416
|
+
try:
|
|
417
|
+
return int(new_id)
|
|
418
|
+
except (TypeError, ValueError) as exc:
|
|
419
|
+
raise GlpiProtocolError(
|
|
420
|
+
f"GLPI user authorization response has a non-numeric id: {payload!r}"
|
|
421
|
+
) from exc
|
|
422
|
+
|
|
423
|
+
async def update_user_authorization(
|
|
424
|
+
self,
|
|
425
|
+
authorization_id: GlpiId,
|
|
426
|
+
*,
|
|
427
|
+
entity_id: GlpiId | None = None,
|
|
428
|
+
recursive: bool | None = None,
|
|
429
|
+
) -> None:
|
|
430
|
+
"""Move or re-scope one user authorization.
|
|
431
|
+
|
|
432
|
+
The GLPI v2 API has no route for a user's authorizations, so this
|
|
433
|
+
helper updates the ``Profile_User`` row through the legacy v1 API,
|
|
434
|
+
which requires ``v1_base_url``/``v1_user_token`` to be configured.
|
|
435
|
+
Only the supplied arguments are changed.
|
|
436
|
+
|
|
437
|
+
Parameters
|
|
438
|
+
----------
|
|
439
|
+
authorization_id : int | str
|
|
440
|
+
Identifier of the authorization (see
|
|
441
|
+
:meth:`list_user_authorizations`).
|
|
442
|
+
entity_id : int | str | None, optional
|
|
443
|
+
New entity for the authorization.
|
|
444
|
+
recursive : bool | None, optional
|
|
445
|
+
New recursion flag.
|
|
446
|
+
|
|
447
|
+
Raises
|
|
448
|
+
------
|
|
449
|
+
GlpiValidationError
|
|
450
|
+
When neither ``entity_id`` nor ``recursive`` is given; no request
|
|
451
|
+
is sent.
|
|
452
|
+
RuntimeError
|
|
453
|
+
When no legacy v1 session is configured on the client.
|
|
454
|
+
GlpiStatusError
|
|
455
|
+
When the legacy API returns a non-success status.
|
|
456
|
+
GlpiProtocolError
|
|
457
|
+
When the legacy API answers that the update was refused.
|
|
458
|
+
"""
|
|
459
|
+
|
|
460
|
+
if entity_id is None and recursive is None:
|
|
461
|
+
raise GlpiValidationError(
|
|
462
|
+
"update_user_authorization needs entity_id, recursive or both."
|
|
463
|
+
)
|
|
464
|
+
v1 = self._require_v1_session(_V1_AUTHORIZATION_FEATURE_LABEL)
|
|
465
|
+
fields: dict[str, object] = {"id": int(authorization_id)}
|
|
466
|
+
if entity_id is not None:
|
|
467
|
+
fields["entities_id"] = int(entity_id)
|
|
468
|
+
if recursive is not None:
|
|
469
|
+
fields["is_recursive"] = int(recursive)
|
|
470
|
+
payload = await v1.request_json(
|
|
471
|
+
"PUT",
|
|
472
|
+
f"Profile_User/{authorization_id}",
|
|
473
|
+
json_body={"input": fields},
|
|
474
|
+
failure_message=f"Failed to update user authorization {authorization_id}",
|
|
475
|
+
)
|
|
476
|
+
ensure_v1_write_accepted(
|
|
477
|
+
payload, f"Updating user authorization {authorization_id}"
|
|
478
|
+
)
|
|
479
|
+
|
|
480
|
+
async def delete_user_authorization(self, authorization_id: GlpiId) -> None:
|
|
481
|
+
"""Remove one user authorization.
|
|
482
|
+
|
|
483
|
+
The GLPI v2 API has no route for a user's authorizations, so this
|
|
484
|
+
helper deletes the ``Profile_User`` row through the legacy v1 API,
|
|
485
|
+
which requires ``v1_base_url``/``v1_user_token`` to be configured.
|
|
486
|
+
|
|
487
|
+
Parameters
|
|
488
|
+
----------
|
|
489
|
+
authorization_id : int | str
|
|
490
|
+
Identifier of the authorization to remove.
|
|
491
|
+
|
|
492
|
+
Raises
|
|
493
|
+
------
|
|
494
|
+
RuntimeError
|
|
495
|
+
When no legacy v1 session is configured on the client.
|
|
496
|
+
GlpiStatusError
|
|
497
|
+
When the legacy API returns a non-success status.
|
|
498
|
+
GlpiProtocolError
|
|
499
|
+
When the legacy API answers that the deletion was refused.
|
|
500
|
+
"""
|
|
501
|
+
|
|
502
|
+
v1 = self._require_v1_session(_V1_AUTHORIZATION_FEATURE_LABEL)
|
|
503
|
+
payload = await v1.request_json(
|
|
504
|
+
"DELETE",
|
|
505
|
+
f"Profile_User/{authorization_id}",
|
|
506
|
+
failure_message=f"Failed to delete user authorization {authorization_id}",
|
|
507
|
+
)
|
|
508
|
+
ensure_v1_write_accepted(
|
|
509
|
+
payload, f"Deleting user authorization {authorization_id}"
|
|
510
|
+
)
|
|
511
|
+
|
|
299
512
|
|
|
300
513
|
__all__ = ["UserMixin"]
|
|
@@ -25,6 +25,7 @@ from glpi_python_client._async.clients.api import (
|
|
|
25
25
|
DocumentMixin,
|
|
26
26
|
EntityMixin,
|
|
27
27
|
FollowupMixin,
|
|
28
|
+
GroupMixin,
|
|
28
29
|
KBArticleCommentMixin,
|
|
29
30
|
KBArticleMixin,
|
|
30
31
|
KBArticleRevisionMixin,
|
|
@@ -59,6 +60,7 @@ class AsyncGlpiClient(
|
|
|
59
60
|
ComputerMixin,
|
|
60
61
|
UserMixin,
|
|
61
62
|
EntityMixin,
|
|
63
|
+
GroupMixin,
|
|
62
64
|
LocationMixin,
|
|
63
65
|
ContractTypeMixin,
|
|
64
66
|
KBCategoryMixin,
|
|
@@ -19,6 +19,7 @@ COMPUTER_ENDPOINT = "Assets/Computer"
|
|
|
19
19
|
# administration/
|
|
20
20
|
USER_ENDPOINT = "Administration/User"
|
|
21
21
|
ENTITY_ENDPOINT = "Administration/Entity"
|
|
22
|
+
GROUP_ENDPOINT = "Administration/Group"
|
|
22
23
|
|
|
23
24
|
# dropdowns/
|
|
24
25
|
CONTRACT_TYPE_ENDPOINT = "Dropdowns/ContractType"
|
|
@@ -52,6 +53,7 @@ __all__ = [
|
|
|
52
53
|
"DOCUMENT_ENDPOINT",
|
|
53
54
|
"ENTITY_ENDPOINT",
|
|
54
55
|
"FOLLOWUP_SUFFIX",
|
|
56
|
+
"GROUP_ENDPOINT",
|
|
55
57
|
"KB_ARTICLE_ENDPOINT",
|
|
56
58
|
"KB_CATEGORY_ENDPOINT",
|
|
57
59
|
"KB_COMMENT_SUFFIX",
|
|
@@ -10,6 +10,7 @@ from __future__ import annotations
|
|
|
10
10
|
from datetime import tzinfo
|
|
11
11
|
from typing import TypeVar
|
|
12
12
|
|
|
13
|
+
from glpi_python_client._errors import GlpiProtocolError
|
|
13
14
|
from glpi_python_client.models._base import (
|
|
14
15
|
SERVER_TIMEZONE_CONTEXT_KEY,
|
|
15
16
|
GlpiModel,
|
|
@@ -94,4 +95,37 @@ def model_from_payload(
|
|
|
94
95
|
return model_class.model_validate(payload, context=context)
|
|
95
96
|
|
|
96
97
|
|
|
97
|
-
__all__ = ["model_from_payload", "model_to_payload"]
|
|
98
|
+
__all__ = ["ensure_v1_write_accepted", "model_from_payload", "model_to_payload"]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def ensure_v1_write_accepted(payload: object, description: str) -> None:
|
|
102
|
+
"""Raise when a legacy v1 write answered ``false`` for an item.
|
|
103
|
+
|
|
104
|
+
The v1 API reports a refused write with a success HTTP status and a
|
|
105
|
+
body such as ``[{"<id>": false, "message": "..."}]``, so the status
|
|
106
|
+
check alone lets it pass. An empty body, or a body without any
|
|
107
|
+
``false`` entry, is accepted.
|
|
108
|
+
|
|
109
|
+
Parameters
|
|
110
|
+
----------
|
|
111
|
+
payload : object
|
|
112
|
+
Parsed JSON body returned by the v1 session.
|
|
113
|
+
description : str
|
|
114
|
+
What was attempted, used as the prefix of the error message.
|
|
115
|
+
|
|
116
|
+
Raises
|
|
117
|
+
------
|
|
118
|
+
GlpiProtocolError
|
|
119
|
+
When any item of the answer is ``false``; the message carries the
|
|
120
|
+
server's own explanation when it gave one.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
if not isinstance(payload, list):
|
|
124
|
+
return
|
|
125
|
+
for entry in payload:
|
|
126
|
+
if not isinstance(entry, dict):
|
|
127
|
+
continue
|
|
128
|
+
if any(value is False for key, value in entry.items() if key != "message"):
|
|
129
|
+
reason = entry.get("message")
|
|
130
|
+
suffix = f": {reason}" if reason else ""
|
|
131
|
+
raise GlpiProtocolError(f"{description} was refused by GLPI{suffix}")
|