76 lines
2.4 KiB
Python
76 lines
2.4 KiB
Python
import os
|
|
import sys
|
|
import asyncio
|
|
import logging
|
|
from bson import ObjectId
|
|
|
|
# Add the project root to the Python path
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
# Import repositories
|
|
from src.db.repositories.team_repository import team_repository
|
|
from src.db.repositories.user_repository import user_repository
|
|
from src.db.repositories.api_key_repository import api_key_repository
|
|
|
|
# Import models
|
|
from src.db.models.team import TeamModel
|
|
from src.db.models.user import UserModel
|
|
from src.db.models.api_key import ApiKeyModel
|
|
|
|
# Import security functions
|
|
from src.core.security import generate_api_key, calculate_expiry_date
|
|
|
|
async def create_admin():
|
|
# Create a new team
|
|
print("Creating admin team...")
|
|
team = TeamModel(
|
|
name="Admin Team",
|
|
description="Default admin team for system administration"
|
|
)
|
|
|
|
created_team = await team_repository.create(team)
|
|
print(f"Created team with ID: {created_team.id}")
|
|
|
|
# Create admin user
|
|
print("Creating admin user...")
|
|
user = UserModel(
|
|
name="Admin User",
|
|
email="admin@example.com",
|
|
team_id=created_team.id,
|
|
is_admin=True,
|
|
is_active=True
|
|
)
|
|
|
|
created_user = await user_repository.create(user)
|
|
print(f"Created admin user with ID: {created_user.id}")
|
|
|
|
# Generate API key
|
|
print("Generating API key...")
|
|
raw_key, hashed_key = generate_api_key(str(created_team.id), str(created_user.id))
|
|
expiry_date = calculate_expiry_date()
|
|
|
|
# Create API key in database
|
|
api_key = ApiKeyModel(
|
|
key_hash=hashed_key,
|
|
user_id=created_user.id,
|
|
team_id=created_team.id,
|
|
name="Admin API Key",
|
|
description="Initial API key for admin user",
|
|
expiry_date=expiry_date,
|
|
is_active=True
|
|
)
|
|
|
|
created_key = await api_key_repository.create(api_key)
|
|
print(f"Created API key with ID: {created_key.id}")
|
|
print(f"API Key (save this, it won't be shown again): {raw_key}")
|
|
|
|
return {
|
|
"team_id": str(created_team.id),
|
|
"user_id": str(created_user.id),
|
|
"api_key": raw_key
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
print("Creating admin user and API key...")
|
|
result = asyncio.run(create_admin())
|
|
print("\nSetup complete! Use the API key to authenticate API calls.") |