156 lines
5.3 KiB
Python
156 lines
5.3 KiB
Python
import functools
|
|
|
|
from flask import g, jsonify, render_template, request
|
|
from flask_babel import lazy_gettext as _l
|
|
from werkzeug.datastructures import ImmutableMultiDict
|
|
from wtforms import StringField
|
|
from wtforms.validators import Optional
|
|
|
|
import CTFd.forms.auth as auth_forms
|
|
import CTFd.forms.self as self_forms
|
|
from CTFd.models import Users, db
|
|
from CTFd.utils import get_config
|
|
from CTFd.utils.user import authed, get_current_user, is_admin
|
|
|
|
from .models import AffiliationCodes, AffiliationCodeUsers
|
|
|
|
LOCKED_MESSAGE = "Your affiliation was set by a registration code and cannot be changed"
|
|
|
|
|
|
def system_code():
|
|
return str(get_config("registration_code", default="") or "")
|
|
|
|
|
|
def patch_registration_form():
|
|
original_attach = auth_forms.attach_registration_code_field
|
|
|
|
def attach_registration_code_field(form_cls):
|
|
if system_code():
|
|
return original_attach(form_cls)
|
|
if AffiliationCodes.any_enabled():
|
|
form_cls.registration_code = StringField(
|
|
"Registration Code",
|
|
description="Organization code, if you have one. It sets your affiliation",
|
|
validators=[Optional()],
|
|
)
|
|
|
|
def build_registration_code_field(form):
|
|
field = getattr(form, "registration_code", None)
|
|
if field is None:
|
|
return []
|
|
field.field_type = "text"
|
|
return [field]
|
|
|
|
auth_forms.attach_registration_code_field = attach_registration_code_field
|
|
auth_forms.build_registration_code_field = build_registration_code_field
|
|
|
|
|
|
def _register_error(error):
|
|
return render_template(
|
|
"register.html",
|
|
errors=[error],
|
|
name=request.form.get("name", ""),
|
|
email=request.form.get("email", ""),
|
|
password=request.form.get("password", ""),
|
|
)
|
|
|
|
|
|
def wrap_register_view(app):
|
|
original = app.view_functions["auth.register"]
|
|
|
|
@functools.wraps(original)
|
|
def register(*args, **kwargs):
|
|
if request.method != "POST" or authed():
|
|
return original(*args, **kwargs)
|
|
|
|
entered = request.form.get("registration_code", "").strip()
|
|
sys_code = system_code()
|
|
if not entered or entered.lower() == sys_code.lower():
|
|
return original(*args, **kwargs)
|
|
|
|
code = AffiliationCodes.find(entered)
|
|
if code is None or not code.enabled:
|
|
if sys_code:
|
|
return original(*args, **kwargs)
|
|
return _register_error(_l("The registration code you entered was incorrect"))
|
|
if code.exhausted:
|
|
return _register_error(_l("This registration code has reached its usage limit"))
|
|
|
|
code_id, affiliation = code.id, code.affiliation
|
|
form = request.form.copy()
|
|
form["affiliation"] = affiliation
|
|
if sys_code:
|
|
form["registration_code"] = sys_code
|
|
request.form = ImmutableMultiDict(form)
|
|
|
|
response = original(*args, **kwargs)
|
|
|
|
user = get_current_user() if authed() else None
|
|
if user is not None and user.affiliation == affiliation:
|
|
AffiliationCodeUsers.query.filter_by(user_id=user.id).delete()
|
|
db.session.add(AffiliationCodeUsers(user_id=user.id, code_id=code_id))
|
|
db.session.commit()
|
|
return response
|
|
|
|
app.view_functions["auth.register"] = register
|
|
|
|
|
|
def patch_settings_form():
|
|
original = self_forms.SettingsForm
|
|
|
|
def SettingsForm(*args, **kwargs):
|
|
form = original(*args, **kwargs)
|
|
user = get_current_user()
|
|
if user is not None and AffiliationCodeUsers.is_locked(user.id):
|
|
form.affiliation.render_kw = {"readonly": True, "title": LOCKED_MESSAGE}
|
|
return form
|
|
|
|
self_forms.SettingsForm = SettingsForm
|
|
|
|
|
|
def _affiliation_change_target():
|
|
"""The user whose affiliation this API request changes, if any."""
|
|
if request.method != "PATCH":
|
|
return None
|
|
if request.endpoint == "api.users_user_private":
|
|
user = get_current_user()
|
|
elif request.endpoint == "api.users_user_public" and is_admin():
|
|
user = Users.query.filter_by(id=request.view_args["user_id"]).first()
|
|
else:
|
|
return None
|
|
|
|
data = request.get_json(silent=True)
|
|
if user is None or not isinstance(data, dict) or "affiliation" not in data:
|
|
return None
|
|
if (data["affiliation"] or "") == (user.affiliation or ""):
|
|
return None
|
|
return user
|
|
|
|
|
|
def register_affiliation_guard(app):
|
|
@app.before_request
|
|
def guard_locked_affiliation():
|
|
if not authed():
|
|
return
|
|
user = _affiliation_change_target()
|
|
if user is None or not AffiliationCodeUsers.is_locked(user.id):
|
|
return
|
|
if is_admin():
|
|
# The admin overrides the code: unlock once the change is saved
|
|
g.affiliation_unlock_user_id = user.id
|
|
return
|
|
return jsonify({"success": False, "errors": {"affiliation": [LOCKED_MESSAGE]}}), 400
|
|
|
|
@app.before_request
|
|
def track_user_deletion():
|
|
if request.method == "DELETE" and request.endpoint == "api.users_user_public":
|
|
g.affiliation_unlock_user_id = request.view_args["user_id"]
|
|
|
|
@app.after_request
|
|
def drop_membership(response):
|
|
user_id = g.pop("affiliation_unlock_user_id", None)
|
|
if user_id is not None and response.status_code == 200:
|
|
AffiliationCodeUsers.query.filter_by(user_id=user_id).delete()
|
|
db.session.commit()
|
|
return response
|