Permisos en progreso
This commit is contained in:
@@ -28,6 +28,9 @@ async def create_tenant(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user = Depends(deps.get_current_active_superuser)
|
||||
):
|
||||
from app.models.permission import TenantPermission, DEFAULT_PERMISSIONS
|
||||
from datetime import datetime
|
||||
|
||||
# Check existing slug
|
||||
query = select(Tenant).where(Tenant.slug == tenant.slug)
|
||||
result = await db.execute(query)
|
||||
@@ -38,6 +41,23 @@ async def create_tenant(
|
||||
data['slug'] = data['slug'].lower().strip()
|
||||
db_tenant = Tenant(**data)
|
||||
db.add(db_tenant)
|
||||
await db.flush() # genera el id sin hacer commit
|
||||
|
||||
# Inicializar permisos por defecto para el nuevo tenant
|
||||
now = datetime.utcnow()
|
||||
for role, perms in DEFAULT_PERMISSIONS.items():
|
||||
for permission, granted in perms.items():
|
||||
db.add(TenantPermission(
|
||||
id=uuid.uuid4(),
|
||||
tenant_id=db_tenant.id,
|
||||
user_id=None,
|
||||
role=role,
|
||||
permission=permission,
|
||||
granted=granted,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
))
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(db_tenant)
|
||||
return db_tenant
|
||||
|
||||
Reference in New Issue
Block a user