Example #1
0
def user_claim_page():
    username = request.args.get("username")
    if username is None:
        username = ""
    else:
        method = request.args.get("method")
        user = User.query.filter_by(forums_username=username).first()
        if user and user.rank.atLeast(UserRank.NEW_MEMBER):
            flash("User has already been claimed", "error")
            return redirect(url_for("user_claim_page"))
        elif user is None and method == "github":
            flash("Unable to get Github username for user", "error")
            return redirect(url_for("user_claim_page"))
        elif user is None:
            flash("Unable to find that user", "error")
            return redirect(url_for("user_claim_page"))

        if user is not None and method == "github":
            return redirect(url_for("github_signin_page"))

    token = None
    if "forum_token" in session:
        token = session["forum_token"]
    else:
        token = randomString(32)
        session["forum_token"] = token

    if request.method == "POST":
        ctype = request.form.get("claim_type")
        username = request.form.get("username")

        if username is None or len(username.strip()) < 2:
            flash("Invalid username", "error")
        elif ctype == "github":
            task = checkForumAccount.delay(username)
            return redirect(
                url_for("check_task",
                        id=task.id,
                        r=url_for("user_claim_page",
                                  username=username,
                                  method="github")))
        elif ctype == "forum":
            user = User.query.filter_by(forums_username=username).first()
            if user is not None and user.rank.atLeast(UserRank.NEW_MEMBER):
                flash("That user has already been claimed!", "error")
                return redirect(url_for("user_claim_page"))

            # Get signature
            sig = None
            try:
                profile = getProfile("https://forum.minetest.net", username)
                sig = profile.signature
            except IOError:
                flash("Unable to get forum signature - does the user exist?",
                      "error")
                return redirect(url_for("user_claim_page", username=username))

            # Look for key
            if token in sig:
                if user is None:
                    user = User(username)
                    user.forums_username = username
                    db.session.add(user)
                    db.session.commit()

                if loginUser(user):
                    return redirect(url_for("set_password_page"))
                else:
                    flash("Unable to login as user", "error")
                    return redirect(
                        url_for("user_claim_page", username=username))

            else:
                flash("Could not find the key in your signature!", "error")
                return redirect(url_for("user_claim_page", username=username))
        else:
            flash("Unknown claim type", "error")

    return render_template("users/claim.html", username=username, key=token)
Example #2
0
def claim_forums():
    username = request.args.get("username")
    if username is None:
        username = ""
    else:
        method = request.args.get("method")

        if not check_username(username):
            flash(
                "Invalid username - must only contain A-Za-z0-9._. Consider contacting an admin",
                "danger")
            return redirect(url_for("users.claim_forums"))

        user = User.query.filter_by(forums_username=username).first()
        if user and user.rank.atLeast(UserRank.NEW_MEMBER):
            flash("User has already been claimed", "danger")
            return redirect(url_for("users.claim_forums"))
        elif method == "github":
            if user is None or user.github_username is None:
                flash("Unable to get GitHub username for user", "danger")
                return redirect(
                    url_for("users.claim_forums", username=username))
            else:
                return redirect(url_for("github.start"))

    if "forum_token" in session:
        token = session["forum_token"]
    else:
        token = randomString(12)
        session["forum_token"] = token

    if request.method == "POST":
        ctype = request.form.get("claim_type")
        username = request.form.get("username")

        if not check_username(username):
            flash(
                "Invalid username - must only contain A-Za-z0-9._. Consider contacting an admin",
                "danger")
        elif ctype == "github":
            task = checkForumAccount.delay(username)
            return redirect(
                url_for("tasks.check",
                        id=task.id,
                        r=url_for("users.claim_forums",
                                  username=username,
                                  method="github")))
        elif ctype == "forum":
            user = User.query.filter_by(forums_username=username).first()
            if user is not None and user.rank.atLeast(UserRank.NEW_MEMBER):
                flash("That user has already been claimed!", "danger")
                return redirect(url_for("users.claim_forums"))

            # Get signature
            sig = None
            try:
                profile = getProfile("https://forum.minetest.net", username)
                sig = profile.signature if profile else None
            except IOError as e:
                if hasattr(e, 'message'):
                    message = e.message
                else:
                    message = str(e)

                flash("Error whilst attempting to access forums: " + message,
                      "danger")
                return redirect(
                    url_for("users.claim_forums", username=username))

            if profile is None:
                flash("Unable to get forum signature - does the user exist?",
                      "danger")
                return redirect(
                    url_for("users.claim_forums", username=username))

            # Look for key
            if sig and token in sig:
                # Try getting again to fix crash
                user = User.query.filter_by(forums_username=username).first()
                if user is None:
                    user = User(username)
                    user.forums_username = username
                    db.session.add(user)
                    db.session.commit()

                if login_user_set_active(user, remember=True):
                    return redirect(url_for("users.set_password"))
                else:
                    flash("Unable to login as user", "danger")
                    return redirect(
                        url_for("users.claim_forums", username=username))

            else:
                flash("Could not find the key in your signature!", "danger")
                return redirect(
                    url_for("users.claim_forums", username=username))
        else:
            flash("Unknown claim type", "danger")

    return render_template("users/claim_forums.html",
                           username=username,
                           key="cdb_" + token)