feat(create_user): Send an email with the credentials reset link

This commit is contained in:
Tom Hubrecht 2024-09-19 13:03:09 +02:00
parent 283815d555
commit 7c11b0de4b
Signed by: thubrecht
SSH key fingerprint: SHA256:r+nK/SIcWlJ0zFZJGHtlAoRwq1Rm+WcKAm5ADYMoQPc
2 changed files with 42 additions and 1 deletions

View file

@ -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 <dgsi@infra.dgnum.eu>",
to=[d["mail"]],
headers={"Reply-To": "contact@dgnum.eu"},
).send()
return super().form_valid(form)

View file

@ -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