This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_routes.py
404 lines (317 loc) · 11.8 KB
/
api_routes.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
from flask_restx import Namespace, Resource
from flask import session, jsonify, request, abort
from CTFd.cache import cache, make_cache_key
from CTFd.utils import get_config
from CTFd.utils.modes import get_model, TEAMS_MODE
from CTFd.utils.user import get_current_team, authed, is_admin
from CTFd.utils.decorators import (
authed_only,
during_ctf_time_only,
admins_only,
is_admin
)
from CTFd.utils.decorators.visibility import (
check_account_visibility
)
from CTFd.utils.config.visibility import (
accounts_visible
)
from sqlalchemy.sql import or_, and_, any_
from .db_tables import db, Attributes, IntersectionTeamAttr, AttributesSelectOptions
from .schemas import AttributesSchema, IntersectionTeamAttrSchema, AttributesSelectOptionsSchema
from .admin_views import supported_input_types
attributes_namespace = Namespace('attributes', description="Endpoint to retrieve Team Attributes")
@attributes_namespace.route('')
class AttributeList(Resource):
def get(self):
if is_admin():
attrs = Attributes.query
else:
attrs = Attributes.query.filter_by(hidden=False)
view = AttributesSchema.views.get(session.get("type", "user"))
response = AttributesSchema(view=view, many=True).dump(attrs)
if response.errors:
return {"success": False, "errors": response.errors}, 400
response = {
'success':True,
'data': response.data
}
return response
@admins_only
def post(self):
req = request.get_json()
schema = AttributesSchema()
response = schema.load(req)
if response.errors:
return {"success": False, "errors": response.errors}, 400
db.session.add(response.data)
db.session.commit()
response = schema.dump(response.data)
db.session.close()
return {"success": True, "data": response.data}
@attributes_namespace.route('/<int:attr_id>')
@attributes_namespace.param("attr_id", "Attribute ID")
class Attribute(Resource):
@check_account_visibility
def get(self, attr_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
if (attr.hidden) and is_admin() is False:
abort(404)
view = AttributesSchema.views.get(session.get("type", "user"))
response = AttributesSchema(view=view).dump(attr)
if response.errors:
return {"success": False, "errors": response.errors}, 400
return {"success": True, "data": response.data}
@admins_only
def patch(self, attr_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
data = request.get_json()
data["id"] = attr_id
if supported_input_types[data['type']] == "checkbox":
if data["default"] == True:
data["default"]="1"
else:
data["default"]="0"
schema = AttributesSchema(view="admin", instance=attr, partial=True)
response = schema.load(data)
if response.errors:
return {"success": False, "errors": response.errors}, 400
response = schema.dump(response.data)
db.session.commit()
db.session.close()
return {"success": True, "data": response.data}
@admins_only
def delete(self, attr_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
db.session.delete(attr)
db.session.commit()
db.session.close()
return {"success": True}
@attributes_namespace.route('/<int:attr_id>/<int:team_id>')
@attributes_namespace.param("attr_id", "Attribute ID")
@attributes_namespace.param("team_id", "Team ID")
class New_Team_Attributes(Resource):
@check_account_visibility
def get(self, attr_id, team_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
if (attr.hidden) and is_admin() is False:
abort(404)
intersec = IntersectionTeamAttr.query.filter_by(attr_id=attr_id).filter_by(team_id = team_id).first_or_404()
if (attr.private) and is_admin() is False:
t = get_current_team()
if not( authed() and t and t.id == intersec.team_id):
abort(404)
view = IntersectionTeamAttrSchema.views.get(session.get("type", "user"))
response = IntersectionTeamAttrSchema(view=view).dump(intersec)
if response.errors:
return {"success": False, "errors": response.errors}, 400
if supported_input_types[attr.type] == "checkbox":
if intersec.value and intersec.value != "false":
response.data["value"] = True
else:
response.data["value"] = False
return {"success": True, "data": response.data}
@authed_only
def post(self, attr_id, team_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
if is_admin() is False:
if (attr.hidden):
abort(404)
if (attr.frozen):
abort(403)
team = get_current_team()
if team_id != team.id:
abort(403)
team_id = team.id
req = request.get_json()
if supported_input_types[attr.type] == "checkbox":
if req["value"] == True:
req["value"]="1"
else:
req["value"]="0"
if "value" not in req:
req["value"] = ""
schema = IntersectionTeamAttrSchema()
intersec = IntersectionTeamAttr.query.filter_by(attr_id=attr_id).filter_by(team_id=team_id)
if intersec.count() > 0:
intersec = intersec.first_or_404()
req["id"] = intersec.id
else:
if "id" in req:
del req["id"]
response = schema.load(req)
if response.errors:
return {"success": False, "errors": response.errors}, 400
db.session.add(response.data)
db.session.commit()
response = schema.dump(response.data)
db.session.close()
return {"success": True, "data": response.data}
@authed_only
def patch(self, attr_id, team_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
if is_admin() is False:
if (attr.hidden):
abort(404)
if (attr.frozen):
abort(403)
team = get_current_team()
if team_id != team.id:
abort(403)
team_id = team.id
data = request.get_json()
intersec = IntersectionTeamAttr.query.filter_by(attr_id=attr_id).filter_by(team_id=team_id)
if intersec.count() > 0:
intersec = intersec.first_or_404()
data["id"] = intersec.id
else:
if "id" in data:
del data["id"]
data["attr_id"] = attr_id
data["team_id"] = team_id
if supported_input_types[attr.type] == "checkbox":
if data["value"] == True:
data["value"]="1"
else:
data["value"]="0"
if "value" not in data:
data["value"] = ""
data["value"] = data["value"].replace('<', '<').replace('>', '>')
view = IntersectionTeamAttrSchema.views.get(session.get("type", "user"))
schema = IntersectionTeamAttrSchema(view=view)
response = schema.load(data)
if response.errors:
return {"success": False, "errors": response.errors}, 400
response = schema.dump(response.data)
db.session.commit()
db.session.close()
return {"success": True, "data": response.data}
@authed_only
def delete(self, attr_id, team_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
if is_admin() is False:
if (attr.hidden):
abort(404)
if (attr.frozen):
abort(403)
team = get_current_team()
team_id = team.id
intersec = IntersectionTeamAttr.query.filter_by(attr_id=attr_id).filter_by(team_id=team_id).first_or_404()
db.session.delete(intersec)
db.session.commit()
db.session.close()
return {"success": True}
@attributes_namespace.route('/teams')
class Attribute_teams(Resource):
@check_account_visibility
def get(self):
if is_admin() is False:
attrs = IntersectionTeamAttr.query.join(Attributes).filter_by(hidden=False, private=False).all()
else:
attrs = IntersectionTeamAttr.query.all()
view = IntersectionTeamAttrSchema.views.get(session.get("type", "user"))
response = IntersectionTeamAttrSchema(view=view, many=True).dump(attrs)
if response.errors:
return {"success": False, "errors": response.errors}, 400
return {"success": True, "data": response.data}
@attributes_namespace.route('/team/<int:team_id>')
@attributes_namespace.param("team_id", "Team ID")
class Attribute_team(Resource):
@check_account_visibility
def get(self, team_id):
if is_admin() is False:
attrs = IntersectionTeamAttr.query.join(Attributes).filter_by(hidden=False, private=False).filter(IntersectionTeamAttr.team_id==team_id).all()
else:
attrs = IntersectionTeamAttr.query.filter_by(team_id=team_id).all()
view = IntersectionTeamAttrSchema.views.get(session.get("type", "user"))
response = IntersectionTeamAttrSchema(view=view, many=True).dump(attrs)
if response.errors:
return {"success": False, "errors": response.errors}, 400
return {"success": True, "data": response.data}
@attributes_namespace.route('/team/me')
class Attribute_team(Resource):
@authed_only
def get(self):
team = get_current_team()
team_id = team.id
if is_admin() is False:
attrs = IntersectionTeamAttr.query.join(Attributes).filter_by(hidden=False).filter(IntersectionTeamAttr.team_id==team_id).all()
else:
attrs = IntersectionTeamAttr.query.filter_by(team_id=team_id).all()
view = IntersectionTeamAttrSchema.views.get(session.get("type", "user"))
response = IntersectionTeamAttrSchema(view=view, many=True).dump(attrs)
if response.errors:
return {"success": False, "errors": response.errors}, 400
return {"success": True, "data": response.data}
@attributes_namespace.route('/<int:attr_id>/options')
@attributes_namespace.param("attr_id", "Attribute ID")
class Attribute_options(Resource):
def get(self, attr_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
if is_admin() is False:
if (attr.hidden):
abort(404)
options = attr.get_options()
if (attr.hidden) and is_admin() is False:
abort(404)
view = AttributesSelectOptionsSchema.views.get(session.get("type", "user"))
response = AttributesSelectOptionsSchema(view=view, many=True).dump(options)
if response.errors:
return {"success": False, "errors": response.errors}, 400
return {"success": True, "data": response.data}
@admins_only
def post(self, attr_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
req = request.get_json()
req['attr_id'] = attr_id
req["value"] = req["value"].replace('<', '<').replace('>', '>')
schema = AttributesSelectOptionsSchema()
response = schema.load(req)
db.session.add(response.data)
if response.errors:
return {"success": False, "errors": response.errors}, 400
db.session.commit()
response = AttributesSelectOptionsSchema(view='admin').dump(response.data)
db.session.close()
return {"success": True, "data": response.data}
@attributes_namespace.route('/<int:attr_id>/options/<int:option_id>')
@attributes_namespace.param("attr_id", "Attribute ID")
class attribute_option(Resource):
@check_account_visibility
def get(self, attr_id, option_id):
attr = Attributes.query.filter_by(id=attr_id).first_or_404()
if is_admin() is False:
if (attr.hidden):
abort(404)
option = AttributesSelectOptions.query.filter_by(id=option_id).filter_by(attr_id=attr_id).first_or_404()
view = AttributesSelectOptionsSchema.views.get(session.get("type", "user"))
response = AttributesSelectOptionsSchema(view=view).dump(option)
if response.errors:
return {"success": False, "errors": response.errors}, 400
return {"success": True, "data": response.data}
@admins_only
def patch(self, attr_id, option_id):
data = request.get_json()
option = AttributesSelectOptions.query.filter_by(id=option_id).filter_by(attr_id)
option = option.first_or_404()
data["id"] = option.id
data["attr_id"] = attr_id
if "value" not in data:
data["value"] = ""
data["value"] = data["value"].replace('<', '<').replace('>', '>')
view = AttributesSelectOptionsSchema.views.get(session.get("type", "user"))
schema = AttributesSelectOptionsSchema(view=view)
response = schema.load(data)
if response.errors:
return {"success": False, "errors": response.errors}, 400
response = schema.dump(response.data)
db.session.commit()
db.session.close()
return {"success": True, "data": response.data}
@admins_only
def delete(self, attr_id, option_id):
option = AttributesSelectOptions.query.filter_by(id=option_id).filter_by(attr_id=attr_id).first_or_404()
db.session.delete(option)
db.session.commit()
db.session.close()
return {"success": True}