2021-05-21 10:14:12 +02:00
|
|
|
from rest_framework import viewsets, status
|
2021-05-18 12:07:03 +02:00
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
2021-04-12 11:16:56 +02:00
|
|
|
|
2021-05-25 10:40:38 +02:00
|
|
|
from djeveric import ConfirmationView
|
2021-05-21 10:14:12 +02:00
|
|
|
from userausfall.models import User, MissingUserAttribute, PasswordMismatch
|
2021-05-25 10:40:38 +02:00
|
|
|
from userausfall.confirmations import ConfidantConfirmation
|
2021-08-02 10:14:38 +02:00
|
|
|
from userausfall.rest_api.permissions import UserPermission
|
|
|
|
from userausfall.rest_api.serializers import UserSerializer, UserActivationSerializer, CreateUserSerializer
|
2021-04-12 11:16:56 +02:00
|
|
|
|
|
|
|
|
2021-05-25 10:40:38 +02:00
|
|
|
class ConfidantConfirmationView(ConfirmationView):
|
|
|
|
confirmation_class = ConfidantConfirmation
|
|
|
|
|
|
|
|
|
2021-05-18 12:07:03 +02:00
|
|
|
class UserViewSet(viewsets.ModelViewSet):
|
2021-08-02 14:25:58 +02:00
|
|
|
# permission_classes = [UserPermission]
|
2021-05-21 10:14:12 +02:00
|
|
|
queryset = User.objects.all()
|
2021-05-18 12:07:03 +02:00
|
|
|
|
2021-08-02 14:25:58 +02:00
|
|
|
@action(detail=False, methods=["post"])
|
2021-05-21 10:14:12 +02:00
|
|
|
def activate(self, request, pk=None):
|
|
|
|
"""Create the corresponding LDAP account."""
|
2021-08-02 14:25:58 +02:00
|
|
|
user: User = request.user # self.get_object()
|
2021-05-21 10:14:12 +02:00
|
|
|
serializer = UserActivationSerializer(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
try:
|
|
|
|
user.create_ldap_account(serializer.validated_data["password"])
|
|
|
|
except (MissingUserAttribute, PasswordMismatch) as e:
|
|
|
|
return Response({"message": str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
else:
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
2021-08-02 10:14:38 +02:00
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.action == "create":
|
|
|
|
return CreateUserSerializer
|