diff --git a/fet2020/authentications/authentications.py b/fet2020/authentications/authentications.py index 7e36443d..37f8611a 100644 --- a/fet2020/authentications/authentications.py +++ b/fet2020/authentications/authentications.py @@ -10,29 +10,20 @@ def authentication(username, password): if password is None or password.strip() == "": return None - # username format - new_username = "uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at" - userdn = new_username.format(username=username) - server_uri = "ldap://juri.fet.htu.tuwien.ac.at" server = ldap3.Server(server_uri, port=389, use_ssl=True) - - has_user = False + userdn = f"uid={username},ou=user,dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at" try: conn = ldap3.Connection(server, user=userdn, password=password, auto_bind=True) conn.search("dc=fet,dc=htu,dc=tuwien,dc=ac,dc=at", "(objectclass=person)") for user in sorted(conn.entries): - if ("DN: uid=" + str(username.lower())) in str(user): - has_user = True + if f"DN: uid={username}" in str(user): + return username except LDAPBindError as e: - logger.info("Username does not exist. Error: {}".format(e)) - username = None + logger.info(f"Username does not exist. Error: {e}") except Exception as e: - logger.info("Connection to server lost. Error: {}".format(e)) - username = None + logger.info(f"Connection to server lost. Error: {e}") - if not has_user: - username = None - - return username + logger.info(f"This username has been typed: '{username}'") + return None diff --git a/fet2020/authentications/views.py b/fet2020/authentications/views.py index b161a9e6..da6ac71f 100644 --- a/fet2020/authentications/views.py +++ b/fet2020/authentications/views.py @@ -1,10 +1,9 @@ -from django.shortcuts import render, redirect -from django.contrib.auth import login, logout from django.contrib import messages +from django.contrib.auth import login, logout from django.contrib.auth.models import User +from django.shortcuts import render, redirect from documents.etherpadlib import del_ep_cookie - from .authentications import authentication from .decorators import unauthenticated_user, authenticated_user from .forms import LoginForm @@ -13,12 +12,12 @@ from .forms import LoginForm @unauthenticated_user def loginPage(request): if request.method == "POST": - username = request.POST.get("username") + username = request.POST.get("username").lower() password = request.POST.get("password") auth_user = authentication(username, password) - if auth_user is not None: + if auth_user: try: user = User.objects.get(username=auth_user.lower()) except User.DoesNotExist: @@ -31,7 +30,7 @@ def loginPage(request): except: return redirect("home") else: - messages.info(request, "username or password is incorrect") + messages.error(request, "Anmeldung nicht erfolgreich. Bitte überprüfe Benutzername und Passwort.") form = LoginForm() diff --git a/fet2020/templates/authentications/login.html b/fet2020/templates/authentications/login.html index b917b7ca..b813fe11 100644 --- a/fet2020/templates/authentications/login.html +++ b/fet2020/templates/authentications/login.html @@ -1,11 +1,11 @@ {% extends 'base.html' %} -{% block title %}Login{% endblock %} +{% block title %}Anmeldung{% endblock %} {% block content %}
-

Login für FET-Mitarbeiter

+

Anmeldung für FET-Mitarbeiter

{% csrf_token %} @@ -19,7 +19,7 @@ {% endfor %}