Updated login
This commit is contained in:
60
py_app/app/db_create_scripts/create_external_superadmin.py
Normal file
60
py_app/app/db_create_scripts/create_external_superadmin.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import mariadb
|
||||
import os
|
||||
|
||||
def get_external_db_connection():
|
||||
"""Get MariaDB connection using external_server.conf"""
|
||||
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']
|
||||
)
|
||||
|
||||
def create_external_users_table():
|
||||
"""Create users table and superadmin user in external MariaDB database"""
|
||||
try:
|
||||
conn = get_external_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create users table if not exists (MariaDB syntax)
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(50) UNIQUE NOT NULL,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(50) NOT NULL
|
||||
)
|
||||
''')
|
||||
|
||||
# Insert superadmin user if not exists
|
||||
cursor.execute('''
|
||||
INSERT IGNORE INTO users (username, password, role)
|
||||
VALUES (%s, %s, %s)
|
||||
''', ('superadmin', 'superadmin123', 'superadmin'))
|
||||
|
||||
# Check if user was created/exists
|
||||
cursor.execute("SELECT username, password, role FROM users WHERE username = %s", ('superadmin',))
|
||||
result = cursor.fetchone()
|
||||
|
||||
if result:
|
||||
print(f"SUCCESS: Superadmin user exists in external database")
|
||||
print(f"Username: {result[0]}, Password: {result[1]}, Role: {result[2]}")
|
||||
else:
|
||||
print("ERROR: Failed to create/find superadmin user")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("External MariaDB users table setup completed.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"ERROR: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_external_users_table()
|
||||
53
py_app/app/db_create_scripts/find_users_databases.py
Normal file
53
py_app/app/db_create_scripts/find_users_databases.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
def check_database(db_path, description):
|
||||
"""Check if a database exists and show its users."""
|
||||
if os.path.exists(db_path):
|
||||
print(f"\n{description}: FOUND at {db_path}")
|
||||
try:
|
||||
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 cursor.fetchone():
|
||||
cursor.execute("SELECT id, username, password, role FROM users")
|
||||
users = cursor.fetchall()
|
||||
if users:
|
||||
print("Users in this database:")
|
||||
for user in users:
|
||||
print(f" ID: {user[0]}, Username: {user[1]}, Password: {user[2]}, Role: {user[3]}")
|
||||
else:
|
||||
print(" Users table exists but is empty")
|
||||
else:
|
||||
print(" No users table found")
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
print(f" Error reading database: {e}")
|
||||
else:
|
||||
print(f"\n{description}: NOT FOUND at {db_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Check different possible locations for users.db
|
||||
|
||||
# 1. Root quality_recticel/instance/users.db
|
||||
root_instance = "/home/ske087/quality_recticel/instance/users.db"
|
||||
check_database(root_instance, "Root instance users.db")
|
||||
|
||||
# 2. App instance folder
|
||||
app_instance = "/home/ske087/quality_recticel/py_app/instance/users.db"
|
||||
check_database(app_instance, "App instance users.db")
|
||||
|
||||
# 3. Current working directory
|
||||
cwd_db = "/home/ske087/quality_recticel/py_app/users.db"
|
||||
check_database(cwd_db, "Working directory users.db")
|
||||
|
||||
# 4. Flask app database (relative to py_app)
|
||||
flask_db = "/home/ske087/quality_recticel/py_app/app/users.db"
|
||||
check_database(flask_db, "Flask app users.db")
|
||||
|
||||
print("\n" + "="*50)
|
||||
print("RECOMMENDATION:")
|
||||
print("The login should use the external MariaDB database.")
|
||||
print("Make sure you have created the superadmin user in MariaDB using create_roles_table.py")
|
||||
Reference in New Issue
Block a user