mirror of
https://github.com/ae-utbm/sith.git
synced 2026-12-05 07:12:48 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ac2bb48c2 | ||
|
|
d7fa0b5ed6 |
No files matched your search
+1
-1
@@ -36,7 +36,7 @@ class GroupAdmin(admin.ModelAdmin):
|
|||||||
list_display = ("name", "description", "is_manually_manageable")
|
list_display = ("name", "description", "is_manually_manageable")
|
||||||
list_filter = ("is_manually_manageable",)
|
list_filter = ("is_manually_manageable",)
|
||||||
search_fields = ("name",)
|
search_fields = ("name",)
|
||||||
autocomplete_fields = ("permissions",)
|
autocomplete_fields = ("permissions", "manageable_by")
|
||||||
|
|
||||||
|
|
||||||
@admin.register(BanGroup)
|
@admin.register(BanGroup)
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Generated by Django 5.2.19 on 2026-12-04 13:24
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
import core.models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [("core", "0053_alter_user_email_alter_user_second_email")]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterModelManagers(
|
||||||
|
name="group", managers=[("objects", core.models.CustomGroupManager())]
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="group",
|
||||||
|
name="manageable_by",
|
||||||
|
field=models.ManyToManyField(
|
||||||
|
blank=True,
|
||||||
|
help_text="Groups that are able to grant and remove this group.",
|
||||||
|
related_name="managed_groups",
|
||||||
|
to="core.group",
|
||||||
|
verbose_name="manageable by",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
||||||
+23
-1
@@ -33,7 +33,7 @@ from typing import TYPE_CHECKING, Final, Self
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import AbstractUser, UserManager
|
from django.contrib.auth.models import AbstractUser, GroupManager, UserManager
|
||||||
from django.contrib.auth.models import AnonymousUser as AuthAnonymousUser
|
from django.contrib.auth.models import AnonymousUser as AuthAnonymousUser
|
||||||
from django.contrib.auth.models import Group as AuthGroup
|
from django.contrib.auth.models import Group as AuthGroup
|
||||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||||
@@ -65,6 +65,19 @@ if TYPE_CHECKING:
|
|||||||
from club.models import Club
|
from club.models import Club
|
||||||
|
|
||||||
|
|
||||||
|
class GroupQuerySet(models.QuerySet):
|
||||||
|
def manageable_by(self, user: User):
|
||||||
|
"""Filter groups that are editable by this user in the sith UI."""
|
||||||
|
if user.has_perm("core.change_group"):
|
||||||
|
return self.filter(is_manually_manageable=True)
|
||||||
|
return self.filter(
|
||||||
|
is_manually_manageable=True, manageable_by__in=user.groups.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CustomGroupManager(GroupManager.from_queryset(GroupQuerySet)): ...
|
||||||
|
|
||||||
|
|
||||||
class Group(AuthGroup):
|
class Group(AuthGroup):
|
||||||
"""Wrapper around django.auth.Group"""
|
"""Wrapper around django.auth.Group"""
|
||||||
|
|
||||||
@@ -74,6 +87,15 @@ class Group(AuthGroup):
|
|||||||
help_text=_("If False, this shouldn't be shown on group management pages"),
|
help_text=_("If False, this shouldn't be shown on group management pages"),
|
||||||
)
|
)
|
||||||
description = models.TextField(_("description"))
|
description = models.TextField(_("description"))
|
||||||
|
manageable_by = models.ManyToManyField(
|
||||||
|
"Group",
|
||||||
|
verbose_name=_("manageable by"),
|
||||||
|
help_text=_("Groups that are able to grant and remove this group."),
|
||||||
|
related_name="managed_groups",
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
objects = CustomGroupManager()
|
||||||
|
|
||||||
def get_absolute_url(self) -> str:
|
def get_absolute_url(self) -> str:
|
||||||
return reverse("core:group_list")
|
return reverse("core:group_list")
|
||||||
|
|||||||
@@ -295,6 +295,49 @@ def test_user_update_groups(client: Client):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_user_update_groups_non_admin(client: Client):
|
||||||
|
"""Test that a non-admin user can update the groups it is allowed to."""
|
||||||
|
managed_group = baker.make(Group, is_manually_manageable=True)
|
||||||
|
other_group = baker.make(Group, is_manually_manageable=True)
|
||||||
|
operator = subscriber_user.make(
|
||||||
|
groups=[baker.make(Group, managed_groups=[managed_group])]
|
||||||
|
)
|
||||||
|
user = baker.make(User, groups=[other_group])
|
||||||
|
client.force_login(operator)
|
||||||
|
response = client.post(
|
||||||
|
reverse("core:user_groups", kwargs={"user_id": user.id}),
|
||||||
|
data={"groups": [managed_group.id]},
|
||||||
|
)
|
||||||
|
assertRedirects(response, user.get_absolute_url())
|
||||||
|
# the manageable group should have been attributed
|
||||||
|
# and the non-manageable groups should be kept
|
||||||
|
assert set(user.groups.all()) == {
|
||||||
|
Group.objects.get(pk=settings.SITH_GROUP_PUBLIC_ID),
|
||||||
|
managed_group,
|
||||||
|
other_group,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_user_update_groups_denied(client: Client):
|
||||||
|
"""Test that a user that can manage no group cannot access the page at all."""
|
||||||
|
operator = subscriber_user.make(groups=[baker.make(Group)])
|
||||||
|
user = baker.make(User)
|
||||||
|
client.force_login(operator)
|
||||||
|
response = client.get(reverse("core:user_groups", kwargs={"user_id": user.id}))
|
||||||
|
assert response.status_code == 403
|
||||||
|
response = client.post(
|
||||||
|
reverse("core:user_groups", kwargs={"user_id": user.id}),
|
||||||
|
data={"groups": [baker.make(Group).id]},
|
||||||
|
)
|
||||||
|
assert response.status_code == 403
|
||||||
|
# no group should have been attributed at all
|
||||||
|
assert set(user.groups.all()) == {
|
||||||
|
Group.objects.get(pk=settings.SITH_GROUP_PUBLIC_ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_logout(client: Client):
|
def test_logout(client: Client):
|
||||||
user = baker.make(User)
|
user = baker.make(User)
|
||||||
@@ -378,6 +421,19 @@ def test_displayed_user_self_tabs(user_factory, expected_tabs: list[str]):
|
|||||||
"account",
|
"account",
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
# this user is in a group that is allowed to manage another group
|
||||||
|
# so the groups tab should be displayed
|
||||||
|
lambda: subscriber_user.make(
|
||||||
|
groups=[
|
||||||
|
baker.make(
|
||||||
|
Group,
|
||||||
|
managed_groups=[baker.make(Group, is_manually_manageable=True)],
|
||||||
|
)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
["infos", "godfathers", "pictures", "clubs", "groups"],
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_displayed_other_user_tabs(user_factory, expected_tabs: list[str]):
|
def test_displayed_other_user_tabs(user_factory, expected_tabs: list[str]):
|
||||||
|
|||||||
+6
-2
@@ -366,7 +366,7 @@ class UserGroupsForm(forms.ModelForm):
|
|||||||
required_css_class = "required"
|
required_css_class = "required"
|
||||||
|
|
||||||
groups = forms.ModelMultipleChoiceField(
|
groups = forms.ModelMultipleChoiceField(
|
||||||
queryset=Group.objects.filter(is_manually_manageable=True),
|
queryset=Group.objects.none(),
|
||||||
widget=CheckboxSelectMultiple,
|
widget=CheckboxSelectMultiple,
|
||||||
label=_("Groups"),
|
label=_("Groups"),
|
||||||
required=False,
|
required=False,
|
||||||
@@ -376,6 +376,10 @@ class UserGroupsForm(forms.ModelForm):
|
|||||||
model = User
|
model = User
|
||||||
fields = ["groups"]
|
fields = ["groups"]
|
||||||
|
|
||||||
|
def __init__(self, *args, request_user: User, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields["groups"].queryset = Group.objects.manageable_by(request_user).all()
|
||||||
|
|
||||||
def save(self, *args, **kwargs) -> User:
|
def save(self, *args, **kwargs) -> User:
|
||||||
# make the super method manage error without persisting in db
|
# make the super method manage error without persisting in db
|
||||||
super().save(commit=False)
|
super().save(commit=False)
|
||||||
@@ -384,7 +388,7 @@ class UserGroupsForm(forms.ModelForm):
|
|||||||
self.instance.groups.set(
|
self.instance.groups.set(
|
||||||
[
|
[
|
||||||
*self.cleaned_data["groups"],
|
*self.cleaned_data["groups"],
|
||||||
*self.instance.groups.filter(is_manually_manageable=False),
|
*self.instance.groups.difference(self.fields["groups"].queryset),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
return self.instance
|
return self.instance
|
||||||
|
|||||||
+10
-5
@@ -26,7 +26,6 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
from django.views.generic import ListView
|
from django.views.generic import ListView
|
||||||
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
||||||
|
|
||||||
from core.auth.mixins import CanEditMixin
|
|
||||||
from core.models import Group, User
|
from core.models import Group, User
|
||||||
from core.views import DetailFormView
|
from core.views import DetailFormView
|
||||||
from core.views.forms import PermissionGroupsForm
|
from core.views.forms import PermissionGroupsForm
|
||||||
@@ -61,16 +60,19 @@ class EditMembersForm(forms.Form):
|
|||||||
# Views
|
# Views
|
||||||
|
|
||||||
|
|
||||||
class GroupListView(CanEditMixin, ListView):
|
class GroupListView(PermissionRequiredMixin, ListView):
|
||||||
"""Displays the Group list."""
|
"""Displays the Group list."""
|
||||||
|
|
||||||
model = Group
|
model = Group
|
||||||
queryset = Group.objects.filter(is_manually_manageable=True)
|
queryset = Group.objects.filter(is_manually_manageable=True)
|
||||||
ordering = ["name"]
|
ordering = ["name"]
|
||||||
template_name = "core/group_list.jinja"
|
template_name = "core/group_list.jinja"
|
||||||
|
# This page is basically a list of links towards group edit pages,
|
||||||
|
# so give access only to users who can edit groups
|
||||||
|
permission_required = "core.change_group"
|
||||||
|
|
||||||
|
|
||||||
class GroupEditView(CanEditMixin, UpdateView):
|
class GroupEditView(PermissionRequiredMixin, UpdateView):
|
||||||
"""Edit infos of a Group."""
|
"""Edit infos of a Group."""
|
||||||
|
|
||||||
model = Group
|
model = Group
|
||||||
@@ -78,6 +80,7 @@ class GroupEditView(CanEditMixin, UpdateView):
|
|||||||
pk_url_kwarg = "group_id"
|
pk_url_kwarg = "group_id"
|
||||||
template_name = "core/group_edit.jinja"
|
template_name = "core/group_edit.jinja"
|
||||||
fields = ["name", "description"]
|
fields = ["name", "description"]
|
||||||
|
permission_required = "core.change_group"
|
||||||
|
|
||||||
|
|
||||||
class GroupCreateView(PermissionRequiredMixin, CreateView):
|
class GroupCreateView(PermissionRequiredMixin, CreateView):
|
||||||
@@ -90,7 +93,7 @@ class GroupCreateView(PermissionRequiredMixin, CreateView):
|
|||||||
permission_required = "core.add_group"
|
permission_required = "core.add_group"
|
||||||
|
|
||||||
|
|
||||||
class GroupTemplateView(CanEditMixin, DetailFormView):
|
class GroupTemplateView(PermissionRequiredMixin, DetailFormView):
|
||||||
"""Display all users in a given Group
|
"""Display all users in a given Group
|
||||||
Allow adding and removing users from it.
|
Allow adding and removing users from it.
|
||||||
"""
|
"""
|
||||||
@@ -100,6 +103,7 @@ class GroupTemplateView(CanEditMixin, DetailFormView):
|
|||||||
form_class = EditMembersForm
|
form_class = EditMembersForm
|
||||||
pk_url_kwarg = "group_id"
|
pk_url_kwarg = "group_id"
|
||||||
template_name = "core/group_detail.jinja"
|
template_name = "core/group_detail.jinja"
|
||||||
|
permission_required = "core.change_group"
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
resp = super().form_valid(form)
|
resp = super().form_valid(form)
|
||||||
@@ -127,7 +131,7 @@ class GroupTemplateView(CanEditMixin, DetailFormView):
|
|||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
class GroupDeleteView(CanEditMixin, DeleteView):
|
class GroupDeleteView(PermissionRequiredMixin, DeleteView):
|
||||||
"""Delete a Group."""
|
"""Delete a Group."""
|
||||||
|
|
||||||
model = Group
|
model = Group
|
||||||
@@ -135,6 +139,7 @@ class GroupDeleteView(CanEditMixin, DeleteView):
|
|||||||
pk_url_kwarg = "group_id"
|
pk_url_kwarg = "group_id"
|
||||||
template_name = "core/delete_confirm.jinja"
|
template_name = "core/delete_confirm.jinja"
|
||||||
success_url = reverse_lazy("core:group_list")
|
success_url = reverse_lazy("core:group_list")
|
||||||
|
permission_required = "core.delete_group"
|
||||||
|
|
||||||
|
|
||||||
class PermissionGroupsUpdateView(
|
class PermissionGroupsUpdateView(
|
||||||
|
|||||||
+15
-5
@@ -35,7 +35,6 @@ from django.contrib.auth.decorators import login_required
|
|||||||
from django.contrib.auth.forms import SetPasswordForm
|
from django.contrib.auth.forms import SetPasswordForm
|
||||||
from django.contrib.auth.mixins import (
|
from django.contrib.auth.mixins import (
|
||||||
LoginRequiredMixin,
|
LoginRequiredMixin,
|
||||||
PermissionRequiredMixin,
|
|
||||||
UserPassesTestMixin,
|
UserPassesTestMixin,
|
||||||
)
|
)
|
||||||
from django.contrib.messages.views import SuccessMessageMixin
|
from django.contrib.messages.views import SuccessMessageMixin
|
||||||
@@ -62,7 +61,7 @@ from django.views.generic.edit import FormView, UpdateView
|
|||||||
from honeypot.decorators import check_honeypot
|
from honeypot.decorators import check_honeypot
|
||||||
|
|
||||||
from core.auth.mixins import CanEditMixin, CanEditPropMixin, CanViewMixin
|
from core.auth.mixins import CanEditMixin, CanEditPropMixin, CanViewMixin
|
||||||
from core.models import Gift, Preferences, User
|
from core.models import Gift, Group, Preferences, User
|
||||||
from core.views.forms import (
|
from core.views.forms import (
|
||||||
CGUApprovalForm,
|
CGUApprovalForm,
|
||||||
GiftForm,
|
GiftForm,
|
||||||
@@ -300,7 +299,10 @@ class UserTabsMixin(TabedViewMixin):
|
|||||||
"name": _("Timetable"),
|
"name": _("Timetable"),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
if self.request.user.is_owner(user):
|
if (
|
||||||
|
user.has_perm("core.change_group")
|
||||||
|
or Group.objects.manageable_by(self.request.user).exists()
|
||||||
|
):
|
||||||
tab_list.append(
|
tab_list.append(
|
||||||
{
|
{
|
||||||
"url": reverse("core:user_groups", kwargs={"user_id": user.id}),
|
"url": reverse("core:user_groups", kwargs={"user_id": user.id}),
|
||||||
@@ -595,7 +597,7 @@ class UserPreferencesView(UserTabsMixin, UseFragmentsMixin, CanEditMixin, Update
|
|||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
class UserUpdateGroupView(PermissionRequiredMixin, UserTabsMixin, UpdateView):
|
class UserUpdateGroupView(UserPassesTestMixin, UserTabsMixin, UpdateView):
|
||||||
"""Edit a user's groups."""
|
"""Edit a user's groups."""
|
||||||
|
|
||||||
model = User
|
model = User
|
||||||
@@ -604,7 +606,15 @@ class UserUpdateGroupView(PermissionRequiredMixin, UserTabsMixin, UpdateView):
|
|||||||
form_class = UserGroupsForm
|
form_class = UserGroupsForm
|
||||||
context_object_name = "profile"
|
context_object_name = "profile"
|
||||||
current_tab = "groups"
|
current_tab = "groups"
|
||||||
permission_required = "core.change_group"
|
|
||||||
|
def test_func(self) -> bool:
|
||||||
|
return self.request.user.is_authenticated and (
|
||||||
|
self.request.user.has_perm("core.change_group")
|
||||||
|
or Group.objects.manageable_by(self.request.user).exists()
|
||||||
|
)
|
||||||
|
|
||||||
|
def get_form_kwargs(self) -> dict[str, Any]:
|
||||||
|
return super().get_form_kwargs() | {"request_user": self.request.user}
|
||||||
|
|
||||||
|
|
||||||
class UserToolsView(LoginRequiredMixin, UserTabsMixin, TemplateView):
|
class UserToolsView(LoginRequiredMixin, UserTabsMixin, TemplateView):
|
||||||
|
|||||||
Reference in new issue
Block a user