forked from picoCTF/picoCTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam.py
557 lines (459 loc) · 17.1 KB
/
team.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""Team management module."""
from pymongo.collation import Collation, CollationStrength
from voluptuous import Length, Required, Schema
import api
from api import cache, check, log_action, PicoException
from api.cache import memoize
PROBLEMSOLVED_FILTER = ["category", "name", "score", "solve_time"]
new_team_schema = Schema(
{
Required("team_name"): check(
(
"The team name must be between 3 and 40 characters.",
[str, Length(min=3, max=40)],
),
(
"This team name conflicts with an existing user name.",
[lambda name: api.user.get_user(name=name) is None],
),
(
"A team with that name already exists.",
[lambda name: api.team.get_team(name=name) is None],
),
),
Required("team_password"): check(
(
"Passwords must be between 3 and 20 characters.",
[str, Length(min=3, max=20)],
)
),
},
extra=True,
)
def get_team(tid=None, name=None):
"""
Retrieve a team based on a property (tid, name, etc.).
Args:
tid: team id
name: team name
Returns:
Returns the corresponding team object or None if it could not be found
"""
db = api.db.get_conn()
match = {}
if tid is not None:
match.update({"tid": tid})
elif name is not None:
match.update({"team_name": name})
elif api.user.is_logged_in():
match.update({"tid": api.user.get_user()["tid"]})
else:
return None
return db.teams.find_one(match, {"_id": 0})
def update_team(tid, updates):
"""
Update a team with new properties.
Args:
tid: the tid of the team to update
updates: dict of updated properties
Returns:
tid of the updated team (unchanged), or
None if the team was not found
"""
db = api.db.get_conn()
if len(updates) > 0:
success = db.teams.find_one_and_update({"tid": tid}, {"$set": updates})
if not success:
return None
return tid
@memoize(timeout=5 * 24 * 60 * 60)
def get_groups(tid):
"""
Get the group membership for a team.
Args:
tid: The team id
Returns:
List of group objects the team is a member of.
"""
# Get all groups associated with the given team
db = api.db.get_conn()
associated_groups = list(
db.groups.find(
{"$or": [{"owner": tid}, {"teachers": tid}, {"members": tid}]},
{"name": 1, "gid": 1, "owner": 1, "teachers": 1, "members": 1, "_id": 0},
)
)
for group in associated_groups:
# Replace the owner tid with the team's name
group["owner"] = api.team.get_team(tid=group["owner"])["team_name"]
return associated_groups
def create_and_join_new_team(team_name, team_password, user):
"""
Fulfill new team requests for users who have already registered.
Seperate from create_team() as we need to do additional logic:
- Check that the new team name doesn't conflict with a team or user
- Check that the user creating the team is on their initial
1-person "username team"
Args:
team_name: The desired name for the team.
Must be unique across users and teams.
team_password: The team's password.
user: The user
Returns:
The tid of the new team
Raises:
PicoException if a team or user with name team_name already exists,
or if user has already created a team
"""
# Ensure name does not conflict with existing user or team
db = api.db.get_conn()
if db.users.find_one(
{"username": team_name},
collation=Collation(locale="en", strength=CollationStrength.PRIMARY),
):
raise PicoException("There is already a user with this name.", 409)
if db.teams.find_one(
{"team_name": team_name},
collation=Collation(locale="en", strength=CollationStrength.PRIMARY),
):
raise PicoException("There is already a team with this name.", 409)
# Make sure the creating user has not already created a team
current_team = api.team.get_team(tid=user["tid"])
if current_team["team_name"] != user["username"]:
raise PicoException("You can only create one new team per user account!", 422)
# Create the team and join it
new_tid = create_team(
{
"team_name": team_name,
"password": api.common.hash_password(team_password),
"affiliation": current_team["affiliation"],
"creator": user["uid"],
"allow_ineligible_members": False,
}
)
join_team(team_name, team_password, user)
return new_tid
def create_team(params):
"""
Directly insert team into the database.
Assumes all fields have been validated.
Args:
params:
team_name: Name of the team
password: team's hashed password
affiliation: team's affiliation
Returns:
The newly created team id.
"""
db = api.db.get_conn()
params["tid"] = api.common.token()
params["size"] = 0
params["instances"] = {}
settings = api.config.get_settings()
if settings["shell_servers"]["enable_sharding"]:
params["server_number"] = api.shell_servers.get_assigned_server_number(
new_team=True
)
db.teams.insert(params)
return params["tid"]
def get_team_members(tid=None, name=None, show_disabled=True):
"""
Retrieve the members on a team.
Args:
tid: the team id to query
name: the team name to query
Returns:
A list of the team's members.
"""
db = api.db.get_conn()
tid = get_team(name=name, tid=tid)["tid"]
users = list(
db.users.find(
{"tid": tid},
{
"_id": 0,
"uid": 1,
"username": 1,
"firstname": 1,
"lastname": 1,
"disabled": 1,
"email": 1,
"teacher": 1,
"country": 1,
"usertype": 1,
},
)
)
return [user for user in users if show_disabled or not user.get("disabled", False)]
def get_team_uids(tid=None, name=None, show_disabled=True):
"""
Get the list of uids that belong to a team.
Args:
tid: the team id
name: the team name
Returns:
A list of uids
"""
return [
user["uid"]
for user in get_team_members(tid=tid, name=name, show_disabled=show_disabled)
]
def get_team_information(tid):
"""
Retrieve the information of a team.
Args:
tid: the team id
Returns:
A dict of team information.
team_name
members
"""
team_info = get_team(tid=tid)
# Sanitize
team_info.pop("password", None)
team_info.pop("instances", None)
# @TODO there is a LOT of stuff being added here - expensive. all needed?
# ideally combine/replace this w/ get_team()
# Add additional information
team_info["score"] = api.stats.get_score(tid=tid, time_weighted=False)
team_info["members"] = [
{
"username": member["username"],
"firstname": member["firstname"],
"lastname": member["lastname"],
"email": member["email"],
"uid": member["uid"],
"affiliation": member.get("affiliation", "None"),
"country": member["country"],
"usertype": member["usertype"],
"can_leave": api.user.can_leave_team(member["uid"]),
}
for member in get_team_members(tid=tid, show_disabled=False)
]
team_info["progression"] = api.stats.get_score_progression(tid=tid)
team_info["flagged_submissions"] = api.submissions.get_suspicious_submissions(tid)
team_info["max_team_size"] = api.config.get_settings()["max_team_size"]
if api.config.get_settings()["achievements"]["enable_achievements"]:
team_info["achievements"] = api.achievement.get_earned_achievements(tid)
team_info["solved_problems"] = []
for solved_problem in api.problem.get_solved_problems(tid=tid):
filtered_problem = {
k: v for k, v in solved_problem.items() if k in PROBLEMSOLVED_FILTER
}
team_info["solved_problems"].append(filtered_problem)
return team_info
def get_all_teams(scoreboard_id=None):
"""
Retrieve all teams.
Args:
scoreboard_id: optional, find only teams eligible for this scoreboard
Returns:
A list of all matching teams.
"""
# Ignore empty teams (remnants of single player self-team ids)
db = api.db.get_conn()
match = {"size": {"$gt": 0}}
# If specified, restrict to teams eligible for a certain scoreboard
if scoreboard_id:
match["eligibilities"] = scoreboard_id
return list(db.teams.find(match, {"_id": 0}))
def join_team(team_name, password, user):
"""
Switch a user from their individual team to a proper team.
You can not use this to freely switch between teams.
Args:
team_name: The name of the team to join
password: The new team's password
user: The user
Returns:
ID of the new team
"""
current_team = api.user.get_team(uid=user["uid"])
desired_team = api.team.get_team(name=team_name)
desired_team_info = api.team.get_team_information(desired_team["tid"])
if current_team["team_name"] != user["username"]:
raise PicoException("You can not switch teams once you have joined one.", 403)
# Make sure the password is correct and there is room on the team
max_team_size = api.config.get_settings()["max_team_size"]
if desired_team["size"] >= max_team_size:
raise PicoException("That team is already at maximum capacity.", 403)
if not api.user.confirm_password(password, desired_team["password"]):
raise PicoException("That is not the correct password to join that team.", 403)
# Update the team's eligibilities
if desired_team_info["size"] == 0:
updated_eligible_scoreboards = [
scoreboard
for scoreboard in api.scoreboards.get_all_scoreboards()
if api.scoreboards.is_eligible(user, scoreboard)
]
else:
currently_eligible_scoreboards = [
api.scoreboards.get_scoreboard(sid)
for sid in desired_team_info["eligibilities"]
]
updated_eligible_scoreboards = [
scoreboard
for scoreboard in currently_eligible_scoreboards
if api.scoreboards.is_eligible(user, scoreboard)
]
lost_eligibilities = [
scoreboard
for scoreboard in currently_eligible_scoreboards
if scoreboard not in updated_eligible_scoreboards
]
if len(lost_eligibilities) > 0 and not desired_team_info.get(
"allow_ineligible_members", False
):
raise PicoException(
"You cannot join this team as doing so would make it "
+ "ineligible for the {} scoreboard.".format(
lost_eligibilities[0]["name"]
),
403,
)
# Join the new team
db = api.db.get_conn()
user_team_update = db.users.find_and_modify(
query={"uid": user["uid"], "tid": current_team["tid"]},
update={"$set": {"tid": desired_team["tid"]}},
new=True,
)
if not user_team_update:
raise PicoException("There was an issue switching your team!")
# Update the eligiblities of the new team
db.teams.find_one_and_update(
{"tid": desired_team["tid"]},
{"$set": {"eligibilities": [s["sid"] for s in updated_eligible_scoreboards]}},
)
# Update the sizes of the old and new teams
db.teams.find_one_and_update({"tid": desired_team["tid"]}, {"$inc": {"size": 1}})
db.teams.find_one_and_update({"tid": current_team["tid"]}, {"$inc": {"size": -1}})
# Remove old team from any groups and attempt to add new team
previous_groups = get_groups(current_team["tid"])
for group in previous_groups:
api.group.leave_group(gid=group["gid"], tid=current_team["tid"])
# Rejoin with new tid if not already member, and classroom
# email filter is not enabled.
if (
desired_team["tid"] not in group["members"]
and desired_team["tid"] not in group["teachers"]
):
group_settings = api.group.get_group_settings(gid=group["gid"])
if not group_settings["email_filter"]:
api.group.join_group(gid=group["gid"], tid=desired_team["tid"])
# Immediately invalidate some caches
cache.invalidate(api.stats.get_score, desired_team["tid"])
cache.invalidate(api.stats.get_score, user["uid"])
cache.invalidate(api.problem.get_unlocked_pids, desired_team["tid"])
cache.invalidate(
api.problem.get_solved_problems,
tid=desired_team["tid"],
uid=user["uid"],
category=None,
)
cache.invalidate(
api.problem.get_solved_problems,
tid=desired_team["tid"],
uid=None,
category=None,
)
cache.invalidate(
api.problem.get_solved_problems, tid=desired_team["tid"], uid=user["uid"]
)
cache.invalidate(api.problem.get_solved_problems, tid=desired_team["tid"])
cache.invalidate(api.problem.get_solved_problems, uid=user["uid"])
cache.invalidate(
api.stats.get_score_progression, tid=desired_team["tid"], category=None
)
cache.invalidate(api.stats.get_score_progression, tid=desired_team["tid"])
return desired_team["tid"]
@log_action(dont_log=["params.new-password", "params.new-password-confirmation"])
def update_password_request(params):
"""
Update team password.
Assumes args are keys in params.
Args:
params:
new-password: the new password
new-password-confirmation: confirmation of password
"""
user = api.user.get_user()
current_team = api.team.get_team(tid=user["tid"])
if current_team["team_name"] == user["username"]:
raise PicoException("You have not created a team yet.", 422)
if params["new-password"] != params["new-password-confirmation"]:
raise PicoException("Your team passwords do not match.", 422)
db = api.db.get_conn()
db.teams.update(
{"tid": user["tid"]},
{"$set": {"password": api.common.hash_password(params["new-password"])}},
)
def is_teacher_team(tid):
"""Check if team is a teacher's self-team."""
team = get_team(tid=tid)
members = get_team_members(tid=tid)
if (
team["size"] == 1
and members[0]["username"] == team["team_name"]
and members[0]["teacher"]
):
return True
else:
return False
@log_action
def delete_team(tid):
"""Scrub all traces of a team."""
db = api.db.get_conn()
db.submissions.delete_many({"tid": tid})
db.problem_feedback.delete_many({"tid": tid})
db.teams.find_one_and_delete({"tid": tid})
for group in get_groups(tid):
api.group.leave_group(group["gid"], tid)
api.cache.invalidate(api.team.get_groups, tid)
@log_action
def remove_member(tid, uid):
"""
Move the specified member back to their self-team.
Eliminates custom team if no members remain.
The member specified cannot have submitted any valid solutions.
"""
team = get_team(tid)
curr_user_uid = api.user.get_user()["uid"]
curr_user_is_creator = curr_user_uid == team.get("creator")
if not curr_user_is_creator and uid != curr_user_uid:
raise PicoException(
"Only the team captain can kick other members.", status_code=403
)
if uid not in get_team_uids(tid):
raise PicoException(
"Specified user is not a member of this team.", status_code=404
)
if api.user.get_user(uid=uid)["username"] == team["team_name"]:
raise PicoException("Cannot remove self from default team", status_code=403)
if not api.user.can_leave_team(uid):
if curr_user_is_creator and curr_user_uid == uid:
raise PicoException(
"Team captain must be the only remaining member in order "
+ "to leave.",
status_code=403,
)
else:
raise PicoException(
"This team member has submitted a flag and can no longer "
+ "be removed.",
status_code=403,
)
self_team_tid = api.team.get_team(name=api.user.get_user(uid=uid)["username"])[
"tid"
]
db = api.db.get_conn()
db.users.find_one_and_update({"uid": uid}, {"$set": {"tid": self_team_tid}})
db.teams.find_one_and_update({"tid": self_team_tid}, {"$inc": {"size": 1}})
db.teams.find_one_and_update({"tid": tid}, {"$inc": {"size": -1}})
# Delete the custom team if no members remain
remaining_team_size = db.teams.find_one({"tid": tid}, {"size": 1})["size"]
if remaining_team_size < 1:
delete_team(tid)
# Copy any acquired group memberships back to the self-team
for group in get_groups(tid):
api.group.join_group(gid=group["gid"], tid=self_team_tid)