2020-07-12 17:34:25 +02:00
# ContentDB
2021-01-30 17:59:42 +01:00
# Copyright (C) 2018-21 rubenwardy
2020-01-24 19:15:09 +01:00
#
# This program is free software: you can redistribute it and/or modify
2021-01-30 17:59:42 +01:00
# it under the terms of the GNU Affero General Public License as published by
2020-01-24 19:15:09 +01:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021-01-30 17:59:42 +01:00
# GNU Affero General Public License for more details.
2020-01-24 19:15:09 +01:00
#
2021-01-30 17:59:42 +01:00
# You should have received a copy of the GNU Affero General Public License
2020-01-24 19:15:09 +01:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-06-19 20:32:36 +02:00
2022-01-07 22:46:16 +01:00
from flask_babel import gettext
2020-01-24 19:15:09 +01:00
from . import bp
from flask import redirect , render_template , session , request , flash , url_for
from app . models import db , User , UserRank
2023-06-19 22:27:49 +02:00
from app . utils import random_string , login_user_set_active , is_username_valid
from app . tasks . forumtasks import check_forum_account
from app . utils . phpbbparser import get_profile
2020-12-15 13:29:25 +01:00
2020-12-22 11:58:43 +01:00
2020-01-24 19:15:09 +01:00
@bp.route ( " /user/claim/ " , methods = [ " GET " , " POST " ] )
def claim ( ) :
2020-12-22 11:58:43 +01:00
return render_template ( " users/claim.html " )
@bp.route ( " /user/claim-forums/ " , methods = [ " GET " , " POST " ] )
def claim_forums ( ) :
2020-01-24 19:15:09 +01:00
username = request . args . get ( " username " )
if username is None :
username = " "
else :
method = request . args . get ( " method " )
2020-07-10 19:41:08 +02:00
2022-01-21 22:20:04 +01:00
if not is_username_valid ( username ) :
2022-06-05 01:54:09 +02:00
flash ( gettext ( " Invalid username, Only alphabetic letters (A-Za-z), numbers (0-9), underscores (_), minuses (-), and periods (.) allowed. Consider contacting an admin " ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " ) )
2020-12-04 00:31:01 +01:00
2020-01-24 19:15:09 +01:00
user = User . query . filter_by ( forums_username = username ) . first ( )
2023-06-19 22:27:49 +02:00
if user and user . rank . at_least ( UserRank . NEW_MEMBER ) :
2022-01-07 22:46:16 +01:00
flash ( gettext ( " User has already been claimed " ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " ) )
2020-04-14 15:39:49 +02:00
elif method == " github " :
if user is None or user . github_username is None :
2022-01-07 22:46:16 +01:00
flash ( gettext ( " Unable to get GitHub username for user " ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " , username = username ) )
2020-04-14 15:39:49 +02:00
else :
2024-06-22 12:42:51 +02:00
return redirect ( url_for ( " vcs.github_start " ) )
2020-01-24 19:15:09 +01:00
if " forum_token " in session :
token = session [ " forum_token " ]
else :
2023-06-19 22:27:49 +02:00
token = random_string ( 12 )
2020-01-24 19:15:09 +01:00
session [ " forum_token " ] = token
if request . method == " POST " :
2023-06-19 20:32:36 +02:00
ctype = request . form . get ( " claim_type " )
2020-01-24 19:15:09 +01:00
username = request . form . get ( " username " )
2022-01-21 22:20:04 +01:00
if not is_username_valid ( username ) :
2022-06-05 01:54:09 +02:00
flash ( gettext ( " Invalid username, Only alphabetic letters (A-Za-z), numbers (0-9), underscores (_), minuses (-), and periods (.) allowed. Consider contacting an admin " ) , " danger " )
2020-01-24 19:15:09 +01:00
elif ctype == " github " :
2023-06-19 22:27:49 +02:00
task = check_forum_account . delay ( username )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " tasks.check " , id = task . id , r = url_for ( " users.claim_forums " , username = username , method = " github " ) ) )
2020-01-24 19:15:09 +01:00
elif ctype == " forum " :
user = User . query . filter_by ( forums_username = username ) . first ( )
2023-06-19 22:27:49 +02:00
if user is not None and user . rank . at_least ( UserRank . NEW_MEMBER ) :
2022-01-07 22:46:16 +01:00
flash ( gettext ( " That user has already been claimed! " ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " ) )
2020-01-24 19:15:09 +01:00
# Get signature
try :
2023-06-19 22:27:49 +02:00
profile = get_profile ( " https://forum.minetest.net " , username )
2020-04-14 15:39:49 +02:00
sig = profile . signature if profile else None
except IOError as e :
if hasattr ( e , ' message ' ) :
message = e . message
else :
message = str ( e )
2022-01-07 22:46:16 +01:00
flash ( gettext ( u " Error whilst attempting to access forums: %(message)s " , message = message ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " , username = username ) )
2020-04-14 15:39:49 +02:00
if profile is None :
2022-01-07 22:46:16 +01:00
flash ( gettext ( " Unable to get forum signature - does the user exist? " ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " , username = username ) )
2020-01-24 19:15:09 +01:00
# Look for key
2020-09-15 16:48:03 +02:00
if sig and token in sig :
2020-12-04 00:31:01 +01:00
# Try getting again to fix crash
user = User . query . filter_by ( forums_username = username ) . first ( )
2020-01-24 19:15:09 +01:00
if user is None :
user = User ( username )
user . forums_username = username
db . session . add ( user )
db . session . commit ( )
2021-07-20 00:04:40 +02:00
ret = login_user_set_active ( user , remember = True )
if ret is None :
2022-01-07 22:46:16 +01:00
flash ( gettext ( " Unable to login as user " ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " , username = username ) )
2020-01-24 19:15:09 +01:00
2021-07-20 00:04:40 +02:00
return ret
2020-01-24 19:15:09 +01:00
else :
2022-01-07 22:46:16 +01:00
flash ( gettext ( " Could not find the key in your signature! " ) , " danger " )
2020-12-22 11:58:43 +01:00
return redirect ( url_for ( " users.claim_forums " , username = username ) )
2020-01-24 19:15:09 +01:00
else :
2022-01-07 22:46:16 +01:00
flash ( gettext ( " Unknown claim type " ) , " danger " )
2020-01-24 19:15:09 +01:00
2020-12-22 11:58:43 +01:00
return render_template ( " users/claim_forums.html " , username = username , key = " cdb_ " + token )