apac-ops-bot/backend/scripts/create_test_users.py
SamoilenkoVadym b284cadb86 Add test user authentication and RBAC admin panel
Implemented simple authentication for testing and admin panel for user management:

Backend:
- Add simple email/password login for test users (admin@test.local, user@test.local)
- Implement RBAC (Role-Based Access Control) with Permission enum
- Create admin endpoints for user management and system analytics
- Add bcrypt password hashing for test users
- Create script to generate test users in database

Frontend:
- Add SimpleLogin component for test authentication
- Create AdminPanel with user management and system analytics
- Add role-based navigation (Admin tab visible only for admins)
- Update AuthContext to support both MSAL and simple login
- Add API methods for admin operations

Features:
- Admins can view all users, manage roles, activate/deactivate accounts
- Admins can view system-wide analytics (users, conversations, tokens, costs)
- Regular users only see their own chats and usage
- Role badges in UI show user role (user/admin/superadmin)

Note: Simple authentication is for testing only. Production uses Azure AD MSAL.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-27 20:05:54 +00:00

107 lines
3.2 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Script to create test users in the database
Run: python -m scripts.create_test_users
"""
import asyncio
import sys
import uuid
from pathlib import Path
# Add backend directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
import bcrypt
from app.config import settings
from app.models.user import User
def hash_password(password: str) -> str:
"""Hash password using bcrypt"""
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
async def create_test_users():
"""Create test users in database"""
# Create async engine
engine = create_async_engine(settings.DATABASE_URL, echo=True)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as session:
# Check if test users already exist
from sqlalchemy import select
# Admin user
result = await session.execute(
select(User).where(User.email == "admin@test.local")
)
admin = result.scalar_one_or_none()
if not admin:
admin = User(
id=uuid.uuid4(),
azure_ad_id="test_admin", # Special flag for test users
email="admin@test.local",
display_name="Test Admin",
given_name="Admin",
surname="User",
role="admin",
is_active=True,
preferences={},
meta_data={
"is_test_user": True,
"password_hash": hash_password("admin")
}
)
session.add(admin)
print(f"✅ Created admin user: admin@test.local / admin")
else:
print(f" Admin user already exists")
# Regular user
result = await session.execute(
select(User).where(User.email == "user@test.local")
)
user = result.scalar_one_or_none()
if not user:
user = User(
id=uuid.uuid4(),
azure_ad_id="test_user", # Special flag for test users
email="user@test.local",
display_name="Test User",
given_name="Test",
surname="User",
role="user",
is_active=True,
preferences={},
meta_data={
"is_test_user": True,
"password_hash": hash_password("user")
}
)
session.add(user)
print(f"✅ Created regular user: user@test.local / user")
else:
print(f" Regular user already exists")
await session.commit()
await engine.dispose()
print("\n" + "="*60)
print("Test users created successfully!")
print("="*60)
print("\n📝 Login credentials:")
print(" Admin: admin@test.local / admin")
print(" User: user@test.local / user")
print("\n🔗 Use these at: http://localhost:3000")
print("="*60)
if __name__ == "__main__":
asyncio.run(create_test_users())