frontend kínlódás
This commit is contained in:
@@ -5,9 +5,11 @@ from sqlalchemy.exc import SQLAlchemyError
|
||||
from typing import Dict, Any
|
||||
|
||||
from app.api.deps import get_db, get_current_user
|
||||
from app.schemas.user import UserResponse, UserUpdate, ActiveOrganizationUpdate
|
||||
from app.schemas.user import UserResponse, UserUpdate, ActiveOrganizationUpdate, UserWithTokenResponse
|
||||
from app.models.identity import User
|
||||
from app.services.trust_engine import TrustEngine
|
||||
from app.core.security import create_tokens, DEFAULT_RANK_MAP
|
||||
from app.core.config import settings
|
||||
|
||||
router = APIRouter()
|
||||
trust_engine = TrustEngine()
|
||||
@@ -157,7 +159,7 @@ async def update_user_preferences(
|
||||
return UserResponse.model_validate(current_user)
|
||||
|
||||
|
||||
@router.patch("/me/active-organization", response_model=UserResponse)
|
||||
@router.patch("/me/active-organization", response_model=UserWithTokenResponse)
|
||||
async def update_active_organization(
|
||||
update_data: ActiveOrganizationUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
@@ -167,6 +169,7 @@ async def update_active_organization(
|
||||
Update the user's active organization (scope_id).
|
||||
|
||||
Accepts an organization_id (UUID/string) or None to revert to personal mode.
|
||||
Returns a new JWT token with updated scope_id in the payload.
|
||||
"""
|
||||
# Extract organization_id from request
|
||||
org_id = update_data.organization_id
|
||||
@@ -200,5 +203,22 @@ async def update_active_organization(
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
||||
|
||||
# Return updated user data
|
||||
return UserResponse.model_validate(current_user)
|
||||
# Generate new JWT token with updated scope_id
|
||||
role_key = current_user.role.value.upper()
|
||||
token_payload = {
|
||||
"sub": str(current_user.id),
|
||||
"role": role_key,
|
||||
"rank": DEFAULT_RANK_MAP.get(role_key, "user"),
|
||||
"scope_level": "organization" if org_id else "personal",
|
||||
"scope_id": org_id,
|
||||
"person_id": str(current_user.person_id) if current_user.person_id else None,
|
||||
}
|
||||
|
||||
access_token, _ = create_tokens(data=token_payload)
|
||||
|
||||
# Return user data with new token
|
||||
return UserWithTokenResponse(
|
||||
user=UserResponse.model_validate(current_user),
|
||||
access_token=access_token,
|
||||
token_type="bearer"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user