mirror of https://github.com/JohnHammond/CTFd.git
Fix awards in teams mode (#1289)
* Fix awards not being properly assigned to teams in `TEAMS_MODE`bulk-clear-sessions
parent
30d239fb61
commit
e5f128ec9d
|
@ -2,7 +2,8 @@ from flask import request
|
|||
from flask_restplus import Namespace, Resource
|
||||
|
||||
from CTFd.cache import clear_standings
|
||||
from CTFd.models import Awards, db
|
||||
from CTFd.utils.config import is_teams_mode
|
||||
from CTFd.models import Awards, db, Users
|
||||
from CTFd.schemas.awards import AwardSchema
|
||||
from CTFd.utils.decorators import admins_only
|
||||
|
||||
|
@ -14,6 +15,26 @@ class AwardList(Resource):
|
|||
@admins_only
|
||||
def post(self):
|
||||
req = request.get_json()
|
||||
|
||||
# Force a team_id if in team mode and unspecified
|
||||
if is_teams_mode():
|
||||
team_id = req.get("team_id")
|
||||
if team_id is None:
|
||||
user = Users.query.filter_by(id=req["user_id"]).first()
|
||||
if user.team_id is None:
|
||||
return (
|
||||
{
|
||||
"success": False,
|
||||
"errors": {
|
||||
"team_id": [
|
||||
"User doesn't have a team to associate award with"
|
||||
]
|
||||
},
|
||||
},
|
||||
400,
|
||||
)
|
||||
req["team_id"] = user.team_id
|
||||
|
||||
schema = AwardSchema()
|
||||
|
||||
response = schema.load(req, session=db.session)
|
||||
|
|
|
@ -225,6 +225,7 @@ $(() => {
|
|||
e.preventDefault();
|
||||
const params = $("#user-award-form").serializeJSON(true);
|
||||
params["user_id"] = $("#award-member-input").val();
|
||||
params["team_id"] = TEAM_ID;
|
||||
|
||||
$("#user-award-form > #results").empty();
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,10 +1,12 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from CTFd.models import Awards
|
||||
from tests.helpers import (
|
||||
create_ctfd,
|
||||
destroy_ctfd,
|
||||
gen_award,
|
||||
gen_team,
|
||||
login_as_user,
|
||||
register_user,
|
||||
)
|
||||
|
@ -54,6 +56,49 @@ def test_api_awards_post_admin():
|
|||
destroy_ctfd(app)
|
||||
|
||||
|
||||
def test_api_awards_post_admin_teams_mode():
|
||||
"""Can a user post /api/v1/awards if admin in team mode"""
|
||||
app = create_ctfd(user_mode="teams")
|
||||
with app.app_context():
|
||||
register_user(app)
|
||||
with login_as_user(app, "admin") as client:
|
||||
r = client.post(
|
||||
"/api/v1/awards",
|
||||
json={
|
||||
"name": "Name",
|
||||
"value": "100",
|
||||
"category": "Cate",
|
||||
"description": "Desc",
|
||||
"user_id": 2,
|
||||
},
|
||||
)
|
||||
|
||||
# This should fail because the user doesn't have a team
|
||||
assert r.status_code == 400
|
||||
assert "team_id" in r.get_json()["errors"].keys()
|
||||
assert r.get_json()["success"] is False
|
||||
|
||||
gen_team(app.db)
|
||||
r = client.post(
|
||||
"/api/v1/awards",
|
||||
json={
|
||||
"name": "Name",
|
||||
"value": "100",
|
||||
"category": "Cate",
|
||||
"description": "Desc",
|
||||
"user_id": 3,
|
||||
},
|
||||
)
|
||||
|
||||
# This should pass as we should auto determine the user's team
|
||||
assert r.status_code == 200
|
||||
assert r.get_json()["success"] is True
|
||||
award = Awards.query.filter_by(id=1).first()
|
||||
assert award.user_id == 3
|
||||
assert award.team_id == 1
|
||||
destroy_ctfd(app)
|
||||
|
||||
|
||||
def test_api_award_get_admin():
|
||||
"""Can a user get /api/v1/awards/<award_id> if admin"""
|
||||
app = create_ctfd()
|
||||
|
|
Loading…
Reference in New Issue