mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-30 12:46:39 +00:00
feat: add group manager for update_user
This commit is contained in:
parent
65082e53fb
commit
7a5e33c1e6
|
@ -5,7 +5,7 @@ import uuid
|
||||||
import logging
|
import logging
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from selfprivacy_api import PLEASE_UPDATE_APP_TEXT
|
from selfprivacy_api.utils.strings import PLEASE_UPDATE_APP_TEXT
|
||||||
from selfprivacy_api.models.group import Group
|
from selfprivacy_api.models.group import Group
|
||||||
from selfprivacy_api.models.user import UserDataUser, UserDataUserOrigin
|
from selfprivacy_api.models.user import UserDataUser, UserDataUserOrigin
|
||||||
|
|
||||||
|
@ -16,7 +16,6 @@ from selfprivacy_api.actions.ssh import get_ssh_keys
|
||||||
from selfprivacy_api.repositories.users.json_user_repository import JsonUserRepository
|
from selfprivacy_api.repositories.users.json_user_repository import JsonUserRepository
|
||||||
from selfprivacy_api.repositories.users import ACTIVE_USERS_PROVIDER
|
from selfprivacy_api.repositories.users import ACTIVE_USERS_PROVIDER
|
||||||
from selfprivacy_api.repositories.users.exceptions import (
|
from selfprivacy_api.repositories.users.exceptions import (
|
||||||
DisplaynameNotAlphanumeric,
|
|
||||||
DisplaynameTooLong,
|
DisplaynameTooLong,
|
||||||
SelfPrivacyAppIsOutdate,
|
SelfPrivacyAppIsOutdate,
|
||||||
UserIsProtected,
|
UserIsProtected,
|
||||||
|
@ -144,13 +143,26 @@ def update_user(
|
||||||
raise UserIsProtected
|
raise UserIsProtected
|
||||||
|
|
||||||
if (
|
if (
|
||||||
displayname and len(displayname) >= 32
|
displayname and len(displayname) >= 512
|
||||||
): # we don't know the limitations of each service
|
): # we don't know the limitations of each service
|
||||||
raise DisplaynameTooLong
|
raise DisplaynameTooLong
|
||||||
|
|
||||||
|
if directmemberof:
|
||||||
|
user = ACTIVE_USERS_PROVIDER.get_user_by_username(username=username)
|
||||||
|
|
||||||
|
groups_to_add = [item for item in directmemberof if item not in user.directmemberof] # type: ignore
|
||||||
|
groups_to_delete = [item for item in user.directmemberof if item not in directmemberof] # type: ignore
|
||||||
|
|
||||||
|
if groups_to_add:
|
||||||
|
for group in groups_to_add:
|
||||||
|
ACTIVE_USERS_PROVIDER.add_users_to_group(group_name=group, users=[username])
|
||||||
|
|
||||||
|
if groups_to_delete:
|
||||||
|
for group in groups_to_delete:
|
||||||
|
ACTIVE_USERS_PROVIDER.remove_users_from_group(group_name=group, users=[username])
|
||||||
|
|
||||||
ACTIVE_USERS_PROVIDER.update_user(
|
ACTIVE_USERS_PROVIDER.update_user(
|
||||||
username=username,
|
username=username,
|
||||||
directmemberof=directmemberof,
|
|
||||||
displayname=displayname,
|
displayname=displayname,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,6 @@ class AbstractUserRepository(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def update_user(
|
def update_user(
|
||||||
username: str,
|
username: str,
|
||||||
directmemberof: Optional[list[str]] = None,
|
|
||||||
displayname: Optional[str] = None,
|
displayname: Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -406,7 +406,6 @@ class KanidmUserRepository(AbstractUserRepository):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_user(
|
def update_user(
|
||||||
username: str,
|
username: str,
|
||||||
directmemberof: Optional[list[str]] = None,
|
|
||||||
displayname: Optional[str] = None,
|
displayname: Optional[str] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -433,9 +432,6 @@ class KanidmUserRepository(AbstractUserRepository):
|
||||||
if displayname:
|
if displayname:
|
||||||
data["attrs"]["displayname"] = [displayname]
|
data["attrs"]["displayname"] = [displayname]
|
||||||
|
|
||||||
if directmemberof:
|
|
||||||
data["attrs"]["directmemberof"] = directmemberof
|
|
||||||
|
|
||||||
KanidmUserRepository._send_query(
|
KanidmUserRepository._send_query(
|
||||||
endpoint=f"person/{username}",
|
endpoint=f"person/{username}",
|
||||||
method="PATCH",
|
method="PATCH",
|
||||||
|
@ -589,6 +585,6 @@ class KanidmUserRepository(AbstractUserRepository):
|
||||||
|
|
||||||
KanidmUserRepository._send_query(
|
KanidmUserRepository._send_query(
|
||||||
endpoint=f"group/{group_name}/_attr/member",
|
endpoint=f"group/{group_name}/_attr/member",
|
||||||
method="POST",
|
method="DELETE",
|
||||||
data=data,
|
data=data,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue