Fix bugs related to Discourse username case

This commit is contained in:
Tanner Collin 2021-09-25 22:59:34 +00:00
parent 596813bee7
commit 441e86ec2b
2 changed files with 43 additions and 10 deletions

View File

@ -121,6 +121,26 @@ def get_discourse_usernames():
return usernames return usernames
def translate_usernames(portal_usernames, discourse_usernames):
# the case of portal and discourse usernames might not match
# this causes a problem if someone creates a discourse user
# as John.Smith and later sets up a portal account as john.smith
#
# solution: look for usernames in discourse with the same letters,
# and then convert to the discourse version when using the API
result = []
for pu in portal_usernames:
for du in discourse_usernames:
if pu.lower() == du.lower():
result.append(du)
break
else: # for
result.append(pu)
return result
def set_discourse_password(username, password, first_name, email): def set_discourse_password(username, password, first_name, email):
# sets a user's discourse password # sets a user's discourse password
# creates the account if it doesn't exist # creates the account if it doesn't exist
@ -152,6 +172,9 @@ def set_discourse_password(username, password, first_name, email):
logger.error('Empty email, aborting') logger.error('Empty email, aborting')
abort(400) abort(400)
discourse_usernames = get_discourse_usernames()
username = translate_usernames([username], discourse_usernames)[0]
logger.info('Checking Discourse for existing email: ' + email) logger.info('Checking Discourse for existing email: ' + email)
params = { params = {
'filter': email, 'filter': email,
@ -161,7 +184,7 @@ def set_discourse_password(username, password, first_name, email):
response = response.json() response = response.json()
for user in response: for user in response:
if user['email'] == email: if user['email'].lower() == email.lower():
if user['username'] == username: if user['username'] == username:
logger.info('Username match, skipping') logger.info('Username match, skipping')
continue continue
@ -220,12 +243,14 @@ def add_discourse_group_members(group_name, usernames):
logger.error('Empty usernames, aborting') logger.error('Empty usernames, aborting')
abort(400) abort(400)
discourse_usernames = get_discourse_usernames()
usernames = translate_usernames(usernames, discourse_usernames)
usernames = set(usernames)
group_id = get_discourse_group_id(group_name) group_id = get_discourse_group_id(group_name)
usernames = set(usernames.split(','))
logger.info('Filtering out usernames not on Discourse...') logger.info('Filtering out usernames not on Discourse...')
discourse_usernames = set(get_discourse_usernames()) discourse_usernames = set(discourse_usernames)
usernames = usernames & discourse_usernames usernames = usernames & discourse_usernames
logger.info('Filtering out usernames that are already group members...') logger.info('Filtering out usernames that are already group members...')
@ -260,16 +285,22 @@ def remove_discourse_group_members(group_name, usernames):
logger.error('Empty usernames, aborting') logger.error('Empty usernames, aborting')
abort(400) abort(400)
discourse_usernames = get_discourse_usernames()
usernames = translate_usernames(usernames, discourse_usernames)
usernames = set(usernames)
group_id = get_discourse_group_id(group_name) group_id = get_discourse_group_id(group_name)
usernames = set(usernames.split(','))
logger.info('Filtering out usernames not on Discourse...') logger.info('Filtering out usernames not on Discourse...')
discourse_usernames = set(get_discourse_usernames()) discourse_usernames = set(discourse_usernames)
usernames = usernames & discourse_usernames usernames = usernames & discourse_usernames
usernames = list(usernames) usernames = list(usernames)
logger.info('Removing usernames from the group...') if not len(usernames):
logger.info('Skipping, no one left to remove')
return True
logger.info('Removing %s remaining usernames from the group...', len(usernames))
url = 'https://forum.protospace.ca/groups/{}/members.json'.format(group_id) url = 'https://forum.protospace.ca/groups/{}/members.json'.format(group_id)
data = { data = {

View File

@ -49,8 +49,9 @@ def set_discourse_password():
def add_discourse_group_members(): def add_discourse_group_members():
check_auth() check_auth()
group_name = request.form['group_name'] data = request.get_json()
usernames = request.form['usernames'] group_name = data['group_name']
usernames = data['usernames']
auth_functions.add_discourse_group_members(group_name, usernames) auth_functions.add_discourse_group_members(group_name, usernames)
return '' return ''
@ -59,8 +60,9 @@ def add_discourse_group_members():
def remove_discourse_group_members(): def remove_discourse_group_members():
check_auth() check_auth()
group_name = request.form['group_name'] data = request.get_json()
usernames = request.form['usernames'] group_name = data['group_name']
usernames = data['usernames']
auth_functions.remove_discourse_group_members(group_name, usernames) auth_functions.remove_discourse_group_members(group_name, usernames)
return '' return ''