118 lines
4.0 KiB
Python
118 lines
4.0 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Script to create test users with different roles for demonstrating RBAC
|
|
Run: python create_test_users.py
|
|
"""
|
|
import pymysql
|
|
import hashlib
|
|
from app.config import Config
|
|
|
|
|
|
def hash_password(password):
|
|
"""Hash password using SHA256"""
|
|
return hashlib.sha256(password.encode()).hexdigest()
|
|
|
|
|
|
def create_test_users():
|
|
"""Create test users with different roles"""
|
|
try:
|
|
conn = pymysql.connect(
|
|
user=Config.DB_USER,
|
|
password=Config.DB_PASSWORD,
|
|
host=Config.DB_HOST,
|
|
port=Config.DB_PORT,
|
|
database=Config.DB_NAME
|
|
)
|
|
cursor = conn.cursor()
|
|
|
|
test_users = [
|
|
{
|
|
'username': 'manager1',
|
|
'email': 'manager1@quality-app.local',
|
|
'full_name': 'Manager One',
|
|
'role': 'manager',
|
|
'password': 'manager123',
|
|
'modules': ['quality', 'settings']
|
|
},
|
|
{
|
|
'username': 'manager2',
|
|
'email': 'manager2@quality-app.local',
|
|
'full_name': 'Manager Two',
|
|
'role': 'manager',
|
|
'password': 'manager123',
|
|
'modules': ['quality']
|
|
},
|
|
{
|
|
'username': 'worker1',
|
|
'email': 'worker1@quality-app.local',
|
|
'full_name': 'Worker One',
|
|
'role': 'worker',
|
|
'password': 'worker123',
|
|
'modules': ['quality']
|
|
},
|
|
{
|
|
'username': 'worker2',
|
|
'email': 'worker2@quality-app.local',
|
|
'full_name': 'Worker Two',
|
|
'role': 'worker',
|
|
'password': 'worker123',
|
|
'modules': ['quality']
|
|
},
|
|
]
|
|
|
|
for user in test_users:
|
|
# Check if user exists
|
|
cursor.execute("SELECT id FROM users WHERE username = %s", (user['username'],))
|
|
if cursor.fetchone():
|
|
print(f"User '{user['username']}' already exists, skipping...")
|
|
continue
|
|
|
|
# Create user
|
|
cursor.execute("""
|
|
INSERT INTO users (username, email, full_name, role, is_active)
|
|
VALUES (%s, %s, %s, %s, 1)
|
|
""", (user['username'], user['email'], user['full_name'], user['role']))
|
|
|
|
# Get user ID
|
|
cursor.execute("SELECT id FROM users WHERE username = %s", (user['username'],))
|
|
user_id = cursor.fetchone()[0]
|
|
|
|
# Set password
|
|
password_hash = hash_password(user['password'])
|
|
cursor.execute("""
|
|
INSERT INTO user_credentials (user_id, password_hash)
|
|
VALUES (%s, %s)
|
|
""", (user_id, password_hash))
|
|
|
|
# Grant modules
|
|
for module in user['modules']:
|
|
cursor.execute("""
|
|
INSERT IGNORE INTO user_modules (user_id, module_name)
|
|
VALUES (%s, %s)
|
|
""", (user_id, module))
|
|
|
|
print(f"✓ Created user: {user['username']} (role: {user['role']}, password: {user['password']})")
|
|
|
|
conn.commit()
|
|
cursor.close()
|
|
conn.close()
|
|
|
|
print("\n" + "="*60)
|
|
print("Test users created successfully!")
|
|
print("="*60)
|
|
print("\nTest Accounts:")
|
|
print(" admin / admin123 (Admin - Full access)")
|
|
print(" manager1 / manager123 (Manager - All modules)")
|
|
print(" manager2 / manager123 (Manager - Quality only)")
|
|
print(" worker1 / worker123 (Worker - Quality only)")
|
|
print(" worker2 / worker123 (Worker - Quality only)")
|
|
print("\nYou can now test role-based access control by logging in with these accounts.")
|
|
|
|
except Exception as e:
|
|
print(f"Error creating test users: {e}")
|
|
raise
|
|
|
|
|
|
if __name__ == '__main__':
|
|
create_test_users()
|