2020-12-09 19:22:33 +00:00
|
|
|
from django.contrib.auth import views as auth_views
|
|
|
|
from django.contrib.auth import logout
|
|
|
|
|
2020-04-18 13:11:02 +00:00
|
|
|
from django_auth_ldap.backend import LDAPBackend
|
2020-11-14 09:50:43 +00:00
|
|
|
from rest_framework import mixins, permissions, status, viewsets
|
2020-04-18 13:11:02 +00:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2020-03-18 13:36:40 +00:00
|
|
|
|
2020-11-14 09:50:43 +00:00
|
|
|
from .serializers import *
|
|
|
|
|
2020-12-09 19:22:33 +00:00
|
|
|
class LoginView(auth_views.LoginView):
|
|
|
|
template_name = 'uncloud_auth/login.html'
|
|
|
|
|
|
|
|
class LogoutView(auth_views.LogoutView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-05-10 19:47:44 +00:00
|
|
|
class UserViewSet(viewsets.GenericViewSet):
|
2020-04-18 13:11:02 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2020-03-18 13:36:40 +00:00
|
|
|
serializer_class = UserSerializer
|
2020-04-18 13:11:02 +00:00
|
|
|
|
2020-05-10 19:47:44 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return self.request.user
|
|
|
|
|
2020-04-18 13:11:02 +00:00
|
|
|
def list(self, request, format=None):
|
|
|
|
# This is a bit stupid: we have a user, we create a queryset by
|
|
|
|
# matching on the username. But I don't know a "nicer" way.
|
|
|
|
# Nico, 2020-03-18
|
2020-05-10 19:47:44 +00:00
|
|
|
user = request.user
|
2020-04-18 13:11:02 +00:00
|
|
|
serializer = self.get_serializer(user, context = {'request': request})
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2020-11-14 09:50:43 +00:00
|
|
|
@action(detail=False, methods=['post'])
|
|
|
|
def change_email(self, request):
|
|
|
|
serializer = self.get_serializer(
|
|
|
|
request.user, data=request.data, context={'request': request}
|
|
|
|
)
|
2020-05-10 19:47:44 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2020-11-14 09:50:43 +00:00
|
|
|
|
|
|
|
class AccountManagementViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
|
|
|
|
serializer_class = UserRegistrationSerializer
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
self.perform_create(serializer)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
|
|
return Response(
|
|
|
|
serializer.data, status=status.HTTP_201_CREATED, headers=headers
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-18 13:11:02 +00:00
|
|
|
class AdminUserViewSet(viewsets.ReadOnlyModelViewSet):
|
2020-05-10 19:47:44 +00:00
|
|
|
permission_classes = [permissions.IsAdminUser]
|
2020-03-18 13:36:40 +00:00
|
|
|
|
2020-04-18 13:11:02 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.action == 'import_from_ldap':
|
|
|
|
return ImportUserSerializer
|
2020-03-18 13:36:40 +00:00
|
|
|
else:
|
2020-04-18 13:11:02 +00:00
|
|
|
return UserSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return get_user_model().objects.all()
|
|
|
|
|
|
|
|
@action(detail=False, methods=['post'], url_path='import_from_ldap')
|
|
|
|
def import_from_ldap(self, request, pk=None):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
ldap_username = serializer.validated_data.pop("username")
|
|
|
|
user = LDAPBackend().populate_user(ldap_username)
|
2020-03-18 13:36:40 +00:00
|
|
|
|
2020-04-18 13:11:02 +00:00
|
|
|
return Response(UserSerializer(user, context = {'request': request}).data)
|