diff --git a/src/dgsi/views.py b/src/dgsi/views.py index 86f1651..742da24 100644 --- a/src/dgsi/views.py +++ b/src/dgsi/views.py @@ -1,5 +1,7 @@ from asgiref.sync import async_to_sync from django.contrib.auth.mixins import LoginRequiredMixin +from django.core.mail import EmailMessage +from django.template.loader import render_to_string from django.urls import reverse_lazy from django.views.generic import FormView, TemplateView @@ -30,6 +32,8 @@ class CreateKanidmAccountView(StaffRequiredMixin, FormView): @async_to_sync async def form_valid(self, form): + ttl = 86400 # 24h + d = form.cleaned_data # Create the base account @@ -42,6 +46,26 @@ class CreateKanidmAccountView(StaffRequiredMixin, FormView): if d["active"]: await client.group_add_members("dgnum_members", [d["name"]]) - # TODO: Generate a reset token, and update it + # FIXME: Will maybe change when kanidm gets its shit together and switches to POST + r = await client.call_get( + f"/v1/person/{d['name']}/_credential/_update_intent/{ttl}" + ) + + assert r.data is not None + + token: str = r.data["token"] + link = f"https://sso.dgnum.eu/ui/reset?token={token}" + + # Send an email to the new user with the given email address + EmailMessage( + subject="Réinitialisation de mot de passe DGNum -- DGNum password reset", + body=render_to_string( + "mail/credentials_reset.txt", + context={"link": link}, + ), + from_email="To Be Determined ", + to=[d["mail"]], + headers={"Reply-To": "contact@dgnum.eu"}, + ).send() return super().form_valid(form) diff --git a/src/shared/templates/mail/credentials_reset.txt b/src/shared/templates/mail/credentials_reset.txt new file mode 100644 index 0000000..f20a603 --- /dev/null +++ b/src/shared/templates/mail/credentials_reset.txt @@ -0,0 +1,17 @@ +Bonjour, + +Une demande de réinitialisation de votre mot de passe DGNum a été effectuée. + +Pour mettre à jour vos moyens de connexion, merci de vous rendre à l'adresse : {{ link }} + +-- + +Hello, + +A request to reset your DGNum password has been made. + +To update your login details, please go to: {{ link }} + + +Bien cordialement, +La Délégation Générale Numérique