Create Global Admins team when admin user is created
- Admin user is now automatically added to Global Admins team as owner - Ensures every user belongs to at least one team - Updated unit tests to handle multiple db.add() calls
This commit is contained in:
@@ -11,7 +11,7 @@ from typing import Optional
|
||||
from passlib.context import CryptContext
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .models import User, Session as UserSession, APIKey
|
||||
from .models import User, Session as UserSession, APIKey, Team, TeamMembership
|
||||
from .config import get_settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -363,6 +363,8 @@ def create_default_admin(db: Session) -> Optional[User]:
|
||||
|
||||
The admin password can be set via ORCHARD_ADMIN_PASSWORD environment variable.
|
||||
If not set, defaults to 'changeme123' and requires password change on first login.
|
||||
|
||||
Also creates the "Global Admins" team and adds the admin user to it.
|
||||
"""
|
||||
# Check if any users exist
|
||||
user_count = db.query(User).count()
|
||||
@@ -385,6 +387,27 @@ def create_default_admin(db: Session) -> Optional[User]:
|
||||
must_change_password=must_change,
|
||||
)
|
||||
|
||||
# Create Global Admins team and add admin to it
|
||||
global_admins_team = Team(
|
||||
name="Global Admins",
|
||||
slug="global-admins",
|
||||
description="System administrators with full access",
|
||||
created_by="admin",
|
||||
)
|
||||
db.add(global_admins_team)
|
||||
db.flush()
|
||||
|
||||
membership = TeamMembership(
|
||||
team_id=global_admins_team.id,
|
||||
user_id=admin.id,
|
||||
role="owner",
|
||||
invited_by="admin",
|
||||
)
|
||||
db.add(membership)
|
||||
db.commit()
|
||||
|
||||
logger.info("Created Global Admins team and added admin as owner")
|
||||
|
||||
if settings.admin_password:
|
||||
logger.info("Created default admin user with configured password")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user