Compare commits

..
Author SHA1 Message Date
imperosol 702524d77d feat: forbid self-refill on counters 2026-12-03 14:04:42 +01:00
7 changed files with 72 additions and 100 deletions

No files matched your search

@@ -190,6 +190,17 @@
>
{{ refilling_fragment }}
</div>
{% elif customer in request.barmen %}
<div class="accordion-content">
<p class="alert alert-yellow">
{% trans trimmed %}
You are able to refill AE accounts.
However, it is forbidden to self-refill on a counter.
To refill your account, please ask to another barman,
or do it in the eboutic.
{% endtrans %}
</p>
</div>
{% else %}
<div class="accordion-content">
<p class="alert alert-yellow">
@@ -1,4 +1,5 @@
<form
id="counter-refill-fragment"
hx-trigger="submit"
hx-post="{{ action }}"
hx-swap="outerHTML"
+33 -1
View File
@@ -18,7 +18,7 @@ from decimal import Decimal
from typing import TYPE_CHECKING
import pytest
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup, SoupStrainer
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.contrib.auth.hashers import make_password
@@ -144,6 +144,24 @@ class TestRefilling(TestFullClickBase):
),
)
def refill_form_displayed(self, customer: User | Customer | None = None) -> bool:
"""Assert the click interface is displayed,
then return if it contains the refill form.
"""
if not customer:
customer = self.customer
click_res = self.client.get(
reverse(
"counter:click",
kwargs={"counter_id": self.counter.id, "user_id": customer.pk},
)
)
assert click_res.status_code == 200
soup = BeautifulSoup(
click_res.content, "lxml", parse_only=SoupStrainer(id="click-form")
)
return soup.find(id="counter-refill-fragment") is not None
def test_refilling_office_fail(self):
self.client.force_login(self.club_admin)
assert self.refill_user(self.customer, self.club_counter, 10).status_code == 403
@@ -197,9 +215,22 @@ class TestRefilling(TestFullClickBase):
def test_refilling_counter_no_board_member(self):
self.login_in_bar(barmen=self.barmen)
assert not self.refill_form_displayed()
assert self.refill_user(self.customer, self.counter, 10).status_code == 403
assert self.updated_amount(self.customer) == 0
def test_self_refill_fails(self):
"""Test that a barman cannot refill itself."""
original_amount = self.barmen.customer.amount
self.barmen.user_permissions.add(
Permission.objects.get(codename="add_refilling")
)
self.login_in_bar(barmen=self.barmen)
assert not self.refill_form_displayed(customer=self.barmen)
assert self.refill_user(self.barmen, self.counter, 10).status_code == 403
assert self.updated_amount(self.customer) == original_amount
def test_refilling_user_can_not_buy(self):
self.login_in_bar(barmen=self.barmen)
@@ -228,6 +259,7 @@ class TestRefilling(TestFullClickBase):
def test_refilling_counter_success(self):
self.login_in_bar()
assert self.refill_form_displayed()
assert self.refill_user(self.customer, self.counter, 30).status_code == 302
assert self.updated_amount(self.customer) == 30
assert self.refill_user(self.customer, self.counter, 10.1).status_code == 302
+12 -3
View File
@@ -147,7 +147,9 @@ class CounterClick(
}
def dispatch(self, request, *args, **kwargs):
self.customer = get_object_or_404(Customer, user_id=self.kwargs["user_id"])
self.customer = get_object_or_404(
Customer.objects.select_related("user"), user_id=self.kwargs["user_id"]
)
obj: Counter = self.get_object()
if not self.customer.can_buy or self.customer.user.is_banned_counter:
@@ -278,7 +280,11 @@ class CounterClick(
res["student_card_fragment"] = StudentCardFormFragment.as_fragment()(
self.request, customer=self.customer
)
if self.object.can_refill:
if self.object.can_refill and self.customer.user not in self.request.barmen:
# The counter must a bar with a barman allowed to refill.
# However, a barman, even with the required permission,
# cannot refill itself, which means the refilling process always
# implies two persons (in order to prevent fraud and genuine errors).
res["refilling_fragment"] = RefillingCreateView.as_fragment()(
self.request, customer=self.customer, counter=self.object
)
@@ -312,7 +318,9 @@ class RefillingCreateView(FragmentMixin, CreateView):
template_name = "counter/fragments/create_refill.jinja"
def dispatch(self, request, *args, **kwargs):
self.customer: Customer = get_object_or_404(Customer, pk=kwargs["customer_id"])
self.customer: Customer = get_object_or_404(
Customer.objects.select_related("user"), pk=kwargs["customer_id"]
)
if not self.customer.can_buy:
raise Http404
@@ -321,6 +329,7 @@ class RefillingCreateView(FragmentMixin, CreateView):
if (
not is_logged_in_counter(request, counter=self.counter)
or not self.counter.can_refill
or self.customer.user in self.request.barmen
):
raise PermissionDenied
+13 -26
View File
@@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2026-12-02 13:40+0100\n"
"POT-Creation-Date: 2026-12-03 08:59+0100\n"
"PO-Revision-Date: 2016-07-18\n"
"Last-Translator: Maréchal <thomas.girod@utbm.fr\n"
"Language-Team: AE info <ae.info@utbm.fr>\n"
@@ -3841,6 +3841,18 @@ msgstr "Terminer"
msgid "Refilling"
msgstr "Rechargement"
#: counter/templates/counter/counter_click.jinja
msgid ""
"You are able to refill AE accounts. However, it is forbidden to self-refill "
"on a counter. To refill your account, please ask to another barman, or do it "
"in the eboutic."
msgstr ""
"Vous avez le droit de recharger des comptes AE."
"Cependant, le rechargement de son propre compte sur un comptoir"
"est administrativement prohibé."
"Pour recharger votre compte, veuillez demander à un autre"
"barman ou passer par l'eboutic."
#: counter/templates/counter/counter_click.jinja
msgid ""
"As a barman, you are not able to refill any account on your own. An admin "
@@ -5392,31 +5404,6 @@ msgstr "Vous n'avez pas la permission de faire cela"
msgid "Upload images"
msgstr "Envoyer les images"
#: sas/forms.py
#, python-format
msgid ""
"You cannot grant the edit rights to the %(groups)s group. It is a club "
"simple member group, which means anyone could become an editor by merely "
"joining the club."
msgid_plural ""
"You cannot grant the edit rights to the following groups : %(groups)s. They "
"are club simple member groups, which means anyone could become an editor by "
"merely joining one of the clubs."
msgstr[0] ""
"Vous ne pouvez pas accorder les droits d'édition au groupe %(groups)s. C'est "
"un groupe de simples membres de club, ce qui signifie que n'importe qui "
"pourrait devenir éditeur simplement en rejoignant le club."
msgstr[1] ""
"Vous ne pouvez pas accorder les droits d'édition aux groupes suivants : "
"%(groups)s. Ce sont des groupes de simples membres de club, ce qui signifie "
"que n'importe qui pourrait devenir éditeur simplement en rejoignant un des "
"club."
#: sas/forms.py
#, python-format
msgid "%(group)s : this group cannot get album edition rights."
msgstr "%(group)s : ce groupe ne peut pas recevoir le droit d'éditer un album."
#: sas/forms.py
msgid "You already requested moderation for this picture."
msgstr "Vous avez déjà déposé une demande de retrait pour cette photo."
+1 -44
View File
@@ -4,12 +4,9 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils.timezone import get_current_timezone
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext_lazy
from PIL import Image
from core.utils import resize_image
@@ -22,7 +19,7 @@ from sas.widgets.ajax_select import AutoCompleteSelectAlbum
if TYPE_CHECKING:
from django.db.models.fields.files import FieldFile
from core.models import Group, User
from core.models import User
class AlbumCreateForm(forms.ModelForm):
@@ -87,46 +84,6 @@ class AlbumEditForm(forms.ModelForm):
f.file = resize_image(Image.open(f.file), 200, "WEBP")
return f
def clean_edit_groups(self):
groups: list[Group] = list(
self.cleaned_data["edit_groups"].annotate(
is_club_group=Q(club__isnull=False)
)
)
member_groups = [g.name for g in groups if g.is_club_group]
if member_groups:
self.add_error(
"edit_groups",
ValidationError(
ngettext_lazy(
"You cannot grant the edit rights "
"to the %(groups)s group. "
"It is a club simple member group, which means anyone "
"could become an editor by merely joining the club.",
"You cannot grant the edit rights "
"to the following groups : %(groups)s. "
"They are club simple member groups, which means anyone "
"could become an editor by merely joining one of the clubs.",
number=len(member_groups),
)
% {"groups": ", ".join(member_groups)}
),
)
for group in groups:
if group.id in [
settings.SITH_GROUP_SUBSCRIBERS_ID,
settings.SITH_GROUP_OLD_SUBSCRIBERS_ID,
settings.SITH_GROUP_PUBLIC_ID,
]:
self.add_error(
"edit_groups",
ValidationError(
_("%(group)s : this group cannot get album edition rights.")
% {"group": group.name}
),
)
return groups
def save(self, commit=True): # noqa: FBT002
initial_file = copy.copy(self.initial["file"])
if not self.cleaned_data["file"]:
+1 -26
View File
@@ -15,7 +15,6 @@ from model_bakery import baker
from PIL import Image
from pytest_django.asserts import assertInHTML, assertRedirects
from club.models import Club
from core.baker_recipes import subscriber_user
from core.models import Group, User
from core.utils import RED_PIXEL_PNG
@@ -91,8 +90,7 @@ def test_form_required(album: Album, excluded: str, is_valid: bool): # noqa: FB
@pytest.mark.django_db
def test_form_album_name_length(album: Album):
"""Test that the album name length constraint is enforced"""
def test_form_album_name(album: Album):
data = {
"name": "a" * Album.NAME_MAX_LENGTH,
"parent": album.pk,
@@ -104,29 +102,6 @@ def test_form_album_name_length(album: Album):
assert not AlbumEditForm(data=data).is_valid()
@pytest.mark.django_db
def test_club_member_groups_cannot_be_edit_groups(album: Album):
club = baker.make(Club)
data = {
"name": album.name,
"parent": album.pk,
"date": localdate(),
"edit_groups": [club.members_group_id, settings.SITH_GROUP_SUBSCRIBERS_ID],
}
form = AlbumEditForm(data=data)
assert not form.is_valid()
assert form.errors == {
"edit_groups": [
f"Vous ne pouvez pas accorder les droits "
f"d'édition au groupe {club.members_group.name}. "
"C'est un groupe de simples membres de club, "
"ce qui signifie que n'importe qui pourrait devenir éditeur "
"simplement en rejoignant le club.",
"Cotisants : ce groupe ne peut pas recevoir le droit d'éditer un album.",
]
}
@pytest.mark.django_db
def test_update_recursive_parent(client: Client, album: Album):
client.force_login(baker.make(User, is_superuser=True))