добавил просмотр пользователей, обновил алгоритм работы авторизации
This commit is contained in:
parent
7f4bdae4be
commit
09cfcdad0a
@ -3,7 +3,7 @@ import os
|
||||
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
|
||||
from django.shortcuts import render
|
||||
# from django.db.models import Manager
|
||||
from django.contrib.auth import authenticate, login
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
# только для тестирования!
|
||||
import requests
|
||||
@ -11,11 +11,9 @@ import requests
|
||||
TEST_BASE_FETCH = "https://test.wawaa.ru/dev-fetch.php"
|
||||
|
||||
|
||||
@login_required
|
||||
def view_index(request):
|
||||
if request.user.is_authenticated:
|
||||
return render(request, 'index.html')
|
||||
else:
|
||||
return HttpResponseRedirect('/account/login')
|
||||
return render(request, 'index.html')
|
||||
|
||||
|
||||
def view_stats(request):
|
||||
|
@ -123,6 +123,8 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
},
|
||||
]
|
||||
|
||||
LOGIN_URL = "/account/login"
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/5.0/topics/i18n/
|
||||
|
@ -61,3 +61,10 @@ header * {
|
||||
color: var(--brand-text);
|
||||
}
|
||||
|
||||
.value-good {
|
||||
color: var(--text-good);
|
||||
}
|
||||
|
||||
.value-bad {
|
||||
color: var(--text-bad);
|
||||
}
|
||||
|
59
templates/account/list.html
Normal file
59
templates/account/list.html
Normal file
@ -0,0 +1,59 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block styles %}
|
||||
<style>
|
||||
.table-wrapper {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
border-spacing: 0;
|
||||
}
|
||||
table, th, td {
|
||||
padding: 5px;
|
||||
border: 2px solid var(--bg-selected);
|
||||
}
|
||||
thead {
|
||||
background: var(--brand-bg);
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block header %}
|
||||
<h1> Просмотр аккаунтов </h1>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% if user.is_superuser %}
|
||||
<div class="table-wrapper">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Логин</td>
|
||||
<td>Админ</td>
|
||||
<td>Последний вход</td>
|
||||
<td>Последнее обновление пароля</td>
|
||||
<td>Зарегистрирован</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for u in users %}
|
||||
<tr>
|
||||
<td>{{ u.login }}</td>
|
||||
<td>{{ u.is_superuser }}</td>
|
||||
<td>{{ u.last_login }}</td>
|
||||
<td>{{ u.last_password_change }}</td>
|
||||
<td>{{ u.registered }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{% else %}
|
||||
<h2>Недостаточно прав для просмотра страницы</h2>
|
||||
<p>Для просмотра списка пользователей необходимо иметь статус администратора.</p>
|
||||
{% endif %}
|
||||
{% endblock %}
|
@ -62,11 +62,11 @@
|
||||
{% block content %}
|
||||
<div id="form-wrapper">
|
||||
<h1> Войти </h1>
|
||||
<form action="{% url 'login' %}" method="POST" id="login-form">
|
||||
<form method="POST" id="login-form">
|
||||
{% csrf_token %}
|
||||
|
||||
{% if message %}
|
||||
<div class="form-row error-message">
|
||||
<div class="form-row value-bad">
|
||||
{{ message }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
37
templates/account/view.html
Normal file
37
templates/account/view.html
Normal file
@ -0,0 +1,37 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block styles %}
|
||||
<style>
|
||||
.table-wrapper {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
border-spacing: 0;
|
||||
}
|
||||
table, th, td {
|
||||
padding: 5px;
|
||||
border: 2px solid var(--bg-selected);
|
||||
}
|
||||
thead {
|
||||
background: var(--brand-bg);
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
{% block header %}
|
||||
<h1> Аккаунт {{ view_user.login }} </h1>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
{% if have_permissions %}
|
||||
|
||||
|
||||
|
||||
{% else %}
|
||||
<h2>Недостаточно прав для просмотра страницы</h2>
|
||||
<p>Для просмотра списка пользователей необходимо иметь статус администратора.</p>
|
||||
{% endif %}
|
||||
{% endblock %}
|
@ -24,14 +24,6 @@
|
||||
#canvas-wrapper {
|
||||
max-width: 1000px;
|
||||
}
|
||||
|
||||
.value-good {
|
||||
color: var(--text-good);
|
||||
}
|
||||
|
||||
.value-bad {
|
||||
color: var(--text-bad);
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
|
||||
|
@ -22,5 +22,6 @@ urlpatterns = [
|
||||
path('register', views.default_view, name='register'),
|
||||
path('login', views.view_login, name='login'),
|
||||
path('logout', views.view_logout, name='logout'),
|
||||
path('list', views.view_list, name='accounts-list'),
|
||||
path('change-password', views.default_view, name='change-password'),
|
||||
]
|
||||
|
@ -4,6 +4,8 @@ from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadReque
|
||||
from django.shortcuts import render
|
||||
# from django.db.models import Manager
|
||||
from django.contrib.auth import authenticate, login, logout
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from .models import User
|
||||
|
||||
|
||||
def default_view(request):
|
||||
@ -17,6 +19,15 @@ def view_logout(request):
|
||||
|
||||
|
||||
def view_login(request):
|
||||
if request.user.is_authenticated:
|
||||
# уже авторизован, перенаправляем либо в корень, либо по пути, указанному в next
|
||||
redirect_uri = "/"
|
||||
if "next" in request.GET:
|
||||
if request.GET['next'] != request.path:
|
||||
# чтобы не возникло циклического редиректа
|
||||
redirect_uri = request.GET['next']
|
||||
return HttpResponseRedirect(redirect_uri)
|
||||
|
||||
render_context = {
|
||||
'message': None
|
||||
}
|
||||
@ -26,7 +37,12 @@ def view_login(request):
|
||||
user = authenticate(request, username=username, password=password)
|
||||
if user is not None:
|
||||
login(request, user)
|
||||
return HttpResponseRedirect('/')
|
||||
redirect_uri = "/"
|
||||
if "next" in request.GET:
|
||||
if request.GET['next'] != request.path:
|
||||
# чтобы не возникло циклического редиректа
|
||||
redirect_uri = request.GET['next']
|
||||
return HttpResponseRedirect(redirect_uri)
|
||||
else:
|
||||
render_context['message'] = "Неверный логин или пароль"
|
||||
return render(request, 'account/login.html', render_context)
|
||||
@ -36,4 +52,11 @@ def view_login(request):
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
|
||||
@login_required
|
||||
def view_list(request):
|
||||
users = []
|
||||
# ограничение права на просмотр списка пользователей для непривилегированных пользователей
|
||||
if request.user.is_superuser:
|
||||
users = User.objects.order_by('login')
|
||||
return render(request, 'account/list.html', {'users': users})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user