Fix user delete flow 4
This commit is contained in:
@@ -38,6 +38,11 @@ async def get_tenant_members(request: Request, tenant_id: str = Path(...)):
|
|||||||
"""Get tenant members"""
|
"""Get tenant members"""
|
||||||
return await _proxy_to_tenant_service(request, f"/api/v1/tenants/{tenant_id}/members")
|
return await _proxy_to_tenant_service(request, f"/api/v1/tenants/{tenant_id}/members")
|
||||||
|
|
||||||
|
@router.get("/user/{user_id}")
|
||||||
|
async def get_user_tenants(request: Request, user_id: str = Path(...)):
|
||||||
|
"""Get all tenant memberships for a user (admin only)"""
|
||||||
|
return await _proxy_to_tenant_service(request, f"/api/v1/tenants/user/{user_id}")
|
||||||
|
|
||||||
# ================================================================
|
# ================================================================
|
||||||
# TENANT-SCOPED DATA SERVICE ENDPOINTS
|
# TENANT-SCOPED DATA SERVICE ENDPOINTS
|
||||||
# ================================================================
|
# ================================================================
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class AuthTenantServiceClient(BaseServiceClient):
|
|||||||
async def get_user_tenants(self, user_id: str) -> Optional[List[Dict[str, Any]]]:
|
async def get_user_tenants(self, user_id: str) -> Optional[List[Dict[str, Any]]]:
|
||||||
"""Get all tenant memberships for a user"""
|
"""Get all tenant memberships for a user"""
|
||||||
try:
|
try:
|
||||||
result = await self.get(f"tenants/memberships/{user_id}")
|
result = await self.get(f"tenants/user/{user_id}")
|
||||||
return result.get("memberships", []) if result else []
|
return result.get("memberships", []) if result else []
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Failed to get user tenants", user_id=user_id, error=str(e))
|
logger.error("Failed to get user tenants", user_id=user_id, error=str(e))
|
||||||
|
|||||||
@@ -73,31 +73,6 @@ async def verify_tenant_access(
|
|||||||
detail="Access verification failed"
|
detail="Access verification failed"
|
||||||
)
|
)
|
||||||
|
|
||||||
@router.get("/tenants/users/{user_id}", response_model=List[TenantResponse])
|
|
||||||
async def get_user_tenants(
|
|
||||||
user_id: str,
|
|
||||||
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
|
||||||
db: AsyncSession = Depends(get_db)
|
|
||||||
):
|
|
||||||
|
|
||||||
# Users can only see their own tenants
|
|
||||||
if current_user["user_id"] != user_id:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
|
||||||
detail="Access denied"
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
tenants = await TenantService.get_user_tenants(user_id, db)
|
|
||||||
return tenants
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to get user tenants: {e}")
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
||||||
detail="Failed to retrieve tenants"
|
|
||||||
)
|
|
||||||
|
|
||||||
@router.get("/tenants/{tenant_id}", response_model=TenantResponse)
|
@router.get("/tenants/{tenant_id}", response_model=TenantResponse)
|
||||||
async def get_tenant(
|
async def get_tenant(
|
||||||
tenant_id: UUID = Path(..., description="Tenant ID"),
|
tenant_id: UUID = Path(..., description="Tenant ID"),
|
||||||
@@ -313,7 +288,7 @@ async def delete_tenant_complete(
|
|||||||
detail=f"Failed to delete tenant: {str(e)}"
|
detail=f"Failed to delete tenant: {str(e)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
@router.get("/tenants/memberships/{user_id}")
|
@router.get("/user/{user_id}")
|
||||||
async def get_user_tenants(
|
async def get_user_tenants(
|
||||||
user_id: str,
|
user_id: str,
|
||||||
current_user = Depends(get_current_user_dep),
|
current_user = Depends(get_current_user_dep),
|
||||||
|
|||||||
@@ -129,32 +129,6 @@ class TenantService:
|
|||||||
permissions=[]
|
permissions=[]
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
async def get_user_tenants(user_id: str, db: AsyncSession) -> List[TenantResponse]:
|
|
||||||
"""Get all tenants accessible by user"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Get user's tenant memberships
|
|
||||||
result = await db.execute(
|
|
||||||
select(Tenant)
|
|
||||||
.join(TenantMember, Tenant.id == TenantMember.tenant_id)
|
|
||||||
.where(
|
|
||||||
and_(
|
|
||||||
TenantMember.user_id == user_id,
|
|
||||||
TenantMember.is_active == True,
|
|
||||||
Tenant.is_active == True
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.order_by(Tenant.name)
|
|
||||||
)
|
|
||||||
|
|
||||||
tenants = result.scalars().all()
|
|
||||||
return [TenantResponse.from_orm(tenant) for tenant in tenants]
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Error getting user tenants: {e}")
|
|
||||||
return []
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def get_tenant_by_id(tenant_id: str, db: AsyncSession) -> Optional[TenantResponse]:
|
async def get_tenant_by_id(tenant_id: str, db: AsyncSession) -> Optional[TenantResponse]:
|
||||||
"""Get tenant by ID"""
|
"""Get tenant by ID"""
|
||||||
|
|||||||
Reference in New Issue
Block a user