Browse Source

Refactor user registration logic: add role handling in account adapter and improve serializer initialization

pull/2426/head
GONCALOUNI 6 months ago
parent
commit
6c9ba9aa36
3 changed files with 11 additions and 5 deletions
  1. 2
      backend/config/settings/base.py
  2. 8
      backend/users/adapters.py
  3. 6
      backend/users/custom_serializers.py

2
backend/config/settings/base.py

@ -137,6 +137,8 @@ ROLE_PROJECT_ADMIN = env("ROLE_PROJECT_ADMIN", "project_admin")
ROLE_ANNOTATOR = env("ROLE_ANNOTATOR", "annotator")
ROLE_ANNOTATION_APPROVER = env("ROLE_ANNOTATION_APPROVER", "annotation_approver")
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [

8
backend/users/adapters.py

@ -2,19 +2,19 @@ from allauth.account.adapter import DefaultAccountAdapter
class CustomAccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
password = form.cleaned_data.get('password1')
user = super().save_user(request, user, form, commit=False)
user.plain_password = password
role = form.cleaned_data.get("role") or (hasattr(request, 'data') and request.data.get("role")) or ""
print("CustomAccountAdapter role:", role)
print("CustomAccountAdapter form.cleaned_data:", form.cleaned_data)
role = form.cleaned_data.get("role", "")
if role == "admin":
user.is_staff = True
user.is_superuser = True
else:
user.is_staff = False
if commit:
user.save()
return user

6
backend/users/custom_serializers.py

@ -24,4 +24,8 @@ class CustomRegisterSerializer(RegisterSerializer):
else:
user.is_staff = False
user.save()
return user
return user
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print("CustomRegisterSerializer initialized")
Loading…
Cancel
Save