You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can use the users/register/ endpoint to create any user.
The view's permission classes is set to AllowAny.
The view checks the user making the request.
Anonymous
If the user is anonymous, then it checks if the role field is set to patient. If it is then save the user. If not, then the role can't be of any other type because then an anonymous user can create a doctor which is not allowed. The response returns a 401
role:str = serializer.validated_data.get("role")
# anonymous user can only register as a patient
if request.user.is_anonymous:
if role == CustomUser.PATIENT:
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response({"error_message": "Unauthorized request"}, status=status.HTTP_401_UNAUTHORIZED)
Systems Admin | SuperUser
If the user is admin or superuser then he can create any role
user:CustomUser = request.user
# sys admin
if (user.role == CustomUser.SYS_ADMIN and user_in_group(user, CustomUser.SYS_ADMIN)) or user.is_superuser == True:
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
Doctor
if the user is doctor, they can create user with any role except sys admin
if user.role == CustomUser.DOCTOR and user_in_group(user, CustomUser.DOCTOR):
# doctor cant create a system admin
if role != CustomUser.SYS_ADMIN:
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response({"error_message": "Unauthorized request"}, status=status.HTTP_401_UNAUTHORIZED)
Receptionist
If the user is receptionist, they can only create a patient
if user.role == CustomUser.RECEPTIONIST and user_in_group(user, CustomUser.RECEPTIONIST):
# receptionist can only create patient
if role == CustomUser.PATIENT:
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response({"error_message": "Unauthorized request"}, status=status.HTTP_401_UNAUTHORIZED)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Creating Users and Respective Roles
We have 6 roles
We can use the
users/register/
endpoint to create any user.The view's permission classes is set to AllowAny.
The view checks the user making the request.
Anonymous
If the user is anonymous, then it checks if the role field is set to patient. If it is then save the user. If not, then the role can't be of any other type because then an anonymous user can create a doctor which is not allowed. The response returns a 401
Systems Admin | SuperUser
If the user is admin or superuser then he can create any role
Doctor
if the user is doctor, they can create user with any role except sys admin
Receptionist
If the user is receptionist, they can only create a patient
LabTech
andNurse
cannot create any userBeta Was this translation helpful? Give feedback.
All reactions