231 lines
7.4 KiB
Python
231 lines
7.4 KiB
Python
import secrets
|
|
|
|
from flask import (
|
|
Blueprint,
|
|
flash,
|
|
get_flashed_messages,
|
|
redirect,
|
|
render_template,
|
|
request,
|
|
url_for,
|
|
)
|
|
|
|
from CTFd.cache import clear_standings, clear_user_session
|
|
from CTFd.models import Users, db
|
|
from CTFd.utils.decorators import admins_only
|
|
|
|
from .hooks import (
|
|
patch_registration_form,
|
|
patch_settings_form,
|
|
register_affiliation_guard,
|
|
system_code,
|
|
wrap_register_view,
|
|
)
|
|
from .models import AffiliationCodes, AffiliationCodeUsers
|
|
|
|
PLUGIN_NAME = "ctfd-affiliations"
|
|
ERRORS = f"{PLUGIN_NAME}.errors"
|
|
INFOS = f"{PLUGIN_NAME}.infos"
|
|
|
|
|
|
def _read_code_form(code_id=None):
|
|
"""Return (code, affiliation, max_uses, errors) from the submitted form."""
|
|
code = request.form.get("code", "").strip()
|
|
affiliation = request.form.get("affiliation", "").strip()
|
|
max_uses = request.form.get("max_uses", "").strip()
|
|
errors = []
|
|
|
|
if code_id is None and not code:
|
|
code = secrets.token_hex(4)
|
|
if not code or len(code) > 128:
|
|
errors.append("Code must be 1-128 characters long")
|
|
elif code.lower() == system_code().lower():
|
|
errors.append("Code must differ from the system registration code")
|
|
else:
|
|
existing = AffiliationCodes.find(code)
|
|
if existing is not None and existing.id != code_id:
|
|
errors.append("This code already exists")
|
|
|
|
if not affiliation or len(affiliation) > 127:
|
|
errors.append("Affiliation must be 1-127 characters long")
|
|
|
|
if not max_uses or max_uses == "0":
|
|
max_uses = None
|
|
elif max_uses.isdigit():
|
|
max_uses = int(max_uses)
|
|
else:
|
|
errors.append("Max uses must be a non-negative integer")
|
|
|
|
return code, affiliation, max_uses, errors
|
|
|
|
|
|
def _refresh_users(user_ids):
|
|
for user_id in user_ids:
|
|
clear_user_session(user_id=user_id)
|
|
if user_ids:
|
|
clear_standings()
|
|
|
|
|
|
def _render(template, **kwargs):
|
|
return render_template(
|
|
template,
|
|
errors=get_flashed_messages(category_filter=ERRORS),
|
|
infos=get_flashed_messages(category_filter=INFOS),
|
|
**kwargs,
|
|
)
|
|
|
|
|
|
def load(app):
|
|
app.db.create_all()
|
|
|
|
patch_registration_form()
|
|
patch_settings_form()
|
|
wrap_register_view(app)
|
|
register_affiliation_guard(app)
|
|
|
|
blueprint = Blueprint(
|
|
PLUGIN_NAME,
|
|
__name__,
|
|
template_folder="templates",
|
|
url_prefix=f"/plugins/{PLUGIN_NAME}",
|
|
)
|
|
|
|
def codes_page():
|
|
return redirect(url_for(f"{PLUGIN_NAME}.admin_codes"))
|
|
|
|
def users_page(affiliation):
|
|
return redirect(url_for(f"{PLUGIN_NAME}.admin_users", affiliation=affiliation))
|
|
|
|
@blueprint.route("/admin/codes", methods=["GET"])
|
|
@admins_only
|
|
def admin_codes():
|
|
codes = AffiliationCodes.query.order_by(AffiliationCodes.id.desc()).all()
|
|
return _render("affiliations/codes.html", codes=codes)
|
|
|
|
@blueprint.route("/admin/codes", methods=["POST"])
|
|
@admins_only
|
|
def admin_create_code():
|
|
code, affiliation, max_uses, errors = _read_code_form()
|
|
for error in errors:
|
|
flash(error, ERRORS)
|
|
if not errors:
|
|
db.session.add(
|
|
AffiliationCodes(code=code, affiliation=affiliation, max_uses=max_uses)
|
|
)
|
|
db.session.commit()
|
|
flash(f"Code {code} created", INFOS)
|
|
return codes_page()
|
|
|
|
@blueprint.route("/admin/codes/<int:code_id>", methods=["POST"])
|
|
@admins_only
|
|
def admin_update_code(code_id):
|
|
code = AffiliationCodes.query.filter_by(id=code_id).first_or_404()
|
|
value, affiliation, max_uses, errors = _read_code_form(code_id=code.id)
|
|
for error in errors:
|
|
flash(error, ERRORS)
|
|
if errors:
|
|
return codes_page()
|
|
|
|
renamed = []
|
|
if affiliation != code.affiliation:
|
|
renamed = [
|
|
m.user_id for m in AffiliationCodeUsers.query.filter_by(code_id=code.id)
|
|
]
|
|
if renamed:
|
|
Users.query.filter(Users.id.in_(renamed)).update(
|
|
{"affiliation": affiliation}, synchronize_session=False
|
|
)
|
|
code.code = value
|
|
code.affiliation = affiliation
|
|
code.max_uses = max_uses
|
|
code.enabled = request.form.get("enabled") == "y"
|
|
db.session.commit()
|
|
_refresh_users(renamed)
|
|
|
|
flash(f"Code {value} updated", INFOS)
|
|
return codes_page()
|
|
|
|
@blueprint.route("/admin/codes/<int:code_id>/delete", methods=["POST"])
|
|
@admins_only
|
|
def admin_delete_code(code_id):
|
|
code = AffiliationCodes.query.filter_by(id=code_id).first_or_404()
|
|
# Members keep their affiliation but may change it again
|
|
AffiliationCodeUsers.query.filter_by(code_id=code.id).delete()
|
|
db.session.delete(code)
|
|
db.session.commit()
|
|
flash(f"Code {code.code} deleted", INFOS)
|
|
return codes_page()
|
|
|
|
@blueprint.route("/admin/users", methods=["GET"])
|
|
@admins_only
|
|
def admin_users():
|
|
affiliations = {}
|
|
for code in AffiliationCodes.query.order_by(AffiliationCodes.affiliation):
|
|
affiliations[code.affiliation] = affiliations.get(code.affiliation, 0) + code.uses
|
|
|
|
selected = request.args.get("affiliation")
|
|
codes, members = [], []
|
|
if selected in affiliations:
|
|
codes = (
|
|
AffiliationCodes.query.filter_by(affiliation=selected)
|
|
.order_by(AffiliationCodes.id)
|
|
.all()
|
|
)
|
|
members = (
|
|
AffiliationCodeUsers.query.join(Users)
|
|
.filter(AffiliationCodeUsers.code_id.in_([c.id for c in codes]))
|
|
.order_by(AffiliationCodeUsers.created.desc())
|
|
.all()
|
|
)
|
|
else:
|
|
selected = None
|
|
|
|
return _render(
|
|
"affiliations/users.html",
|
|
affiliations=affiliations,
|
|
selected=selected,
|
|
codes=codes,
|
|
members=members,
|
|
)
|
|
|
|
@blueprint.route("/admin/users", methods=["POST"])
|
|
@admins_only
|
|
def admin_add_user():
|
|
code = AffiliationCodes.query.filter_by(
|
|
id=request.form.get("code_id", type=int)
|
|
).first_or_404()
|
|
user = Users.query.filter_by(id=request.form.get("user_id", type=int)).first()
|
|
if user is None:
|
|
flash("User not found", ERRORS)
|
|
return users_page(code.affiliation)
|
|
|
|
member = AffiliationCodeUsers.query.filter_by(user_id=user.id).first()
|
|
if member is not None and member.code_id == code.id:
|
|
flash(f"{user.name} is already a member", ERRORS)
|
|
return users_page(code.affiliation)
|
|
if member is not None:
|
|
db.session.delete(member)
|
|
|
|
user.affiliation = code.affiliation
|
|
db.session.add(AffiliationCodeUsers(user_id=user.id, code_id=code.id))
|
|
db.session.commit()
|
|
_refresh_users([user.id])
|
|
|
|
flash(f"{user.name} added to {code.affiliation}", INFOS)
|
|
return users_page(code.affiliation)
|
|
|
|
@blueprint.route("/admin/users/<int:user_id>/delete", methods=["POST"])
|
|
@admins_only
|
|
def admin_remove_user(user_id):
|
|
member = AffiliationCodeUsers.query.filter_by(user_id=user_id).first_or_404()
|
|
affiliation = member.code.affiliation
|
|
member.user.affiliation = None
|
|
db.session.delete(member)
|
|
db.session.commit()
|
|
_refresh_users([user_id])
|
|
|
|
flash("User removed from the affiliation", INFOS)
|
|
return users_page(affiliation)
|
|
|
|
app.register_blueprint(blueprint)
|