feat: added simple auth system

This commit is contained in:
2025-05-13 13:20:25 +02:00
parent 07ea49a035
commit 64432a3854
5 changed files with 81 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
from django.urls import reverse_lazy
from django.http import HttpResponseRedirect
class LoginRequiredMiddleware:
def __init__(self, get_response):
self.get_response = get_response
self.login_exempt_urls = [
reverse_lazy("login"),
reverse_lazy("logout"),
"/admin/",
]
def __call__(self, request):
if (
not request.user.is_authenticated and
not any(request.path.startswith(str(url)) for url in self.login_exempt_urls)
):
return HttpResponseRedirect(reverse_lazy("login"))
return self.get_response(request)

View File

@@ -48,6 +48,7 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware", "django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware",
"korrekturmanagementsystem.middleware.LoginRequiredMiddleware"
] ]
ROOT_URLCONF = "korrekturmanagementsystem.urls" ROOT_URLCONF = "korrekturmanagementsystem.urls"
@@ -123,3 +124,6 @@ STATIC_URL = "static/"
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
LOGIN_REDIRECT_URL = "/ticketsystem"
LOGOUT_REDIRECT_URL = "/accounts/login/"

View File

@@ -21,4 +21,5 @@ from django.urls import include, path
urlpatterns = [ urlpatterns = [
path("ticketsystem/", include("ticketsystem.urls")), path("ticketsystem/", include("ticketsystem.urls")),
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("accounts/", include('django.contrib.auth.urls'))
] ]

View File

@@ -0,0 +1,56 @@
{% block content %}
<style>
.login-container {
max-width: 400px;
margin: 4rem auto;
padding: 2rem;
border: 1px solid #ccc;
border-radius: 8px;
background: #f9f9f9;
font-family: sans-serif;
}
.login-container h2 {
text-align: center;
margin-bottom: 1.5rem;
}
.login-container label {
display: block;
margin-top: 1rem;
}
.login-container input {
width: 100%;
padding: 0.5rem;
margin-top: 0.3rem;
border: 1px solid #bbb;
border-radius: 4px;
}
.login-container button {
margin-top: 1.5rem;
width: 100%;
padding: 0.6rem;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-weight: bold;
}
.login-container .errorlist {
color: red;
margin-top: 1rem;
}
</style>
<div class="login-container">
<h2>🔐 Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Anmelden</button>
</form>
</div>
{% endblock %}

View File

@@ -16,7 +16,7 @@ class HomeView(TemplateView):
template_name = "ticketsystem/home.html" template_name = "ticketsystem/home.html"
class TicketListView(ListView): class TicketListView(LoginRequiredMixin, ListView):
model = Ticket model = Ticket
template_name = "ticketsystem/index.html" template_name = "ticketsystem/index.html"
context_object_name = "tickets" context_object_name = "tickets"