2 # Copyright (C) 2018 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 from flask import redirect, render_template, session, request, flash, url_for
20 from flask_user import current_user
21 from app.models import db, User, UserRank
22 from app.utils import randomString, loginUser, rank_required
23 from app.tasks.forumtasks import checkForumAccount
24 from app.tasks.phpbbparser import getProfile
26 @bp.route("/user/claim/", methods=["GET", "POST"])
28 username = request.args.get("username")
32 method = request.args.get("method")
33 user = User.query.filter_by(forums_username=username).first()
34 if user and user.rank.atLeast(UserRank.NEW_MEMBER):
35 flash("User has already been claimed", "danger")
36 return redirect(url_for("users.claim"))
37 elif method == "github":
38 if user is None or user.github_username is None:
39 flash("Unable to get Github username for user", "danger")
40 return redirect(url_for("users.claim"))
42 return redirect(url_for("github.start"))
43 elif user is None and request.method == "POST":
44 flash("Unable to find user", "danger")
45 return redirect(url_for("users.claim"))
49 if "forum_token" in session:
50 token = session["forum_token"]
52 token = randomString(32)
53 session["forum_token"] = token
55 if request.method == "POST":
56 ctype = request.form.get("claim_type")
57 username = request.form.get("username")
59 if username is None or len(username.strip()) < 2:
60 flash("Invalid username", "danger")
61 elif ctype == "github":
62 task = checkForumAccount.delay(username)
63 return redirect(url_for("tasks.check", id=task.id, r=url_for("users.claim", username=username, method="github")))
64 elif ctype == "forum":
65 user = User.query.filter_by(forums_username=username).first()
66 if user is not None and user.rank.atLeast(UserRank.NEW_MEMBER):
67 flash("That user has already been claimed!", "danger")
68 return redirect(url_for("users.claim"))
73 profile = getProfile("https://forum.minetest.net", username)
74 sig = profile.signature if profile else None
76 if hasattr(e, 'message'):
81 flash("Error whilst attempting to access forums: " + message, "danger")
82 return redirect(url_for("users.claim", username=username))
85 flash("Unable to get forum signature - does the user exist?", "danger")
86 return redirect(url_for("users.claim", username=username))
92 user.forums_username = username
97 return redirect(url_for("users.set_password"))
99 flash("Unable to login as user", "danger")
100 return redirect(url_for("users.claim", username=username))
103 flash("Could not find the key in your signature!", "danger")
104 return redirect(url_for("users.claim", username=username))
106 flash("Unknown claim type", "danger")
108 return render_template("users/claim.html", username=username, key=token)