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")
34 user = User.query.filter_by(forums_username=username).first()
35 if user and user.rank.atLeast(UserRank.NEW_MEMBER):
36 flash("User has already been claimed", "danger")
37 return redirect(url_for("users.claim"))
38 elif method == "github":
39 if user is None or user.github_username is None:
40 flash("Unable to get Github username for user", "danger")
41 return redirect(url_for("users.claim", username=username))
43 return redirect(url_for("github.start"))
44 elif user is None and request.method == "POST":
45 flash("Unable to find user", "danger")
46 return redirect(url_for("users.claim"))
50 if "forum_token" in session:
51 token = session["forum_token"]
53 token = randomString(32)
54 session["forum_token"] = token
56 if request.method == "POST":
57 ctype = request.form.get("claim_type")
58 username = request.form.get("username")
60 if username is None or len(username.strip()) < 2:
61 flash("Invalid username", "danger")
62 elif ctype == "github":
63 task = checkForumAccount.delay(username)
64 return redirect(url_for("tasks.check", id=task.id, r=url_for("users.claim", username=username, method="github")))
65 elif ctype == "forum":
66 user = User.query.filter_by(forums_username=username).first()
67 if user is not None and user.rank.atLeast(UserRank.NEW_MEMBER):
68 flash("That user has already been claimed!", "danger")
69 return redirect(url_for("users.claim"))
74 profile = getProfile("https://forum.minetest.net", username)
75 sig = profile.signature if profile else None
77 if hasattr(e, 'message'):
82 flash("Error whilst attempting to access forums: " + message, "danger")
83 return redirect(url_for("users.claim", username=username))
86 flash("Unable to get forum signature - does the user exist?", "danger")
87 return redirect(url_for("users.claim", username=username))
90 if sig and token in sig:
93 user.forums_username = username
98 return redirect(url_for("users.set_password"))
100 flash("Unable to login as user", "danger")
101 return redirect(url_for("users.claim", username=username))
104 flash("Could not find the key in your signature!", "danger")
105 return redirect(url_for("users.claim", username=username))
107 flash("Unknown claim type", "danger")
109 return render_template("users/claim.html", username=username, key=token)