Skip to content

Commit 7c940b9

Browse files
committed
Fix participation visibility for nonlocal users
1 parent 2c78bf7 commit 7c940b9

File tree

8 files changed

+181
-115
lines changed

8 files changed

+181
-115
lines changed

ephios/api/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def filter_queryset(self, request, queryset, view):
1313
# to view public participation information (excl. email) you need to
1414
# * be able to see the event AND
1515
# * the event types' show_participation_data mode must fit
16-
return queryset.viewable_by(request.user)
16+
return queryset.viewable_by(request.user.as_participant())
1717

1818

1919
class UserinfoParticipationPermissionFilter(ParticipationPermissionFilter):

ephios/core/models/events.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.db import models, transaction
77
from django.db.models import (
88
BooleanField,
9+
Case,
910
CharField,
1011
DateTimeField,
1112
ForeignKey,
@@ -16,6 +17,7 @@
1617
Q,
1718
SlugField,
1819
TextField,
20+
When,
1921
)
2022
from django.db.models.functions import Coalesce
2123
from django.utils import formats
@@ -49,9 +51,10 @@ def get_queryset(self):
4951

5052
class EventType(Model):
5153
class ShowParticipantDataChoices(models.IntegerChoices):
52-
EVERYONE = 0, _("to everyone")
54+
INSTANCE_USERS = 0, _("to logged in users")
5355
CONFIRMED = 1, _("to confirmed participants")
5456
RESPONSIBLES = 2, _("only to responsible users")
57+
PUBLIC = 3, _("to everyone including guests and federated users")
5558

5659
title = CharField(_("title"), max_length=254)
5760
color = CharField(_("color"), max_length=7, default="#343a40")
@@ -61,7 +64,7 @@ class ShowParticipantDataChoices(models.IntegerChoices):
6164
"If you restrict who can see participant data, others will only be able to see that there is a participation, but not from whom."
6265
),
6366
choices=ShowParticipantDataChoices.choices,
64-
default=ShowParticipantDataChoices.EVERYONE,
67+
default=ShowParticipantDataChoices.INSTANCE_USERS,
6568
)
6669

6770
class Meta:
@@ -149,9 +152,31 @@ def activate(self):
149152

150153
class ParticipationQuerySet(PolymorphicQuerySet):
151154

152-
def viewable_by(self, user):
153-
if user.is_anonymous:
154-
return self.none()
155+
def with_show_participant_data_to(self, participant):
156+
return self.annotate(
157+
show_participant_data=Case(
158+
When(
159+
id__in=self.viewable_by(participant=participant),
160+
then=True,
161+
),
162+
default=False,
163+
output_field=BooleanField(),
164+
),
165+
)
166+
167+
def viewable_by(self, participant):
168+
from ephios.core.signup.participants import LocalUserParticipant
169+
170+
if not isinstance(participant, LocalUserParticipant):
171+
qs = self.filter(
172+
Q(
173+
shift__event__type__show_participant_data=EventType.ShowParticipantDataChoices.PUBLIC
174+
)
175+
| Q(id__in=participant.all_participations())
176+
)
177+
return qs.distinct()
178+
179+
user = getattr(participant, "user")
155180
viewable_events = get_objects_for_user(user, "core.view_event")
156181
viewable_userprofiles = get_objects_for_user(user, "core.view_userprofile")
157182
editable_events = get_objects_for_user(user, "core.change_event")
@@ -162,7 +187,10 @@ def viewable_by(self, user):
162187
)
163188
qs = self.filter(shift__event__in=viewable_events).filter(
164189
Q(
165-
shift__event__type__show_participant_data=EventType.ShowParticipantDataChoices.EVERYONE
190+
shift__event__type__show_participant_data=EventType.ShowParticipantDataChoices.INSTANCE_USERS
191+
)
192+
| Q(
193+
shift__event__type__show_participant_data=EventType.ShowParticipantDataChoices.PUBLIC
166194
)
167195
| Q(
168196
shift__event__type__show_participant_data=EventType.ShowParticipantDataChoices.CONFIRMED,

ephios/core/views/event.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,8 @@ def get_queryset(self):
482482
return base.prefetch_related("shifts").prefetch_related(
483483
Prefetch(
484484
"shifts__participations",
485-
queryset=AbstractParticipation.objects.all().annotate(
486-
show_participant_data=Case(
487-
When(
488-
id__in=AbstractParticipation.objects.all().viewable_by(
489-
self.request.user
490-
),
491-
then=True,
492-
),
493-
default=False,
494-
output_field=BooleanField(),
495-
)
485+
queryset=AbstractParticipation.objects.all().with_show_participant_data_to(
486+
participant=self.request.user.as_participant()
496487
),
497488
)
498489
)

0 commit comments

Comments
 (0)