2025-07-17 19:03:11 +02:00
|
|
|
"""
|
|
|
|
|
User management API routes
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
2025-07-21 14:41:33 +02:00
|
|
|
from typing import Dict, Any
|
2025-07-18 14:41:39 +02:00
|
|
|
import structlog
|
2025-07-17 19:03:11 +02:00
|
|
|
|
|
|
|
|
from app.core.database import get_db
|
2025-07-20 08:33:23 +02:00
|
|
|
from app.schemas.auth import UserResponse, PasswordChange
|
2025-07-19 21:16:25 +02:00
|
|
|
from app.schemas.users import UserUpdate
|
2025-07-17 19:03:11 +02:00
|
|
|
from app.services.user_service import UserService
|
|
|
|
|
from app.models.users import User
|
|
|
|
|
|
2025-07-21 14:41:33 +02:00
|
|
|
# Import unified authentication from shared library
|
|
|
|
|
from shared.auth.decorators import (
|
|
|
|
|
get_current_user_dep,
|
|
|
|
|
get_current_tenant_id_dep,
|
|
|
|
|
require_role # For admin-only endpoints
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-18 14:41:39 +02:00
|
|
|
logger = structlog.get_logger()
|
2025-07-17 19:03:11 +02:00
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
@router.get("/me", response_model=UserResponse)
|
|
|
|
|
async def get_current_user_info(
|
2025-07-21 14:41:33 +02:00
|
|
|
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
2025-07-17 19:03:11 +02:00
|
|
|
db: AsyncSession = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""Get current user information"""
|
|
|
|
|
try:
|
|
|
|
|
return UserResponse(
|
|
|
|
|
id=str(current_user.id),
|
|
|
|
|
email=current_user.email,
|
|
|
|
|
full_name=current_user.full_name,
|
|
|
|
|
is_active=current_user.is_active,
|
|
|
|
|
is_verified=current_user.is_verified,
|
|
|
|
|
phone=current_user.phone,
|
|
|
|
|
language=current_user.language,
|
|
|
|
|
timezone=current_user.timezone,
|
|
|
|
|
created_at=current_user.created_at,
|
|
|
|
|
last_login=current_user.last_login
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Get current user error: {e}")
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
detail="Failed to get user information"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@router.put("/me", response_model=UserResponse)
|
|
|
|
|
async def update_current_user(
|
2025-07-19 21:16:25 +02:00
|
|
|
user_update: UserUpdate,
|
2025-07-21 20:43:17 +02:00
|
|
|
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
2025-07-17 19:03:11 +02:00
|
|
|
db: AsyncSession = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""Update current user information"""
|
|
|
|
|
try:
|
|
|
|
|
updated_user = await UserService.update_user(current_user.id, user_update, db)
|
|
|
|
|
return UserResponse(
|
|
|
|
|
id=str(updated_user.id),
|
|
|
|
|
email=updated_user.email,
|
|
|
|
|
full_name=updated_user.full_name,
|
|
|
|
|
is_active=updated_user.is_active,
|
|
|
|
|
is_verified=updated_user.is_verified,
|
|
|
|
|
phone=updated_user.phone,
|
|
|
|
|
language=updated_user.language,
|
|
|
|
|
timezone=updated_user.timezone,
|
|
|
|
|
created_at=updated_user.created_at,
|
|
|
|
|
last_login=updated_user.last_login
|
|
|
|
|
)
|
|
|
|
|
except HTTPException:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Update user error: {e}")
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
detail="Failed to update user"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@router.post("/change-password")
|
|
|
|
|
async def change_password(
|
2025-07-20 08:33:23 +02:00
|
|
|
password_data: PasswordChange,
|
2025-07-21 20:43:17 +02:00
|
|
|
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
2025-07-17 19:03:11 +02:00
|
|
|
db: AsyncSession = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""Change user password"""
|
|
|
|
|
try:
|
|
|
|
|
await UserService.change_password(
|
|
|
|
|
current_user.id,
|
|
|
|
|
password_data.current_password,
|
|
|
|
|
password_data.new_password,
|
|
|
|
|
db
|
|
|
|
|
)
|
|
|
|
|
return {"message": "Password changed successfully"}
|
|
|
|
|
except HTTPException:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Password change error: {e}")
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
detail="Failed to change password"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@router.delete("/me")
|
|
|
|
|
async def delete_current_user(
|
2025-07-21 20:43:17 +02:00
|
|
|
current_user: Dict[str, Any] = Depends(get_current_user_dep),
|
2025-07-17 19:03:11 +02:00
|
|
|
db: AsyncSession = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""Delete current user account"""
|
|
|
|
|
try:
|
|
|
|
|
await UserService.delete_user(current_user.id, db)
|
|
|
|
|
return {"message": "User account deleted successfully"}
|
|
|
|
|
except HTTPException:
|
|
|
|
|
raise
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Delete user error: {e}")
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
detail="Failed to delete user account"
|
|
|
|
|
)
|