Fakultas Ilmu Komputer UI

Commit b2fcf71c authored by Kefas Satrio Bangkit Solideantyo's avatar Kefas Satrio Bangkit Solideantyo
Browse files

[GREEN] implement user role feature

parent 81f79bab
Pipeline #72166 passed with stage
in 5 minutes and 33 seconds
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import Group
from django.utils.translation import ugettext_lazy as _
......@@ -17,6 +18,12 @@ class CustomUserManager(BaseUserManager):
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
if not Group.objects.filter(name='client').exists():
client_role = Group.objects.create(name='client')
user.groups.add(client_role)
else:
client_role = Group.objects.get(name='client')
user.groups.add(client_role)
return user
def create_superuser(self, email, password, **extra_fields):
......
......@@ -26,4 +26,4 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
objects = CustomUserManager()
def __str__(self):
return self.email
return str(self.email)
from rest_framework import serializers
from rest_framework import serializers, exceptions
from dj_rest_auth.serializers import LoginSerializer
from .models import CustomUser
class CustomUserDetailsSerializer(serializers.ModelSerializer):
def to_representation(self, instance):
result = super().to_representation(instance)
if instance.groups.exists():
result['role'] = instance.groups.first().name
return result
class Meta:
model = CustomUser
fields = ('id', 'name', 'email',)
read_only_fields = ('id', 'email',)
class CustomLoginSerializer(LoginSerializer):
def validate_auth_user_status(self, user):
request = self.context.get('request')
if not request.data.get('role'):
msg = 'Role is missing from the payload.'
raise exceptions.ValidationError(msg)
if not user.groups.filter(name=request.data.get('role')).exists():
msg = 'Invalid role for the user.'
raise exceptions.ValidationError(msg)
def create(self, validated_data):
pass
def update(self, instance, validated_data):
pass
from rest_framework import routers
from django.urls import path, include
from .views import LinkUserAndDietProfileViewSet, GoogleView
from .views import LinkUserAndDietProfileViewSet, GoogleView, CustomLoginView
router = routers.SimpleRouter()
router.register('auth/link-user-and-diet-profile', LinkUserAndDietProfileViewSet, \
......@@ -9,6 +9,7 @@ router.register('auth/link-user-and-diet-profile', LinkUserAndDietProfileViewSet
urlpatterns = [
path('auth/registration/', include('dj_rest_auth.registration.urls')),
path('auth/', include('dj_rest_auth.urls')),
path('auth/user-login/', CustomLoginView.as_view(), name='custom_user_login'),
path('auth/google/', GoogleView.as_view(), name='google'),
]
......
......@@ -2,14 +2,18 @@ from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_simplejwt.tokens import RefreshToken
from dj_rest_auth.views import LoginView
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.hashers import make_password
from dietela_quiz.models import DietProfile
from dietela_quiz.serializers import DietProfileSerializer
from .models import CustomUser
from .serializers import CustomUserDetailsSerializer
from .serializers import CustomUserDetailsSerializer, CustomLoginSerializer
from .google_utils import validate_google_token
class CustomLoginView(LoginView):
serializer_class = CustomLoginSerializer
class LinkUserAndDietProfileViewSet(viewsets.ViewSet):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment