glpi-python-client 0.6.6__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.
@@ -130,6 +130,7 @@ from glpi_python_client.models import (
130
130
  PostTimelineDocument,
131
131
  PostUser,
132
132
  TicketMarkdownOptions,
133
+ UserAuthorization,
133
134
  )
134
135
  from glpi_python_client.rsql import (
135
136
  changed_since,
@@ -137,7 +138,7 @@ from glpi_python_client.rsql import (
137
138
  date_window,
138
139
  )
139
140
 
140
- __version__ = "0.6.6"
141
+ __version__ = "0.6.7"
141
142
 
142
143
  __all__ = [
143
144
  "AsyncGlpiClient",
@@ -250,6 +251,7 @@ __all__ = [
250
251
  "PostTimelineDocument",
251
252
  "PostUser",
252
253
  "TicketMarkdownOptions",
254
+ "UserAuthorization",
253
255
  "__version__",
254
256
  "changed_since",
255
257
  "created_between",
@@ -11,6 +11,9 @@ from __future__ import annotations
11
11
  from collections.abc import AsyncIterator
12
12
 
13
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
+ )
14
17
  from glpi_python_client._async.clients.commons._transport import TransportMixin
15
18
  from glpi_python_client.models.api_schema.administration._group import (
16
19
  DeleteGroup,
@@ -19,6 +22,8 @@ from glpi_python_client.models.api_schema.administration._group import (
19
22
  PostGroup,
20
23
  )
21
24
 
25
+ _V1_GROUP_FEATURE_LABEL = "group recursion"
26
+
22
27
 
23
28
  class GroupMixin(TransportMixin):
24
29
  """CRUD helpers for ``/Administration/Group``."""
@@ -214,5 +219,43 @@ class GroupMixin(TransportMixin):
214
219
  delete_model_cls=DeleteGroup,
215
220
  )
216
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
+
217
260
 
218
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"]
@@ -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}")
@@ -11,6 +11,9 @@ from __future__ import annotations
11
11
  from collections.abc import Iterator
12
12
 
13
13
  from glpi_python_client._sync.clients.commons._constants import GROUP_ENDPOINT, GlpiId
14
+ from glpi_python_client._sync.clients.commons._payloads import (
15
+ ensure_v1_write_accepted,
16
+ )
14
17
  from glpi_python_client._sync.clients.commons._transport import TransportMixin
15
18
  from glpi_python_client.models.api_schema.administration._group import (
16
19
  DeleteGroup,
@@ -19,6 +22,8 @@ from glpi_python_client.models.api_schema.administration._group import (
19
22
  PostGroup,
20
23
  )
21
24
 
25
+ _V1_GROUP_FEATURE_LABEL = "group recursion"
26
+
22
27
 
23
28
  class GroupMixin(TransportMixin):
24
29
  """CRUD helpers for ``/Administration/Group``."""
@@ -214,5 +219,43 @@ class GroupMixin(TransportMixin):
214
219
  delete_model_cls=DeleteGroup,
215
220
  )
216
221
 
222
+ 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 = 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
+
217
260
 
218
261
  __all__ = ["GroupMixin"]
@@ -11,14 +11,24 @@ from __future__ import annotations
11
11
  from collections.abc import Iterator
12
12
 
13
13
  from glpi_python_client._sync.clients.commons._constants import USER_ENDPOINT, GlpiId
14
+ from glpi_python_client._sync.clients.commons._payloads import (
15
+ ensure_v1_write_accepted,
16
+ model_from_payload,
17
+ )
14
18
  from glpi_python_client._sync.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
+ 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 = 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
+ 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 = 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
+ 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 = 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
+ 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 = 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"]
@@ -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}")
@@ -33,6 +33,7 @@ from glpi_python_client.models.api_schema.administration import (
33
33
  PostEntity,
34
34
  PostGroup,
35
35
  PostUser,
36
+ UserAuthorization,
36
37
  )
37
38
  from glpi_python_client.models.api_schema.assets import (
38
39
  DeleteComputer,
@@ -235,4 +236,5 @@ __all__ = [
235
236
  "PostTimelineDocument",
236
237
  "PostUser",
237
238
  "TicketMarkdownOptions",
239
+ "UserAuthorization",
238
240
  ]
@@ -24,6 +24,9 @@ from glpi_python_client.models.api_schema.administration._user import (
24
24
  PatchUser,
25
25
  PostUser,
26
26
  )
27
+ from glpi_python_client.models.api_schema.administration._user_authorization import (
28
+ UserAuthorization,
29
+ )
27
30
 
28
31
  __all__ = [
29
32
  "DeleteEntity",
@@ -38,4 +41,5 @@ __all__ = [
38
41
  "PostEntity",
39
42
  "PostGroup",
40
43
  "PostUser",
44
+ "UserAuthorization",
41
45
  ]
@@ -0,0 +1,48 @@
1
+ """Schema of one GLPI user authorization (``Profile_User`` row).
2
+
3
+ A user authorization says that a user holds a profile in an entity, and
4
+ whether that profile also applies to the entity's sub-entities. The GLPI v2
5
+ API has no route for these rows, so they are read and written through the
6
+ legacy v1 REST API, which names the columns ``users_id``, ``profiles_id`` and
7
+ ``entities_id`` and encodes flags as ``0``/``1``.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from pydantic import AliasChoices, Field
13
+
14
+ from glpi_python_client.models._base import GlpiModel
15
+
16
+
17
+ class UserAuthorization(GlpiModel):
18
+ """One profile held by a user in an entity.
19
+
20
+ Both the v1 column names (``users_id``, ``profiles_id``, ``entities_id``)
21
+ and the Python field names are accepted on input; the ``0``/``1`` flags
22
+ of the v1 API become booleans.
23
+
24
+ Parameters
25
+ ----------
26
+ id : int
27
+ Identifier of the authorization row.
28
+ user_id : int
29
+ Identifier of the user holding the profile.
30
+ profile_id : int
31
+ Identifier of the profile.
32
+ entity_id : int
33
+ Identifier of the entity the profile applies to.
34
+ is_recursive : bool, optional
35
+ Whether the profile also applies to the sub-entities of the entity.
36
+ is_dynamic : bool, optional
37
+ Whether the row was added by a directory rule rather than by hand.
38
+ is_default_profile : bool, optional
39
+ Whether this is the user's default profile.
40
+ """
41
+
42
+ id: int
43
+ user_id: int = Field(validation_alias=AliasChoices("user_id", "users_id"))
44
+ profile_id: int = Field(validation_alias=AliasChoices("profile_id", "profiles_id"))
45
+ entity_id: int = Field(validation_alias=AliasChoices("entity_id", "entities_id"))
46
+ is_recursive: bool = False
47
+ is_dynamic: bool = False
48
+ is_default_profile: bool = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: glpi-python-client
3
- Version: 0.6.6
3
+ Version: 0.6.7
4
4
  Summary: A typed Python client for GLPI ITSM APIs.
5
5
  Project-URL: Homepage, https://github.com/baraline/glpi_python_client
6
6
  Project-URL: Documentation, https://glpi-python-client.readthedocs.io/en/latest/
@@ -1,4 +1,4 @@
1
- glpi_python_client/__init__.py,sha256=PxyLPvdfRqFJPkX1RhaRjOJruLSmLJgW7M6_d2M634A,5762
1
+ glpi_python_client/__init__.py,sha256=dI8KsHQAL5wux1na4RbbJr5C5Hk5LcUm1D0gj33Ash8,5810
2
2
  glpi_python_client/_errors.py,sha256=Bc9UvlxulbdcGvzZIKXnJCTL78sW78R6EFg50P-1dpY,7151
3
3
  glpi_python_client/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  glpi_python_client/rsql.py,sha256=3u3lH3_-F1bvHbVzxALXTDPoHyoMATP1f4QPTar_sEU,8974
@@ -14,8 +14,8 @@ glpi_python_client/_async/clients/client.py,sha256=4lZbd4wHNRqI2xbzjsff3iy8e4FnM
14
14
  glpi_python_client/_async/clients/api/__init__.py,sha256=keInRT6ogmkeWETnNQC6VliBjZZk4ur2hRcIyWSv1_k,1776
15
15
  glpi_python_client/_async/clients/api/administration/__init__.py,sha256=raqpFI2U8WDfeSRzN8-PcqhzD3sKP0oTamgj77-Nhxk,614
16
16
  glpi_python_client/_async/clients/api/administration/_entity.py,sha256=qG1SBMwOCZWHjHV4KfswY6bTaTQP9I83mQvOd-Xo70w,6453
17
- glpi_python_client/_async/clients/api/administration/_group.py,sha256=uqx4Qqt7bWCYZlQayDIL5l_wW8kUg50ngxz3ktD771w,6511
18
- glpi_python_client/_async/clients/api/administration/_user.py,sha256=SQ_CM353cFD0BW0GQSQp__HZQlmJsAUuAiOgs-1NoNg,10085
17
+ glpi_python_client/_async/clients/api/administration/_group.py,sha256=c8qpqHl3METBggY5VJCAQyZNfmqBtOhi3IcxFK6eoMU,8175
18
+ glpi_python_client/_async/clients/api/administration/_user.py,sha256=lwwaMh0b8qP1V41xPQGa3lnoaLTy3vdboDE4abOiDpE,17681
19
19
  glpi_python_client/_async/clients/api/assets/__init__.py,sha256=WFWG5zcRf0kFtz35it3W83xOlqXJ77IcYI51KRSiL08,198
20
20
  glpi_python_client/_async/clients/api/assets/_computer.py,sha256=U-dDeYmY3MaS0BTqJAQThKtKTbvDS7BvshbXheD64rM,13634
21
21
  glpi_python_client/_async/clients/api/assistance/__init__.py,sha256=nL77QpQ4YzfdSWhkpUfcp5SwYlfjtONL37RgjU_OTuM,302
@@ -44,7 +44,7 @@ glpi_python_client/_async/clients/commons/_config.py,sha256=JlUZy_oPQj0ldSbBVWcy
44
44
  glpi_python_client/_async/clients/commons/_constants.py,sha256=Y2uQ0zE_7m0DLSKLg41JEbW4ww23Mk9g-hDbjNZjaZQ,1834
45
45
  glpi_python_client/_async/clients/commons/_filters.py,sha256=eQdrc06RhEuGHtvAqBJy1VrOVHVjeiFh82pvX-HxGw8,4695
46
46
  glpi_python_client/_async/clients/commons/_http.py,sha256=8x9H9eHw-neOYoQ2MAP2JmbVqcpno42pZlzfuhizpmk,15240
47
- glpi_python_client/_async/clients/commons/_payloads.py,sha256=7LEzk72WwLcLqxWXQ_rHcjAnpo3WIuq97X768-Zkvt8,3707
47
+ glpi_python_client/_async/clients/commons/_payloads.py,sha256=dtljozIhGB_wga3bLqT2hPNuV_ImX8EfHKv9CPV2EvY,4957
48
48
  glpi_python_client/_async/clients/commons/_transport.py,sha256=QhXpQK54DAYAXJ20n1Dg2targ5yThWL6OkivLI9uYfE,23515
49
49
  glpi_python_client/_async/clients/custom/__init__.py,sha256=Pi65Zs4lko41pk5L3vDqjv9b24OM6HDxBmQSGURhWP0,886
50
50
  glpi_python_client/_async/clients/custom/_statistics.py,sha256=GFgxHLWWZvQzHxl2bjerhlJDjuttvVa1aZIdaGIztg0,38751
@@ -61,8 +61,8 @@ glpi_python_client/_sync/clients/client.py,sha256=WTqrTs_0xSUSR8ez__JjHPgDBhsBUt
61
61
  glpi_python_client/_sync/clients/api/__init__.py,sha256=_nlTpSQyldJrTVvlifp__DQGF6_vqCl_YPWp6FCa-mM,1767
62
62
  glpi_python_client/_sync/clients/api/administration/__init__.py,sha256=3s3Nfawu1FI97Q-WIlz5OgLZL7TszItF6l_JzQHVzaM,611
63
63
  glpi_python_client/_sync/clients/api/administration/_entity.py,sha256=jYvIl1hXcPQuAABa0I0TCKBG6FMHtO8xl55xhG5CRS8,6369
64
- glpi_python_client/_sync/clients/api/administration/_group.py,sha256=84J78kE35pBhokqrRTMDj2H7yyU8UWvPfdbCcDLR8hY,6427
65
- glpi_python_client/_sync/clients/api/administration/_user.py,sha256=8QuL9G_6donw0UNtpQEyxh1gHpjfymnDcGy-7AW7IcA,9989
64
+ glpi_python_client/_sync/clients/api/administration/_group.py,sha256=uKIt5q-G2YmOQLMJWcjLkLTNYmFdUtB0gKGSgUvj9YQ,8078
65
+ glpi_python_client/_sync/clients/api/administration/_user.py,sha256=y2DHsMn029ofg4KJ62w6HGvBdtQJzs6dNhZUmIWpqxo,17536
66
66
  glpi_python_client/_sync/clients/api/assets/__init__.py,sha256=_4-S2nkXQNuFXzZzXoLR3DnykaKTzata9ZPN74ASO8s,197
67
67
  glpi_python_client/_sync/clients/api/assets/_computer.py,sha256=jILfoo7L2FJwL7Q_uFXkpfDBCuPczCAJ_TbAoyiGrj0,13490
68
68
  glpi_python_client/_sync/clients/api/assistance/__init__.py,sha256=dAXVQPgCmPhCLESRC8nkVvCKLwp655wu9nsyqmvzLDk,300
@@ -91,23 +91,24 @@ glpi_python_client/_sync/clients/commons/_config.py,sha256=f2wVtUUdh6rxaf-s84X8F
91
91
  glpi_python_client/_sync/clients/commons/_constants.py,sha256=Y2uQ0zE_7m0DLSKLg41JEbW4ww23Mk9g-hDbjNZjaZQ,1834
92
92
  glpi_python_client/_sync/clients/commons/_filters.py,sha256=eQdrc06RhEuGHtvAqBJy1VrOVHVjeiFh82pvX-HxGw8,4695
93
93
  glpi_python_client/_sync/clients/commons/_http.py,sha256=5p8S1J1usIB0WQg5u3h6zovr14an-NoIGNfwAHEOSS4,15239
94
- glpi_python_client/_sync/clients/commons/_payloads.py,sha256=7LEzk72WwLcLqxWXQ_rHcjAnpo3WIuq97X768-Zkvt8,3707
94
+ glpi_python_client/_sync/clients/commons/_payloads.py,sha256=dtljozIhGB_wga3bLqT2hPNuV_ImX8EfHKv9CPV2EvY,4957
95
95
  glpi_python_client/_sync/clients/commons/_transport.py,sha256=x9xRFSUrJLPyp1DrVh8Txyhr9DZmWkKXZ1FM9T6xjfo,23303
96
96
  glpi_python_client/_sync/clients/custom/__init__.py,sha256=sxfGlHVulucP7bacWhUMi9qGJZaiZm8lXceY0OGqWio,882
97
97
  glpi_python_client/_sync/clients/custom/_statistics.py,sha256=xrD5C99ieROJx60iUomBHacQ_49HpgQL4XjnfREUQvs,38616
98
98
  glpi_python_client/_sync/clients/custom/_ticket_context.py,sha256=LDUvKNr8VpZonF5bDo8QYYwMEsWmayVU28Ra3GonNmE,3004
99
99
  glpi_python_client/content/__init__.py,sha256=TaZPo8WSvVj4U-hSnxhUcqU8JnCKNZYpfQf4pH45aN4,424
100
100
  glpi_python_client/content/conversion.py,sha256=0dxQW0ojSo1KC8zcTFa476KQa1i3sLWzQ8gTEEVslys,45341
101
- glpi_python_client/models/__init__.py,sha256=7OxYvXSQc4QdK1VAQb_1BGvgwGc6211gKnSdLWy-qhY,5586
101
+ glpi_python_client/models/__init__.py,sha256=-t0GsZuXTp4te9bgvMxsSH-BhXCGl8lulw7-x7cwE4g,5634
102
102
  glpi_python_client/models/_base.py,sha256=pYg69KHuR5TDED5utr4Knr7HncBnLTxXHJZYf6vEUKA,12183
103
103
  glpi_python_client/models/api_schema/__init__.py,sha256=yTxUYih1Xy7NUogzD7UKp2b8HyE4wdeC_FCKS6ZRVVM,1176
104
104
  glpi_python_client/models/api_schema/_common.py,sha256=wVtaIkUM9iS3jyDb0bpFopEkP_3vP1ax7D8rwHe9QkI,2004
105
105
  glpi_python_client/models/api_schema/_content.py,sha256=DlsEmlXIK_0jyXh3j5PgU6vkwch2o0I8UpUkLq2fqYw,12826
106
106
  glpi_python_client/models/api_schema/enums.py,sha256=bHNCuumOXIZm80n5RqIhCzFqizII-3er41mn2aktF5s,5071
107
- glpi_python_client/models/api_schema/administration/__init__.py,sha256=CFZ36OIk1PPBzkXkCtOo2C6s7JfaKSp63Nw4-b_coxc,924
107
+ glpi_python_client/models/api_schema/administration/__init__.py,sha256=Hqgh6JZ0TQYVY2OHLK98sI0hh5VkZy4oJBrgyagNG4Y,1060
108
108
  glpi_python_client/models/api_schema/administration/_entity.py,sha256=KdbxMyPXumGZKmmabDIciF3nFS61pLRp5SBOTFa8slk,3297
109
109
  glpi_python_client/models/api_schema/administration/_group.py,sha256=8aWnDupTT9lxGtzMC_dAG8yDLO_NKtFz-OCNHOYESi0,5298
110
110
  glpi_python_client/models/api_schema/administration/_user.py,sha256=MQ6cHKdni98sjm-Whf0GKdGpNjPgGGJpUBgVjlGa19Q,15872
111
+ glpi_python_client/models/api_schema/administration/_user_authorization.py,sha256=-WC4YUuR9ebLkBBBUDgX626V-I9DNV6pPIfbgp_C6C0,1801
111
112
  glpi_python_client/models/api_schema/assets/__init__.py,sha256=QikGfQwwhjKt3vUd5FYC1F-BeClP5ByK1hahRRumdsA,562
112
113
  glpi_python_client/models/api_schema/assets/_computer.py,sha256=me3px9rkaN_ZataDVLjOer9wsZw_fRoRikxkLzXopsE,10304
113
114
  glpi_python_client/models/api_schema/assets/_contract_item.py,sha256=sS9SqdI47IGFaFBa5_8koOu26wut198-N-CG1OKbl7s,3430
@@ -139,7 +140,7 @@ glpi_python_client/models/custom_schema/_ticket_context.py,sha256=jnlfgho-aDbmA9
139
140
  glpi_python_client/testing/__init__.py,sha256=iLtUyJjyv3fEhy3PN25NxNTbtqDl3SoHLbNium0Z80U,568
140
141
  glpi_python_client/testing/fixtures.py,sha256=HKt6MXHgcmLn4L98pSwp-oGlc9C--_vrABNe5G_iBeg,1286
141
142
  glpi_python_client/testing/utils.py,sha256=JyO3w2d-0kYdtC1hEZ2Tu_YjnCz8nUQ_Ck05-1EhsX4,4404
142
- glpi_python_client-0.6.6.dist-info/METADATA,sha256=RiSWOUSsejQZVqs8hWndBh8TB9DPm07vq56KpoluRZo,8234
143
- glpi_python_client-0.6.6.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
144
- glpi_python_client-0.6.6.dist-info/licenses/LICENSE,sha256=nL8re-TIKkxCoL7OkSBja6EB4NsQR1HoOJgxv_MEhtc,1088
145
- glpi_python_client-0.6.6.dist-info/RECORD,,
143
+ glpi_python_client-0.6.7.dist-info/METADATA,sha256=3asw5YwL4G533S-Bdb677wMfE3H6ofnClEHKnIxOWvI,8234
144
+ glpi_python_client-0.6.7.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
145
+ glpi_python_client-0.6.7.dist-info/licenses/LICENSE,sha256=nL8re-TIKkxCoL7OkSBja6EB4NsQR1HoOJgxv_MEhtc,1088
146
+ glpi_python_client-0.6.7.dist-info/RECORD,,