2021-10-22 11:43:33 +02:00
|
|
|
from djeveric.views import ConfirmModelMixin
|
|
|
|
from rest_framework import mixins, status, viewsets
|
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-10-22 11:43:33 +02:00
|
|
|
from userausfall.models import MissingUserAttribute, PasswordMismatch, TrustBridge, User
|
2021-10-22 12:38:25 +02:00
|
|
|
from userausfall.rest_api.serializers import TrustBridgeSerializer, UserSerializer
|
2021-10-21 11:53:44 +02:00
|
|
|
from userausfall.views import get_authenticated_user
|
|
|
|
|
|
|
|
|
2021-10-22 11:43:33 +02:00
|
|
|
class TrustBridgeViewSet(
|
|
|
|
ConfirmModelMixin, mixins.CreateModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
|
|
|
|
):
|
|
|
|
queryset = TrustBridge.objects
|
2021-10-21 11:53:44 +02:00
|
|
|
serializer_class = TrustBridgeSerializer
|
|
|
|
|
2021-10-22 11:43:33 +02:00
|
|
|
def get_basic_queryset(self):
|
|
|
|
return self.queryset.filter(trust_taker=get_authenticated_user(self.request))
|
2021-04-12 11:16:56 +02:00
|
|
|
|
|
|
|
|
2021-10-22 12:38:25 +02:00
|
|
|
class UserViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
|
|
|
|
serializer_class = UserSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return User.objects.filter(pk=get_authenticated_user(self.request).pk)
|
|
|
|
|
2021-08-03 12:38:34 +02:00
|
|
|
@action(detail=True, methods=["post"])
|
2021-05-21 10:14:12 +02:00
|
|
|
def activate(self, request, pk=None):
|
|
|
|
"""Create the corresponding LDAP account."""
|
2021-08-03 12:38:34 +02:00
|
|
|
user: User = self.get_object()
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
2021-05-21 10:14:12 +02:00
|
|
|
if serializer.is_valid():
|
|
|
|
try:
|
2021-08-03 11:41:58 +02:00
|
|
|
# We prevent untrusted user accounts from being activated via API.
|
|
|
|
# They might be activated via Admin or programmatically.
|
|
|
|
if not user.trust_bridge.is_trusted:
|
|
|
|
raise MissingUserAttribute("User has no trusted trust bridge.")
|
2021-05-21 10:14:12 +02:00
|
|
|
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)
|