Add route to change Discourse username

This commit is contained in:
Tanner Collin 2021-10-14 22:47:10 +00:00
parent ee8dd8897e
commit 7b74a58720
2 changed files with 33 additions and 3 deletions

View File

@ -211,9 +211,11 @@ def set_discourse_password(username, password, first_name, email):
'email': email, 'email': email,
'active': True, 'active': True,
'approved': True, 'approved': True,
'user_fields[10]': 'Spaceport auth'
} }
response = discourse_api_post('https://forum.protospace.ca/users.json', data) response = discourse_api_post('https://forum.protospace.ca/users.json', data)
response = response.json() response = response.json()
logger.info('Response: %s', response)
if response['success']: if response['success']:
logger.info('Skipping set password') logger.info('Skipping set password')
@ -309,10 +311,27 @@ def remove_discourse_group_members(group_name, usernames):
discourse_api_delete(url, data) discourse_api_delete(url, data)
return True return True
def change_discourse_username(username, new_username):
if not username:
logger.error('Empty username, aborting')
abort(400)
if not new_username:
logger.error('Empty new_username, aborting')
abort(400)
logger.info('Changing username %s to %s...', username, new_username)
url = 'https://forum.protospace.ca/users/{}/preferences/username'.format(username)
data = {
'new_username': new_username,
}
discourse_api_put(url, data)
return True
if __name__ == '__main__': if __name__ == '__main__':
#set_wiki_password('tanner.collin', 'protospace1') #set_wiki_password('tanner.collin', 'protospace1')
#set_discourse_password('test8a', 'protospace1', 'testie', 'test8@example.com') set_discourse_password('test8a', 'protospace1', 'testie', 'test8@example.com')
print(get_discourse_usernames()) #for u in get_discourse_usernames():
pass # print(u)
#pass

View File

@ -11,6 +11,7 @@ HTTP_UNAUTHORIZED = 401
def check_auth(): def check_auth():
auth_header = request.headers.get('Authorization', '') auth_header = request.headers.get('Authorization', '')
if auth_header != 'Token ' + secrets.AUTH_TOKEN: if auth_header != 'Token ' + secrets.AUTH_TOKEN:
logger.info('Bad auth token, aborting.')
abort(HTTP_UNAUTHORIZED) abort(HTTP_UNAUTHORIZED)
@app.route('/') @app.route('/')
@ -67,5 +68,15 @@ def remove_discourse_group_members():
auth_functions.remove_discourse_group_members(group_name, usernames) auth_functions.remove_discourse_group_members(group_name, usernames)
return '' return ''
@app.route('/change-discourse-username', methods=['POST'])
def change_discourse_username():
check_auth()
username = request.form['username']
new_username = request.form['new_username']
auth_functions.change_discourse_username(username, new_username)
return ''
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0') app.run(debug=True, host='0.0.0.0')