From db543f6fe976d941a47f7468ef21fe05b05e5805 Mon Sep 17 00:00:00 2001 From: Aleksey Date: Mon, 2 Dec 2024 16:51:52 +0300 Subject: [PATCH] Update services.py add admin --- .../services.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/back/taiga_contrib_access_token_auth/services.py b/back/taiga_contrib_access_token_auth/services.py index 333e696..a81238a 100644 --- a/back/taiga_contrib_access_token_auth/services.py +++ b/back/taiga_contrib_access_token_auth/services.py @@ -15,8 +15,12 @@ FILTER_GROUPS = settings.FILTER_GROUPS PROJECTS = settings.PROJECTS DEFAULT_ROLE = settings.DEFAULT_ROLE +ADMIN_GROUP = settings.ADMIN_GROUP # Добавляем переменную окружения для группы администраторов def determine_role_and_project(groups): + """ + Определяет роль и проект на основе групп пользователя. + """ for group in groups: parts = group.split(':') if len(parts) == 2: @@ -76,12 +80,21 @@ def access_token_register( name=role ) - membership_model.objects.get_or_create( + # Проверка на наличие группы администраторов + is_admin = ADMIN_GROUP in groups + + membership, created = membership_model.objects.get_or_create( user=user, project=project, - role=role + defaults={'role': role, 'is_admin': is_admin} ) - logger.info(f"Role assigned to user: {email}, role: {role}, project_id: {project.id}") + + # Обновление статуса администратора, если пользователь ранее не был админом + if not created and is_admin and not membership.is_admin: + membership.is_admin = True + membership.save() + + logger.info(f"Role assigned to user: {email}, role: {role}, project_id: {project.id}, is_admin: {is_admin}") return user