implement user authentication flow, session management, and admin user management capabilities
This commit is contained in:
parent
7dcda4b5ef
commit
76565477e7
2 changed files with 141 additions and 25 deletions
17
main.py
17
main.py
|
|
@ -80,17 +80,22 @@ def init_db():
|
|||
)
|
||||
''')
|
||||
|
||||
# Create default admin if no users exist
|
||||
# Create or update default admin if no users exist or only one user exists
|
||||
c.execute('SELECT COUNT(*) FROM users')
|
||||
if c.fetchone()[0] == 0:
|
||||
count = c.fetchone()[0]
|
||||
if count == 0:
|
||||
print("Creating default admin user...")
|
||||
admin_hash = get_password_hash(WEB_PASSWORD)
|
||||
c.execute('INSERT INTO users (username, hashed_password, is_admin) VALUES (?, ?, ?)',
|
||||
('admin', admin_hash, 1))
|
||||
|
||||
# Associate existing instances with the new admin user
|
||||
admin_id = c.lastrowid
|
||||
c.execute('UPDATE instances SET user_id = ? WHERE user_id IS NULL', (admin_id,))
|
||||
elif count == 1:
|
||||
# If there's only one user and it's 'admin', ensure its password matches WEB_PASSWORD
|
||||
c.execute('SELECT id, username FROM users LIMIT 1')
|
||||
user = c.fetchone()
|
||||
if user[1] == 'admin':
|
||||
print("Ensuring admin password matches WEB_PASSWORD...")
|
||||
admin_hash = get_password_hash(WEB_PASSWORD)
|
||||
c.execute('UPDATE users SET hashed_password = ? WHERE id = ?', (admin_hash, user[0]))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue