simplified super admin creation
This commit is contained in:
parent
4470d2fb82
commit
e8c1effd15
|
@ -12,18 +12,22 @@ db = SQLAlchemy()
|
||||||
|
|
||||||
|
|
||||||
# create admin users (only if they don't exists already)
|
# create admin users (only if they don't exists already)
|
||||||
def create_super_admins(app, db, user_datastore, logger):
|
def create_super_admins(app, user_datastore):
|
||||||
|
admin_file = Path(app.config.get('ADMIN_FILE'))
|
||||||
|
|
||||||
# setup user database when starting the app
|
# setup user database when starting the app
|
||||||
with app.app_context():
|
|
||||||
new_admin_data = []
|
new_admin_data = []
|
||||||
if app.config['ADMIN_FILE'] is not None:
|
if not admin_file.exists():
|
||||||
if not Path(app.config['ADMIN_FILE']).exists():
|
app.logger.warning(
|
||||||
logger.warning(
|
f"Admin user creation file not found at path "
|
||||||
f"Admin user creation file not found at {app.config['ADMIN_FILE']}")
|
f"{admin_file.absolute()}."
|
||||||
|
f"No super admins have been created in the datastore."
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
# store data for new admins in memory s.t. the file can be deleted afterwards
|
# store data for new admins in memory s.t. the file can be deleted
|
||||||
with open(app.config['ADMIN_FILE']) as f:
|
# afterwards
|
||||||
for i, line in enumerate(f.readlines()):
|
admin_data = admin_file.read_text().split('\n')
|
||||||
|
for i, line in enumerate(admin_data):
|
||||||
if not line.strip().startswith('#'):
|
if not line.strip().startswith('#'):
|
||||||
try:
|
try:
|
||||||
user, email, pw = line.split()
|
user, email, pw = line.split()
|
||||||
|
@ -32,10 +36,12 @@ def create_super_admins(app, db, user_datastore, logger):
|
||||||
{'username': user, 'email': email,
|
{'username': user, 'email': email,
|
||||||
'password': pw})
|
'password': pw})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(
|
app.logger.error(
|
||||||
f"Error while parsing line {i} in admin config file. Config file should contain lines of "
|
f"Error while parsing line {i} in admin config file. Config file should contain lines of "
|
||||||
f"'<username> <email> <password>\\n'\n Exception: {e}\nAdmin account could not be created.")
|
f"'<username> <email> <password>\\n'\n Exception: {e}\nAdmin account could not be created."
|
||||||
|
)
|
||||||
|
|
||||||
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
super_admin_role = user_datastore.find_or_create_role(
|
super_admin_role = user_datastore.find_or_create_role(
|
||||||
'super_admin') # root admin = can create other admins
|
'super_admin') # root admin = can create other admins
|
||||||
|
@ -50,16 +56,18 @@ def create_super_admins(app, db, user_datastore, logger):
|
||||||
roles = [super_admin_role, admin_role]
|
roles = [super_admin_role, admin_role]
|
||||||
if not d['password'] == 'LDAP':
|
if not d['password'] == 'LDAP':
|
||||||
roles.append(local_role)
|
roles.append(local_role)
|
||||||
logger.info(
|
|
||||||
f"New super admin user created with username '{d['username']}' and email '{d['email']}', roles = {[r.name for r in roles]}")
|
|
||||||
|
|
||||||
# create new admin (only if admin does not already exist)
|
# create new admin (only if admin does not already exist)
|
||||||
new_admin = user_datastore.create_user(email=d['email'],
|
new_admin = user_datastore.create_user(
|
||||||
username=d[
|
email=d['email'], username=d['username'],
|
||||||
'username'],
|
password=hash_password(d['password']), roles=roles
|
||||||
password=hash_password(
|
)
|
||||||
d['password']),
|
app.logger.info(
|
||||||
roles=roles)
|
f"New super admin user created with username "
|
||||||
|
f"'{new_admin.username}' and email '{new_admin.email}'"
|
||||||
|
f", roles = {[r.name for r in new_admin.roles]}"
|
||||||
|
)
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@ -99,6 +107,6 @@ def create_app():
|
||||||
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
||||||
security.init_app(app, user_datastore, login_form=ExtendedLoginForm)
|
security.init_app(app, user_datastore, login_form=ExtendedLoginForm)
|
||||||
|
|
||||||
create_super_admins(app, db, user_datastore, logger)
|
create_super_admins(app, user_datastore)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
Loading…
Reference in New Issue
Block a user