Skip to content

Commit 6cc07f3

Browse files
authored
fix: permissions (#1782)
* fix: permissions * tweak
1 parent 5d484f0 commit 6cc07f3

File tree

2 files changed

+69
-26
lines changed

2 files changed

+69
-26
lines changed

Pipfile.lock

+21-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

game/permissions.py

+48-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def _get_userprofile_school(userprofile):
1111
elif hasattr(userprofile, "student"):
1212
return userprofile.student.class_field.teacher.school
1313
else:
14-
LOGGER.error(f"Userprofile ID {userprofile.id} has no teacher or student attribute")
14+
LOGGER.error(
15+
f"Userprofile ID {userprofile.id} has no teacher or student attribute"
16+
)
1517
return None
1618

1719

@@ -49,19 +51,32 @@ def can_play_or_delete_level(user, level):
4951
# If the teacher is an admin, they can play any student's level in the school, otherwise only student levels
5052
# from their own classes
5153
if user.userprofile.teacher.is_admin and hasattr(level.owner, "student"):
52-
return user.userprofile.teacher.school == level.owner.student.class_field.teacher.school
54+
return (
55+
user.userprofile.teacher.school
56+
== level.owner.student.class_field.teacher.school
57+
)
5358
else:
5459
return user.userprofile.teacher.teaches(level.owner)
5560

5661

5762
def can_approve_level(user, level):
58-
return hasattr(user.userprofile, "teacher") and level.shared_with.filter(id=user.id).exists()
63+
return (
64+
hasattr(user.userprofile, "teacher")
65+
and level.shared_with.filter(id=user.id).exists()
66+
)
5967

6068

6169
def can_play_level(user, level, early_access):
62-
if not user.is_anonymous and hasattr(user.userprofile, "student") and user.userprofile.student.class_field:
70+
if (
71+
not user.is_anonymous
72+
and hasattr(user.userprofile, "student")
73+
and user.userprofile.student.class_field
74+
):
6375
# If the user is a student, check that the level isn't locked for their class
64-
return user.userprofile.student.class_field not in level.locked_for_class.all() and not level.needs_approval
76+
return user.userprofile.id == level.owner_id or (
77+
user.userprofile.student.class_field not in level.locked_for_class.all()
78+
and not level.needs_approval
79+
)
6580
elif level.default and not level.episode.in_development:
6681
return True
6782
elif level.anonymous:
@@ -90,7 +105,9 @@ def can_load_level(user, level):
90105
owner_school = _get_userprofile_school(level.owner)
91106
return user_school is not None and user_school == owner_school
92107
else:
93-
return hasattr(user.userprofile, "teacher") and user.userprofile.teacher.teaches(level.owner)
108+
return hasattr(
109+
user.userprofile, "teacher"
110+
) and user.userprofile.teacher.teaches(level.owner)
94111

95112

96113
def can_save_level(user, level):
@@ -126,10 +143,16 @@ def has_permission(self, request, view):
126143
def has_object_permission(self, request, view, obj):
127144
if request.user.is_anonymous:
128145
return False
129-
elif hasattr(request.user.userprofile, "student") and request.user.userprofile.student.is_independent():
146+
elif (
147+
hasattr(request.user.userprofile, "student")
148+
and request.user.userprofile.student.is_independent()
149+
):
130150
return False
131151
# if the user is a teacher and the level is shared with them
132-
elif hasattr(request.user.userprofile, "teacher") and obj.shared_with.filter(id=request.user.id).exists():
152+
elif (
153+
hasattr(request.user.userprofile, "teacher")
154+
and obj.shared_with.filter(id=request.user.id).exists()
155+
):
133156
return True
134157
else:
135158
return obj.owner == request.user.userprofile and not obj.needs_approval
@@ -168,18 +191,30 @@ def can_share_level_with(self, recipient, sharer):
168191
and not (recipient_profile.student.is_independent())
169192
):
170193
# Are they in the same class?
171-
return sharer_profile.student.class_field == recipient_profile.student.class_field
172-
elif hasattr(sharer_profile, "teacher") and sharer_profile.teacher.teaches(recipient_profile):
194+
return (
195+
sharer_profile.student.class_field
196+
== recipient_profile.student.class_field
197+
)
198+
elif hasattr(sharer_profile, "teacher") and sharer_profile.teacher.teaches(
199+
recipient_profile
200+
):
173201
# Is the recipient taught by the sharer?
174202
return True
175-
elif hasattr(recipient_profile, "teacher") and recipient_profile.teacher.teaches(sharer_profile):
203+
elif hasattr(
204+
recipient_profile, "teacher"
205+
) and recipient_profile.teacher.teaches(sharer_profile):
176206
# Is the sharer taught by the recipient?
177207
return True
178-
elif hasattr(sharer_profile, "teacher") and hasattr(recipient_profile, "teacher"):
208+
elif hasattr(sharer_profile, "teacher") and hasattr(
209+
recipient_profile, "teacher"
210+
):
179211
# Are they in the same organisation?
180212
return recipient_profile.teacher.school == sharer_profile.teacher.school
181213
elif hasattr(sharer_profile, "teacher") and sharer_profile.teacher.is_admin:
182-
return recipient_profile.student.class_field.teacher.school == sharer_profile.teacher.school
214+
return (
215+
recipient_profile.student.class_field.teacher.school
216+
== sharer_profile.teacher.school
217+
)
183218
else:
184219
return False
185220

0 commit comments

Comments
 (0)