1975: Replace traceback with error message when creating initial admin user r=mergify[bot] a=ghostwheel42

## What type of PR?

small enhancement

## What does this PR do?

when creating the admin user via cli a traceback is shown when this user is already present in the database.
This is confusing users. I've replaced the traceback with an error message.

### Related issue(s)

#1921

## Prerequisites
Before we can consider review and merge, please make sure the following list is done and checked.
If an entry in not applicable, you can check it or remove it from the list.

- [X] In case of feature or enhancement: documentation updated accordingly
- [X] Unless it's docs or a minor change: add [changelog](https://mailu.io/master/contributors/workflow.html#changelog) entry file.


Co-authored-by: Alexander Graf <ghostwheel42@users.noreply.github.com>
master
bors[bot] 3 years ago committed by GitHub
commit 72e8ec53b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -48,44 +48,44 @@ def advertise():
@click.argument('localpart') @click.argument('localpart')
@click.argument('domain_name') @click.argument('domain_name')
@click.argument('password') @click.argument('password')
@click.option('-m', '--mode') @click.option('-m', '--mode', default='create', metavar='MODE', help='''\b'create' (default): create user. it's an error if user already exists
'ifmissing': only update password if user is missing
'update': create user or update password if user exists
''')
@with_appcontext @with_appcontext
def admin(localpart, domain_name, password, mode='create'): def admin(localpart, domain_name, password, mode):
""" Create an admin user """ Create an admin user
'mode' can be:
- 'create' (default) Will try to create user and will raise an exception if present
- 'ifmissing': if user exists, nothing happens, else it will be created
- 'update': user is created or, if it exists, its password gets updated
""" """
if not mode in ('create', 'update', 'ifmissing'):
raise click.ClickException(f'invalid mode: {mode!r}')
domain = models.Domain.query.get(domain_name) domain = models.Domain.query.get(domain_name)
if not domain: if not domain:
domain = models.Domain(name=domain_name) domain = models.Domain(name=domain_name)
db.session.add(domain) db.session.add(domain)
user = None email = f'{localpart}@{domain_name}'
if mode == 'ifmissing' or mode == 'update': if user := models.User.query.get(email):
email = f'{localpart}@{domain_name}' if mode == 'ifmissing':
user = models.User.query.get(email) print(f'user {email!r} exists, not updating')
if user and mode == 'ifmissing':
print('user %s exists, not updating' % email)
return return
elif mode == 'update':
if not user: user.set_password(password)
db.session.commit()
print("updated admin password")
else:
raise click.ClickException(f'user {email!r} exists, not created')
else:
user = models.User( user = models.User(
localpart=localpart, localpart=localpart,
domain=domain, domain=domain,
global_admin=True global_admin=True
) )
user.set_password(password)
db.session.add(user) db.session.add(user)
user.set_password(password)
db.session.commit() db.session.commit()
print("created admin user") print("created admin user")
elif mode == 'update':
user.set_password(password)
db.session.commit()
print("updated admin password")
@mailu.command() @mailu.command()

Loading…
Cancel
Save