Allow users to customize their Discourse username

This commit is contained in:
Tanner Collin 2021-10-02 04:44:33 +00:00
parent b0b480c95b
commit bef5ac437a
8 changed files with 52 additions and 3 deletions

View File

@ -32,8 +32,8 @@ class Command(BaseCommand):
'protospace_instructors': [],
}
for user in User.objects.all():
username = user.username
for user in User.objects.filter(member__discourse_username__isnull=False):
username = user.member.discourse_username
# handle non-member vs. member
if user.member.paused_date:

View File

@ -58,6 +58,7 @@ class Member(models.Model):
paused_date = models.DateField(blank=True, null=True)
monthly_fees = models.IntegerField(default=55, blank=True, null=True)
is_allowed_entry = models.BooleanField(default=True)
discourse_username = models.CharField(default=None, max_length=40, blank=True, null=True)
history = HistoricalRecords(excluded_fields=['member_forms'])

View File

@ -210,6 +210,18 @@ class MemberSerializer(serializers.ModelSerializer):
instance.photo_medium = medium
instance.photo_large = large
if 'discourse_username' in validated_data:
changed = validated_data['discourse_username'] != instance.discourse_username
if changed and utils_auth.discourse_is_configured():
username = instance.discourse_username
new_username = validated_data['discourse_username']
logger.info('Changing discourse_username from %s to %s', username, new_username)
if utils_auth.change_discourse_username(username, new_username) != 200:
msg = 'Problem connecting to Discourse Auth server: change username.'
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(discourse_username='Invalid Discourse username.'))
return super().update(instance, validated_data)
# admin viewing member details
@ -599,6 +611,8 @@ class MyPasswordChangeSerializer(PasswordChangeSerializer):
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
data['username'] = self.user.member.discourse_username or self.user.username
if utils_auth.discourse_is_configured():
if request_id: utils_stats.set_progress(request_id, 'Changing Discourse password...')
if utils_auth.set_discourse_password(data) != 200:
@ -606,6 +620,9 @@ class MyPasswordChangeSerializer(PasswordChangeSerializer):
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
if not self.user.member.discourse_username:
self.user.member.discourse_username = self.user.username
self.user.member.save()
if request_id: utils_stats.set_progress(request_id, 'Changing Spaceport password...')
time.sleep(1)
@ -657,6 +674,8 @@ class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer):
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
data['username'] = self.user.member.discourse_username or self.user.username
if utils_auth.discourse_is_configured():
if request_id: utils_stats.set_progress(request_id, 'Changing Discourse password...')
if utils_auth.set_discourse_password(data) != 200:
@ -664,6 +683,9 @@ class MyPasswordResetConfirmSerializer(PasswordResetConfirmSerializer):
utils.alert_tanner(msg)
logger.info(msg)
raise ValidationError(dict(non_field_errors=msg))
if not self.user.member.discourse_username:
self.user.member.discourse_username = self.user.username
self.user.member.save()
member = self.user.member
logging.info('Password reset completed for: {} {} ({})'.format(member.first_name, member.last_name, member.id))
@ -718,6 +740,12 @@ class SpaceportAuthSerializer(LoginSerializer):
data['first_name'] = user.member.first_name
utils_auth.set_wiki_password(data)
data['username'] = user.member.discourse_username or user.username
utils_auth.set_discourse_password(data)
if not user.member.discourse_username:
user.member.discourse_username = user.username
user.member.save()
return user

View File

@ -414,6 +414,9 @@ def register_user(data, user):
msg = 'Problem connecting to Discourse Auth server: set.'
utils.alert_tanner(msg)
logger.info(msg)
if not user.member.discourse_username:
user.member.discourse_username = user.username
user.member.save()
if utils_auth.discourse_is_configured():
if data['request_id']: utils_stats.set_progress(data['request_id'], 'Adding to Discourse group...')

View File

@ -51,3 +51,10 @@ def remove_discourse_group_members(group_name, usernames):
usernames=usernames,
)
return auth_api(secrets.DISCOURSE_AUTH_API_URL + 'remove-discourse-group-members', json=json)
def change_discourse_username(username, new_username):
data = dict(
username=username,
new_username=new_username,
)
return auth_api(secrets.DISCOURSE_AUTH_API_URL + 'change-discourse-username', data=data)

View File

@ -259,6 +259,11 @@ export function AccountForm(props) {
{...makeProps('emergency_contact_phone')}
/>
{member.discourse_username && <Form.Input
label='Discourse Username'
{...makeProps('discourse_username')}
/>}
<Form.Input
label='Member Photo'
name='photo'

View File

@ -534,6 +534,11 @@ export function AdminMemberInfo(props) {
<Table.Cell>{member.user ? 'Yes' : 'No'}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Discourse Username:</Table.Cell>
<Table.Cell>{member.discourse_username || '?'}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Public Bio:</Table.Cell>
</Table.Row>

View File

@ -137,7 +137,7 @@ export function AuthDiscourse(props) {
<Header size='small'>Success!</Header>
<p>You can now log into the Discourse:</p>
<p>
Username: {user.username}<br/>
Username: {user.member.discourse_username || user.username}<br/>
Password: [this Spaceport password]
</p>
<p><a href='https://forum.protospace.ca' rel='noopener noreferrer'>Protospace Discourse</a></p>