diff --git a/CTFd/admin/teams.py b/CTFd/admin/teams.py index 263ab5e5..a35dedf0 100644 --- a/CTFd/admin/teams.py +++ b/CTFd/admin/teams.py @@ -6,6 +6,8 @@ from sqlalchemy.sql import not_ from CTFd import utils +import re + admin_teams = Blueprint('admin_teams', __name__) @@ -45,6 +47,54 @@ def admin_teams_view(page): return render_template('admin/teams.html', teams=teams, pages=pages, curr_page=page) +@admin_teams.route('/admin/team/new', methods=['POST']) +@admins_only +def admin_create_team(): + name = request.form.get('name', None) + password = request.form.get('password', None) + email = request.form.get('email', None) + website = request.form.get('website', None) + affiliation = request.form.get('affiliation', None) + country = request.form.get('country', None) + + errors = [] + + if not name: + errors.append('The team requires a name') + elif Teams.query.filter(Teams.name == name).first(): + errors.append('That name is taken') + + if not email: + errors.append('The team requires an email') + elif Teams.query.filter(Teams.email == email).first(): + errors.append('That email is taken') + + if email: + valid_email = re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email) + if not valid_email: + errors.append("That email address is invalid") + + if not password: + errors.append('The team requires a password') + + if website and (website.startswith('http://') or website.startswith('https://')) is False: + errors.append('Websites must start with http:// or https://') + + if errors: + db.session.close() + return jsonify({'data': errors}) + + team = Teams(name, email, password) + team.website = website + team.affiliation = affiliation + team.country = country + + db.session.add(team) + db.session.commit() + db.session.close() + return jsonify({'data': ['success']}) + + @admin_teams.route('/admin/team/', methods=['GET', 'POST']) @admins_only def admin_team(teamid): @@ -93,6 +143,11 @@ def admin_team(teamid): errors = [] + if email: + valid_email = re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email) + if not valid_email: + errors.append("That email address is invalid") + name_used = Teams.query.filter(Teams.name == name).first() if name_used and int(name_used.id) != int(teamid): errors.append('That name is taken') @@ -101,6 +156,9 @@ def admin_team(teamid): if email_used and int(email_used.id) != int(teamid): errors.append('That email is taken') + if website and (website.startswith('http://') or website.startswith('https://')) is False: + errors.append('Websites must start with http:// or https://') + if errors: db.session.close() return jsonify({'data': errors}) diff --git a/CTFd/themes/admin/templates/teams.html b/CTFd/themes/admin/templates/teams.html index d05bab8f..adc75469 100644 --- a/CTFd/themes/admin/templates/teams.html +++ b/CTFd/themes/admin/templates/teams.html @@ -10,7 +10,10 @@ input[type="checkbox"] { margin: 0px !important; position: relative; top: 5px; } {% block content %}
-

Teams

+

+ Teams + +