feat(dgsi): Add a basic profile view

This commit is contained in:
Tom Hubrecht 2024-09-11 21:08:08 +02:00
parent 772ca45292
commit 8599992dd7
Signed by: thubrecht
SSH key fingerprint: SHA256:r+nK/SIcWlJ0zFZJGHtlAoRwq1Rm+WcKAm5ADYMoQPc
6 changed files with 78 additions and 2 deletions

1
.credentials/KANIDM_URI Normal file
View file

@ -0,0 +1 @@
https://sso.dgnum.eu

View file

@ -1 +1,9 @@
urlpatterns = []
from django.urls import path
from . import views
app_name = "dgsi"
urlpatterns = [
path("profile", views.ProfileView.as_view(), name="dgn-profile"),
]

View file

@ -1 +1,35 @@
# Create your views here.
import json
from typing import Optional
from asgiref.sync import async_to_sync
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import QuerySet
from django.views.generic import DetailView
from shared.kanidm import client
User = get_user_model()
class ProfileView(LoginRequiredMixin, DetailView):
model = User
template_name = "account/profile.html"
def get_object(self, queryset: Optional[QuerySet] = None):
assert isinstance(self.request.user, User)
return self.request.user
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
username = self.request.user.get_username()
ctx["person"] = async_to_sync(client.person_account_get)(username)
content: str = async_to_sync(client.get_radius_token)(username).content
ctx["radius_secret"] = json.loads(content).get("secret")
return ctx

8
src/shared/kanidm.py Normal file
View file

@ -0,0 +1,8 @@
from kanidm import KanidmClient
from loadcredential import Credentials
credentials = Credentials(env_prefix="DGSI_")
client = KanidmClient(
uri=credentials["KANIDM_URI"], token=credentials["KANIDM_AUTH_TOKEN"]
)

View file

@ -8,7 +8,7 @@
<h1 class="title">
<a href="{% url 'index' %}" class="has-text-dark">Dossier Général des Services Informagiques</a>
</h1>
<h2 class="subtitle">Système d'information de la DGNum</h2>
<h2 class="subtitle mt-2">Système d'information de la DGNum</h2>
</div>
<div class="cell">
{% if user.is_authenticated %}

View file

@ -0,0 +1,25 @@
{% extends "base.html" %}
{% block content %}
<h2 class="subtitle">
<span>Profil de {{ person.displayname }}</span>
<span class="tag is-primary is-medium is-pulled-right">{{ person.name }}</span>
</h2>
<hr>
<h3 class="has-text-weight-bold mb-3">Identifiant unique :</h3>
<span class="button is-fullwidth">{{ person.uuid }}</span>
<br>
<h3 class="has-text-weight-bold mb-3">Token RADIUS :</h3>
<span class="button is-fullwidth">{{ radius_secret }}</span>
<br>
<h3 class="has-text-weight-bold mb-3">Membre des groupes suivants :</h3>
{% for group in person.memberof %}
<span class="button is-fullwidth">{{ group }}</span><br>
{% endfor %}
{% endblock content %}