Skip to content
Snippets Groups Projects
  • Andreas Ellewsen's avatar
    a3a7410d
    Add identity verification to frontend · a3a7410d
    Andreas Ellewsen authored
    The profile page of a guest now shows a verification button if the guest
    has a passport or national identificaiton number that has not been
    verified. Clicking the button shows a dialog, with a confirmation button
    which triggers a PATCH request to the backend and reloads the page when
    it returns.
    
    Resolves: GREG-101
    Add identity verification to frontend
    Andreas Ellewsen authored
    The profile page of a guest now shows a verification button if the guest
    has a passport or national identificaiton number that has not been
    verified. Clicking the button shows a dialog, with a confirmation button
    which triggers a PATCH request to the backend and reloads the page when
    it returns.
    
    Resolves: GREG-101
views.py 5.09 KiB
from django.contrib.auth import logout
from django.http import JsonResponse
from django.middleware.csrf import get_token
from django.shortcuts import redirect
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView

from greg.models import Person, Sponsor
from greg.permissions import IsSponsor
from gregui import mailutils
from gregui.models import GregUserProfile


def get_csrf(request):
    response = JsonResponse({"detail": "CSRF cookie set"})
    response["X-CSRFToken"] = get_token(request)
    return response


def logout_view(request):
    if not request.user.is_authenticated:
        return JsonResponse({"detail": "You're not logged in."}, status=400)

    logout(request)
    return JsonResponse({"detail": "Successfully logged out."})


def login_view(request):
    """
    View for pointing login links to

    Sesame will take the query string automatically and use it to create a session for
    the user, so all this needs to do is redirect the user wherever they're supposed to
    go after successfully logging in.
    """
    # TODO: redirect to whatever path the frontend ends up living at (prob '/')
    return redirect("/api/ui/v1/whoami/")


def send_test_email(request):
    mailutils.send_registration_mail("test@example.no", "Foo Bar")
    return JsonResponse({"detail": "Created task to send test mail."})


class SessionView(APIView):
    authentication_classes = [SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated]

    @staticmethod
    # pylint: disable=W0622
    def get(request, format=None):
        return JsonResponse({"isAuthenticated": True})


class WhoAmIView(APIView):
    authentication_classes = [SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated]

    @staticmethod
    # pylint: disable=W0622
    def get(request, format=None):
        return JsonResponse({"username": request.user.username})


class OusView(APIView):
    """Fetch Ous related to the authenticated sponsor."""

    authentication_classes = [SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated, IsSponsor]

    @staticmethod
    # pylint: disable=W0622
    def get(request, format=None):
        profile = GregUserProfile.objects.get(user=request.user)
        sponsor = Sponsor.objects.get(id=profile.sponsor.id)
        return JsonResponse(
            {
                "ous": [
                    {"id": i.id, "nb": i.name_nb, "en": i.name_en}
                    for i in sponsor.units.all()
                ]
            }
        )


class GuestInfoView(APIView):
    """Fetch all the sponsors guests"""

    authentication_classes = [SessionAuthentication, BasicAuthentication]
    permission_classes = [IsAuthenticated, IsSponsor]

    @staticmethod
    # pylint: disable=W0622
    def get(request, format=None):
        user = GregUserProfile.objects.get(user=request.user)

        return JsonResponse(
            {
                "persons": [
                    {
                        "pid": person.id,
                        "first": person.first_name,
                        "last": person.last_name,
                        "email": person.private_email and person.private_email.value,
                        "mobile": person.private_mobile and person.private_mobile.value,
                        "fnr": person.fnr
                        and {
                            "id": person.fnr.id,
                            "value": "".join((person.fnr.value[:-5], "*****")),
                            "type": person.fnr.type,
                            "verified_at": person.fnr.verified_at,
                        },
                        "passport": person.passport
                        and {
                            "id": person.passport.id,
                            "value": person.passport.value,
                            "type": person.passport.type,
                            "verified_at": person.passport.verified_at,
                        },
                        "active": person.is_registered and person.is_verified,
                        "registered": person.is_registered,
                        "verified": person.is_verified,
                        "roles": [
                            {
                                "id": role.id,
                                "name_nb": role.type.name_nb,
                                "name_en": role.type.name_en,
                                "ou_nb": role.orgunit.name_nb,
                                "ou_en": role.orgunit.name_en,
                                "start_date": role.start_date,
                                "end_date": role.end_date,
                                "max_days": role.type.max_days,
                            }
                            for role in person.roles.all()
                        ],
                    }
                    for person in Person.objects.filter(
                        roles__sponsor=user.sponsor
                    ).distinct()
                ]
            }
        )