Skip to content

Commit

Permalink
Merge pull request #131 from fsinfuhh/fix/registration_existing_account
Browse files Browse the repository at this point in the history
Fix account creation error when an account already exists
  • Loading branch information
timonegk authored Oct 4, 2023
2 parents ede394f + 1cd6ce8 commit f575bf0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
17 changes: 17 additions & 0 deletions mafiasi/registration/templates/registration/email_exsts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "base_email.html" %}{% load i18n %}{% autoescape off %}
{% block content_de %}Hallo,
du hast versucht, einen Account zu erstellen, den es schon gibt. Falls du dein Passwort vergessen hast, kannst du es hier zurücksetzen:
{{ password_reset_url }}
Dein Nutzername ist {{ username }}.

Viele Grüße,
Deine Server-AG
{% endblock %}
{% block content_en %}Hello,
you tried to create an account that already exists. If you forgot your password, you can reset it here:
{{ password_reset_url }}
Your username is {{ username }}.

Best regards,
Your Server-AG
{% endblock %}{% endautoescape %}
28 changes: 25 additions & 3 deletions mafiasi/registration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def request_account(request):
slug="employee", defaults={"name": "Employee"}
)

username = _get_username({"account": account, "domain": domain}, yeargroup)
if Mafiasi.objects.filter(username=username).exists():
return _send_email_exists(request, username)

return _finish_account_request(
request,
{
Expand Down Expand Up @@ -130,6 +134,10 @@ def additional_info(request):
if form.is_valid():
account = form.cleaned_data["account"]
domain = form.cleaned_data["domain"]
username = _get_username({"account": account, "domain": domain}, form.cleaned_data["yeargroup"])
if Mafiasi.objects.filter(username=username).exists():
return _send_email_exists(request, username)

return _finish_account_request(
request,
{
Expand All @@ -148,7 +156,7 @@ def request_successful(request):
return TemplateResponse(request, "registration/request_successful.html")


def _create_username(info, yeargroup):
def _get_username(info, yeargroup):
if info["domain"] != settings.PRIMARY_DOMAIN:
return "{}.{}".format(info["account"], settings.REGISTER_DOMAIN_MAPPING[info["domain"]])
elif info["account"][0].isdigit():
Expand All @@ -169,10 +177,10 @@ def create_account(request, info_token):
return TemplateResponse(request, "registration/token_invalid.html")

yeargroup = Yeargroup.objects.get(pk=info["yeargroup_pk"])
username = _create_username(info, yeargroup)
username = _get_username(info, yeargroup)

if Mafiasi.objects.filter(username=username).exists():
return redirect("simple_openid_connect.login")
return redirect(reverse("simple_openid_connect:login"))

if request.method == "POST":
form = PasswordForm(request.POST)
Expand Down Expand Up @@ -306,6 +314,20 @@ def _finish_account_request(request, info):
)


def _send_email_exists(request, username):
email = Mafiasi.objects.get(username=username).real_email
email_content = render_to_string(
"registration/email_exists.txt",
{
"username": username,
"password_reset_url": settings.PASSWORD_RESET_URL,
},
)
return _send_mail_or_error_page(
_("Account exists at %s" % settings.PROJECT_NAME), email_content, email, request, None
)


def _send_mail_or_error_page(subject, content, address, request, email_shown):
try:
send_mail(subject, content, None, [address])
Expand Down
4 changes: 4 additions & 0 deletions mafiasi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
KEYCLOAK_ACCOUNT_CONSOLE_URL = env.str(
"MAFIASI_KEYCLOAK_ACCOUNT_CONSOLE_URL", default="https://identity.mafiasi.de/realms/mafiasi/account"
)
PASSWORD_RESET_URL = env.str(
"MAFIASI_PASSWORD_RESET_URL",
default="https://identity.mafiasi.de/realms/mafiasi/login-actions/reset-credentials?client_id=mafiasi-dashboard",
)

OPENID_SYNC_SUPERUSER = env.bool("MAFIASI_OPENID_SYNC_SUPERUSER", default=True)
if OPENID_SYNC_SUPERUSER:
Expand Down

0 comments on commit f575bf0

Please sign in to comment.