A9: operations console, three pause commands, admin and player reports (WEB-30..32, WEB-40..44, WEB-55, AUD-48)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3d1e820564
commit
eab141f467
24 files changed
+928
-11
No files matched your search
+54
-1
@@ -1,6 +1,59 @@
|
||||
"""Users in the admin (WEB-32). Staff can deactivate or promote an account but never delete one: a user's
|
||||
ledger rows are PROTECTed (DAT-76), and a deactivation is an audited action. A user's game records are shown
|
||||
read-only on the same page."""
|
||||
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
|
||||
from apps.lottery import audit
|
||||
from apps.lottery.models import Movement, Participation
|
||||
|
||||
from .models import User
|
||||
|
||||
admin.site.register(User, UserAdmin)
|
||||
|
||||
class ReadOnlyInline(admin.TabularInline):
|
||||
extra = 0
|
||||
can_delete = False
|
||||
show_change_link = False
|
||||
|
||||
def has_add_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def has_change_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
|
||||
class ParticipationInline(ReadOnlyInline):
|
||||
model = Participation
|
||||
fields = ("round", "status", "bet_amount_sats", "created_at")
|
||||
readonly_fields = fields
|
||||
ordering = ("-id",)
|
||||
max_num = 0
|
||||
verbose_name_plural = "Participations (read-only)"
|
||||
|
||||
|
||||
class MovementInline(ReadOnlyInline):
|
||||
model = Movement
|
||||
fields = ("kind", "status", "amount_sats", "round", "created_at")
|
||||
readonly_fields = fields
|
||||
ordering = ("-id",)
|
||||
max_num = 0
|
||||
verbose_name_plural = "Movements (read-only)"
|
||||
|
||||
|
||||
@admin.register(User)
|
||||
class AccountAdmin(UserAdmin):
|
||||
inlines = [ParticipationInline, MovementInline]
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
super().save_model(request, obj, form, change)
|
||||
if change and {"is_active", "is_staff", "is_superuser"} & set(form.changed_data):
|
||||
audit.record("user_changed", actor=request.user, subject_type="user", subject_id=obj.pk,
|
||||
payload={name: getattr(obj, name) for name in ("is_active", "is_staff", "is_superuser")
|
||||
if name in form.changed_data})
|
||||
@@ -68,3 +68,10 @@ class PasswordChangeForm(forms.Form):
|
||||
cleaned["new_password"] != cleaned["new_password_confirm"]:
|
||||
self.add_error("new_password_confirm", "Le due password non coincidono.")
|
||||
return cleaned
|
||||
|
||||
|
||||
class ReportForm(forms.Form):
|
||||
"""WEB-55: a problem report from the profile."""
|
||||
|
||||
text = forms.CharField(label="Descrivi il problema", min_length=5, max_length=1000, strip=True,
|
||||
widget=forms.Textarea(attrs={"rows": 4, "maxlength": 1000}))
|
||||
@@ -18,6 +18,23 @@
|
||||
<button class="button" type="submit">Aggiorna la password</button>
|
||||
</form>
|
||||
</section>
|
||||
<section class="card narrow" aria-labelledby="segnalazioni">
|
||||
<h2 id="segnalazioni">Segnala un problema</h2>
|
||||
<form method="post" action="{% url 'accounts:report' %}" novalidate>
|
||||
{% csrf_token %}
|
||||
{% include "accounts/_form_errors.html" with form=report_form %}
|
||||
{% for field in report_form %}{% include "accounts/_field.html" %}{% endfor %}
|
||||
<button class="button" type="submit">Invia la segnalazione</button>
|
||||
</form>
|
||||
{% if reports %}
|
||||
<h3>Le tue segnalazioni</h3>
|
||||
<ul class="plain">
|
||||
{% for report in reports %}
|
||||
<li><strong>{{ report.get_status_display }}</strong> · {{ report.created_at|date:"j F Y H:i" }} UTC<br>{{ report.text|linebreaksbr }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
</section>
|
||||
<section class="card narrow">
|
||||
<h2>Esci</h2>
|
||||
<form method="post" action="{% url 'accounts:logout' %}">
|
||||
|
||||
@@ -36,6 +36,7 @@ LOGIN = Backoff("login", threshold=5, base_delay=2.0, max_delay=300.0, decay_sec
|
||||
LOGIN_IP = Backoff("login_ip", threshold=20, base_delay=2.0, max_delay=300.0, decay_seconds=900.0)
|
||||
CHANGE_PASSWORD = Backoff("chpwd", threshold=5, base_delay=2.0, max_delay=300.0, decay_seconds=900.0)
|
||||
REGISTER = Quota("register", limit=5, window_seconds=3600.0)
|
||||
REPORT = Quota("report", limit=10, window_seconds=3600.0) # WEB-55: per user
|
||||
|
||||
_KEY_MAX = 150
|
||||
|
||||
|
||||
@@ -8,4 +8,5 @@ urlpatterns = [
|
||||
path("registrati/", views.register_view, name="register"),
|
||||
path("esci/", views.logout_view, name="logout"),
|
||||
path("app/profilo/", views.profile_view, name="profile"),
|
||||
path("app/profilo/segnalazioni/", views.report_view, name="report"),
|
||||
]
|
||||
+33
-3
@@ -11,10 +11,11 @@ from django.views.decorators.cache import never_cache
|
||||
from django.views.decorators.http import require_http_methods, require_POST
|
||||
|
||||
from apps.lottery import audit
|
||||
from apps.lottery.models import Report
|
||||
|
||||
from . import throttle
|
||||
from .client_ip import client_ip
|
||||
from .forms import LoginForm, PasswordChangeForm, RegisterForm
|
||||
from .forms import LoginForm, PasswordChangeForm, RegisterForm, ReportForm
|
||||
from .models import User
|
||||
|
||||
BACKEND = "apps.accounts.backends.CaseInsensitiveBackend"
|
||||
@@ -107,6 +108,35 @@ def logout_view(request):
|
||||
return redirect(settings.LOGOUT_REDIRECT_URL)
|
||||
|
||||
|
||||
def _profile_context(request, *, form=None, report_form=None) -> dict:
|
||||
"""Only the signed-in user's own data and reports (WEB-03)."""
|
||||
return {
|
||||
"form": form or PasswordChangeForm(request.user),
|
||||
"report_form": report_form or ReportForm(),
|
||||
"reports": Report.objects.filter(user=request.user).order_by("-created_at", "-id"),
|
||||
}
|
||||
|
||||
|
||||
@never_cache
|
||||
@login_required
|
||||
@require_POST
|
||||
def report_view(request):
|
||||
"""WEB-55: send a problem report. A rejected form does not use the quota; a sent one does."""
|
||||
key = str(request.user.pk)
|
||||
wait = throttle.retry_after(throttle.REPORT, key)
|
||||
form = ReportForm(request.POST)
|
||||
if wait > 0:
|
||||
return _too_many(request, "accounts/profile.html", _profile_context(request, report_form=form), wait)
|
||||
if not form.is_valid():
|
||||
return render(request, "accounts/profile.html", _profile_context(request, report_form=form), status=400)
|
||||
with transaction.atomic():
|
||||
report = Report.objects.create(user=request.user, text=form.cleaned_data["text"])
|
||||
audit.record("report_submitted", actor=request.user, subject_type="report", subject_id=report.pk)
|
||||
throttle.record(throttle.REPORT, key)
|
||||
messages.success(request, "Segnalazione inviata. Ne vedrai lo stato qui sotto.")
|
||||
return redirect("accounts:profile")
|
||||
|
||||
|
||||
@never_cache
|
||||
@login_required
|
||||
@require_http_methods(["GET", "POST"])
|
||||
@@ -116,7 +146,7 @@ def profile_view(request):
|
||||
key = str(request.user.pk)
|
||||
wait = throttle.retry_after(throttle.CHANGE_PASSWORD, key)
|
||||
if wait > 0:
|
||||
return _too_many(request, "accounts/profile.html", {"form": form}, wait)
|
||||
return _too_many(request, "accounts/profile.html", _profile_context(request, form=form), wait)
|
||||
if form.is_valid():
|
||||
if not request.user.check_password(form.cleaned_data["current_password"]):
|
||||
# SEC-54: a stolen session must not be able to guess the password and lock the owner out.
|
||||
@@ -133,4 +163,4 @@ def profile_view(request):
|
||||
update_session_auth_hash(request, request.user)
|
||||
messages.success(request, "Password aggiornata. Le altre sessioni sono state chiuse.")
|
||||
return redirect("accounts:profile")
|
||||
return render(request, "accounts/profile.html", {"form": form})
|
||||
return render(request, "accounts/profile.html", _profile_context(request, form=form))
|
||||
Reference in new issue
Block a user