Role management, login logic, and debug improvements. MariaDB login now uses correct syntax.
This commit is contained in:
45
py_app/app/db_create_scripts/create_roles_table.py
Normal file
45
py_app/app/db_create_scripts/create_roles_table.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
def create_roles_and_users_tables(db_path):
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
# Create users table if not exists
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
role TEXT NOT NULL
|
||||
)
|
||||
''')
|
||||
# Insert superadmin user if not exists (default password: 'admin', change after first login)
|
||||
cursor.execute('''
|
||||
INSERT OR IGNORE INTO users (username, password, role)
|
||||
VALUES (?, ?, ?)
|
||||
''', ('superadmin', 'superadmin123', 'superadmin'))
|
||||
# Create roles table if not exists
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
access_level TEXT NOT NULL,
|
||||
description TEXT
|
||||
)
|
||||
''')
|
||||
# Insert superadmin role if not exists
|
||||
cursor.execute('''
|
||||
INSERT OR IGNORE INTO roles (name, access_level, description)
|
||||
VALUES (?, ?, ?)
|
||||
''', ('superadmin', 'full', 'Full access to all app areas and functions'))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Default path to users.db in instance folder
|
||||
instance_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../instance'))
|
||||
if not os.path.exists(instance_folder):
|
||||
os.makedirs(instance_folder)
|
||||
db_path = os.path.join(instance_folder, 'users.db')
|
||||
create_roles_and_users_tables(db_path)
|
||||
print("Roles and users tables created. Superadmin user and role initialized.")
|
||||
@@ -0,0 +1,26 @@
|
||||
import mariadb
|
||||
import os
|
||||
|
||||
def get_external_db_connection():
|
||||
settings_file = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../instance/external_server.conf'))
|
||||
settings = {}
|
||||
with open(settings_file, 'r') as f:
|
||||
for line in f:
|
||||
key, value = line.strip().split('=', 1)
|
||||
settings[key] = value
|
||||
return mariadb.connect(
|
||||
user=settings['username'],
|
||||
password=settings['password'],
|
||||
host=settings['server_domain'],
|
||||
port=int(settings['port']),
|
||||
database=settings['database_name']
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
conn = get_external_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DROP TABLE IF EXISTS users")
|
||||
cursor.execute("DROP TABLE IF EXISTS roles")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Dropped users and roles tables from external database.")
|
||||
30
py_app/app/db_create_scripts/print_internal_users.py
Normal file
30
py_app/app/db_create_scripts/print_internal_users.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
instance_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../instance'))
|
||||
db_path = os.path.join(instance_folder, 'users.db')
|
||||
|
||||
if not os.path.exists(db_path):
|
||||
print("users.db not found at", db_path)
|
||||
exit(1)
|
||||
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Check if users table exists
|
||||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users'")
|
||||
if not cursor.fetchone():
|
||||
print("No users table found in users.db.")
|
||||
conn.close()
|
||||
exit(1)
|
||||
|
||||
# Print all users
|
||||
cursor.execute("SELECT id, username, password, role FROM users")
|
||||
rows = cursor.fetchall()
|
||||
if not rows:
|
||||
print("No users found in users.db.")
|
||||
else:
|
||||
print("Users in users.db:")
|
||||
for row in rows:
|
||||
print(f"id={row[0]}, username={row[1]}, password={row[2]}, role={row[3]}")
|
||||
conn.close()
|
||||
34
py_app/app/db_create_scripts/seed_internal_superadmin.py
Normal file
34
py_app/app/db_create_scripts/seed_internal_superadmin.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import sqlite3
|
||||
import os
|
||||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'your_secret_key' # Use the same key as in __init__.py
|
||||
|
||||
instance_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../instance'))
|
||||
if not os.path.exists(instance_folder):
|
||||
os.makedirs(instance_folder)
|
||||
db_path = os.path.join(instance_folder, 'users.db')
|
||||
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create users table if not exists
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
role TEXT NOT NULL
|
||||
)
|
||||
''')
|
||||
|
||||
# Insert superadmin user if not exists
|
||||
cursor.execute('''
|
||||
INSERT OR IGNORE INTO users (username, password, role)
|
||||
VALUES (?, ?, ?)
|
||||
''', ('superadmin', 'superadmin123', 'superadmin'))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Internal users.db seeded with superadmin user.")
|
||||
Reference in New Issue
Block a user