mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
feat(accounts): a superuser ends a sign-in lock; the session length is a setting
A user locked by failed sign-ins could only be unlocked with manage.py axes_reset_username on the server, and nothing in the panel showed the lock. The Users page and the user's page now mark a locked user, counted as django-axes counts (a username's failures from one address, over browsers, in any letter case), and the user's page has Unlock sign-in for a superuser; it ends the lock from every address and is on the Logs page. A sign-in lasted Django's default two weeks, also after the browser was closed. It now ends Session length (hours) after the sign-in, a setting on the Settings page, 24 by default; a change applies from the next sign-in. An idle timeout would not help: an open VM list refreshes itself every five seconds. Blocking a user signs them out. django-auth-ldap does not check is_active for an open session, so a blocked LDAP user kept theirs. The OTP sign-in page showed its error as a red list inside the red box; it is plain text as on the other sign-in page, and django-otp's errors are in Turkish and Chinese.
This commit is contained in:
parent
5d3280f9ea
commit
f7dcdbab31
18 changed files with 259 additions and 10 deletions
|
|
@ -21,7 +21,7 @@
|
|||
</div>
|
||||
<div class="list-group list-group-flush searchable">
|
||||
{% for user in users %}
|
||||
<div class="list-group-item py-3" data-search="{{ user.username }} {{ user.email }}{% for group in user.groups.all %} {{ group.name }}{% endfor %} {% if user.is_superuser %}{% trans "Superuser" %}{% elif user.is_staff %}{% trans "Template editor" %}{% else %}{% trans "User" %}{% endif %} {% if user.is_active %}{% trans "Active" %}{% else %}{% trans "Blocked" %}{% endif %}">
|
||||
<div class="list-group-item py-3" data-search="{{ user.username }} {{ user.email }}{% for group in user.groups.all %} {{ group.name }}{% endfor %} {% if user.is_superuser %}{% trans "Superuser" %}{% elif user.is_staff %}{% trans "Template editor" %}{% else %}{% trans "User" %}{% endif %} {% if user.is_active %}{% trans "Active" %}{% else %}{% trans "Blocked" %}{% endif %}{% if user.username|lower in locked %} {% trans "Sign-in locked" %}{% endif %}">
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-12 col-md-4">
|
||||
<a href="{% url 'accounts:account' user.id %}" class="fw-semibold text-break">{{ user.username }}</a>
|
||||
|
|
@ -38,6 +38,9 @@
|
|||
{% else %}
|
||||
<span class="badge text-bg-danger"><span aria-hidden="true">{% bs_icon 'slash-circle' %}</span> {% trans "Blocked" %}</span>
|
||||
{% endif %}
|
||||
{% if user.username|lower in locked %}
|
||||
<a href="{% url 'accounts:account' user.id %}" class="badge bg-warning text-dark text-decoration-none"><span aria-hidden="true">{% bs_icon 'lock' %}</span> {% trans "Sign-in locked" %}</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12 col-md-5 small">
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
from contextlib import contextmanager
|
||||
from datetime import timedelta
|
||||
|
||||
from accounts.lockout import locked_usernames
|
||||
from accounts.models import ApiToken, Instance, UserAttributes, UserInstance
|
||||
from appsettings.settings import app_settings
|
||||
from computes.utils import lock_ownership, users_lock
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import update_session_auth_hash
|
||||
from django.contrib.auth import SESSION_KEY, update_session_auth_hash
|
||||
from django.contrib.auth.models import Group, Permission, User
|
||||
from django.contrib.sessions.models import Session
|
||||
from django.core.paginator import Paginator
|
||||
from django.http import HttpResponse
|
||||
from django.db import IntegrityError, transaction
|
||||
|
|
@ -179,6 +181,7 @@ def user_list(request):
|
|||
"admin/user_list.html",
|
||||
{
|
||||
"users": users,
|
||||
"locked": locked_usernames(),
|
||||
"title": _("Users"),
|
||||
},
|
||||
)
|
||||
|
|
@ -259,6 +262,7 @@ def user_update(request, pk):
|
|||
attributes = attributes_form.save()
|
||||
if not user.is_active: # as user_block
|
||||
ApiToken.objects.filter(user=user).delete()
|
||||
end_sessions(user)
|
||||
fields = set(changed_fields(before, snapshot(user))) | set(changed_fields(before_attributes, snapshot(attributes)))
|
||||
fields = sorted(fields - {"password", "last_login"})
|
||||
entry.action = "change " + ", ".join(fields)
|
||||
|
|
@ -342,6 +346,14 @@ def user_delete(request, pk):
|
|||
)
|
||||
|
||||
|
||||
def end_sessions(user):
|
||||
"""Sign a blocked user out: django-auth-ldap does not check is_active for
|
||||
an open session, so a blocked LDAP user would keep theirs."""
|
||||
for session in Session.objects.filter(expire_date__gt=timezone.now()):
|
||||
if session.get_decoded().get(SESSION_KEY) == str(user.pk):
|
||||
session.delete()
|
||||
|
||||
|
||||
@require_POST
|
||||
@superuser_only
|
||||
def user_block(request, pk):
|
||||
|
|
@ -356,6 +368,7 @@ def user_block(request, pk):
|
|||
user.save(update_fields=["is_active"]) # not the rest as read before the lock
|
||||
# an unblock must not bring back the tokens of someone who took the account over
|
||||
ApiToken.objects.filter(user=user).delete()
|
||||
end_sessions(user)
|
||||
except (NoSuperuser, claims.Busy) as err:
|
||||
refused(request, err)
|
||||
return redirect("admin:user_list")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue