diff --git a/app/app.py b/app/app.py index 87207b4..4ff4dc8 100644 --- a/app/app.py +++ b/app/app.py @@ -18,6 +18,7 @@ from utils import * from library import * import titledb +import os def init(): global watcher @@ -37,6 +38,9 @@ def init(): titledb.update_titledb(app_settings) load_titledb(app_settings) + + + os.makedirs(CONFIG_DIR, exist_ok=True) os.makedirs(DATA_DIR, exist_ok=True) @@ -451,6 +455,9 @@ def on_library_change(events): if __name__ == '__main__': logger.info('Starting initialization of Ownfoil...') init() + with app.app_context(): + init_user_from_environment(environment_name="USER_ADMIN", admin=True) + init_user_from_environment(environment_name="USER_GUEST", admin=False) logger.info('Initialization steps done, starting server...') app.run(debug=False, host="0.0.0.0", port=8465) # Shutdown server diff --git a/app/auth.py b/app/auth.py index 7ddb855..6f5210d 100644 --- a/app/auth.py +++ b/app/auth.py @@ -94,7 +94,52 @@ def basic_auth(request): login_manager = LoginManager() login_manager.login_view = 'auth.login' +def create_or_update_user(username, password, admin_access=False, shop_access=False, backup_access=False): + """ + Create a new user or update an existing user with the given credentials and access rights. + """ + user = User.query.filter_by(user=username).first() + if user: + logger.info(f'Updating an existing user {username}') + user.admin_access = admin_access + user.shop_access = shop_access + user.backup_access = backup_access + user.password = generate_password_hash(password, method='scrypt') + else: + logger.info(f'Creating a new user {username}') + new_user = User(user=username, password=generate_password_hash(password, method='scrypt'), admin_access=admin_access, shop_access=shop_access, backup_access=backup_access) + db.session.add(new_user) + db.session.commit() +def init_user_from_environment(environment_name, admin=False): + """ + allow to init some user from environment variable to init some users without using the UI + """ + + + username = os.getenv(environment_name + '_NAME') + password = os.getenv(environment_name + '_PASSWORD') + if username and password: + if admin: + logger.info('Initializing an admin user from environment variable...') + admin_access = True + shop_access = True + backup_access = True + else: + logger.info('Initializing a regular user from environment variable...') + admin_access = False + shop_access = True + backup_access = False + + if not admin: + existing_admin = admin_account_created() + if not existing_admin and not admin_access: + logger.error(f'Error creating user {username}, first account created must be admin') + return + + create_or_update_user(username, password, admin_access, shop_access, backup_access) + + @auth_blueprint.route("/login", methods=["GET", "POST"]) def login(): if request.method == "GET": @@ -192,11 +237,7 @@ def signup_post(): return jsonify(resp) # create a new user with the form data. Hash the password so the plaintext version isn't saved. - new_user = User(user=username, password=generate_password_hash(password, method='scrypt'), admin_access=admin_access, shop_access=shop_access, backup_access=backup_access) - - # add the new user to the database - db.session.add(new_user) - db.session.commit() + create_or_update_user(username, password, admin_access, shop_access, backup_access) logger.info(f'Successfully created user {username}.') diff --git a/docker-compose.yml b/docker-compose.yml index ef29286..02f89e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,8 +8,14 @@ services: # For write permission in config directory - PUID=1000 - PGID=1000 + # to create/update an admin user at startup + # - USER_ADMIN_NAME=admin + # - USER_ADMIN_PASSWORD=asdvnf!546 + # to create/update a regular user at startup + # - USER_GUEST_NAME=guest + # - USER_GUEST_PASSWORD=oerze!@8981 volumes: - /your/game/directory:/games - ./config:/app/config ports: - - "8465:8465" \ No newline at end of file + - "8465:8465"