feat: added simple auth system
This commit is contained in:
19
korrekturmanagementsystem/middleware.py
Normal file
19
korrekturmanagementsystem/middleware.py
Normal 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)
|
||||
@@ -48,6 +48,7 @@ MIDDLEWARE = [
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
"korrekturmanagementsystem.middleware.LoginRequiredMiddleware"
|
||||
]
|
||||
|
||||
ROOT_URLCONF = "korrekturmanagementsystem.urls"
|
||||
@@ -123,3 +124,6 @@ STATIC_URL = "static/"
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||
|
||||
LOGIN_REDIRECT_URL = "/ticketsystem"
|
||||
LOGOUT_REDIRECT_URL = "/accounts/login/"
|
||||
@@ -21,4 +21,5 @@ from django.urls import include, path
|
||||
urlpatterns = [
|
||||
path("ticketsystem/", include("ticketsystem.urls")),
|
||||
path("admin/", admin.site.urls),
|
||||
path("accounts/", include('django.contrib.auth.urls'))
|
||||
]
|
||||
|
||||
56
ticketsystem/templates/registration/login.html
Normal file
56
ticketsystem/templates/registration/login.html
Normal 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 %}
|
||||
@@ -16,7 +16,7 @@ class HomeView(TemplateView):
|
||||
template_name = "ticketsystem/home.html"
|
||||
|
||||
|
||||
class TicketListView(ListView):
|
||||
class TicketListView(LoginRequiredMixin, ListView):
|
||||
model = Ticket
|
||||
template_name = "ticketsystem/index.html"
|
||||
context_object_name = "tickets"
|
||||
|
||||
Reference in New Issue
Block a user