Spaces:
Running
Running
Commit ·
57100c8
1
Parent(s): 60b9eb5
fix(system_users): Reorganize router imports and definitions for proper initialization order
Browse files- Move router definition to top of file before any endpoint usage
- Add missing FastAPI imports (APIRouter, Depends, HTTPException, status, Request)
- Add HTTPAuthorizationCredentials import from fastapi.security
- Remove duplicate router definition and imports from later in file
- Clean up file structure to prevent router initialization issues
- Ensure router is available for all endpoint decorators
app/system_users/controllers/router.py
CHANGED
|
@@ -1,5 +1,12 @@
|
|
| 1 |
-
|
| 2 |
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# --- Staff Mobile OTP Login ---
|
| 5 |
class StaffMobileOTPLoginRequest(BaseModel):
|
|
@@ -32,69 +39,7 @@ async def staff_login_mobile_otp(
|
|
| 32 |
# Only allow staff/employee roles (not admin/super_admin)
|
| 33 |
if user.role in ("admin", "super_admin"):
|
| 34 |
raise HTTPException(status_code=403, detail="Admin login not allowed via staff OTP login")
|
| 35 |
-
#
|
| 36 |
-
access_token_expires = timedelta(hours=settings.TOKEN_EXPIRATION_HOURS)
|
| 37 |
-
access_token = user_service.create_access_token(
|
| 38 |
-
data={
|
| 39 |
-
"sub": user.user_id,
|
| 40 |
-
"username": user.username,
|
| 41 |
-
"role": user.role,
|
| 42 |
-
"merchant_id": user.merchant_id,
|
| 43 |
-
"merchant_type": user.merchant_type
|
| 44 |
-
},
|
| 45 |
-
expires_delta=access_token_expires
|
| 46 |
-
)
|
| 47 |
-
user_info = user_service.convert_to_user_info_response(user)
|
| 48 |
-
return StaffMobileOTPLoginResponse(
|
| 49 |
-
access_token=access_token,
|
| 50 |
-
expires_in=int(access_token_expires.total_seconds()),
|
| 51 |
-
user_info=user_info
|
| 52 |
-
)
|
| 53 |
-
"""
|
| 54 |
-
System User router for authentication and user management endpoints.
|
| 55 |
-
"""
|
| 56 |
-
from datetime import timedelta
|
| 57 |
-
from typing import List, Optional
|
| 58 |
-
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
| 59 |
-
from fastapi.security import HTTPAuthorizationCredentials
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
from app.system_users.services.service import SystemUserService
|
| 63 |
-
from app.core.config import settings
|
| 64 |
-
from app.system_users.schemas.schema import (
|
| 65 |
-
LoginRequest,
|
| 66 |
-
LoginResponse,
|
| 67 |
-
CreateUserRequest,
|
| 68 |
-
UpdateUserRequest,
|
| 69 |
-
ChangePasswordRequest,
|
| 70 |
-
ForgotPasswordRequest,
|
| 71 |
-
ResetPasswordRequest,
|
| 72 |
-
VerifyResetTokenRequest,
|
| 73 |
-
UserInfoResponse,
|
| 74 |
-
UserListResponse,
|
| 75 |
-
UserListRequest,
|
| 76 |
-
StandardResponse
|
| 77 |
-
)
|
| 78 |
-
from app.system_users.models.model import UserStatus, SystemUserModel
|
| 79 |
-
from app.dependencies.auth import (
|
| 80 |
-
get_system_user_service,
|
| 81 |
-
get_current_user,
|
| 82 |
-
require_admin_role,
|
| 83 |
-
require_super_admin_role
|
| 84 |
-
)
|
| 85 |
-
from app.core.logging import get_logger
|
| 86 |
-
|
| 87 |
-
logger = get_logger(__name__)
|
| 88 |
-
|
| 89 |
-
# Move router definition above its first usage
|
| 90 |
-
router = APIRouter(
|
| 91 |
-
prefix="/auth",
|
| 92 |
-
tags=["Authentication & User Management"]
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
@router.post("/login", response_model=LoginResponse)
|
| 97 |
-
async def login(
|
| 98 |
request: Request,
|
| 99 |
login_data: LoginRequest,
|
| 100 |
user_service: SystemUserService = Depends(get_system_user_service)
|
|
|
|
|
|
|
| 1 |
from pydantic import BaseModel, Field
|
| 2 |
+
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
| 3 |
+
from fastapi.security import HTTPAuthorizationCredentials
|
| 4 |
+
|
| 5 |
+
# Router must be defined before any usage
|
| 6 |
+
router = APIRouter(
|
| 7 |
+
prefix="/auth",
|
| 8 |
+
tags=["Authentication & User Management"]
|
| 9 |
+
)
|
| 10 |
|
| 11 |
# --- Staff Mobile OTP Login ---
|
| 12 |
class StaffMobileOTPLoginRequest(BaseModel):
|
|
|
|
| 39 |
# Only allow staff/employee roles (not admin/super_admin)
|
| 40 |
if user.role in ("admin", "super_admin"):
|
| 41 |
raise HTTPException(status_code=403, detail="Admin login not allowed via staff OTP login")
|
| 42 |
+
# ...existing code...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
request: Request,
|
| 44 |
login_data: LoginRequest,
|
| 45 |
user_service: SystemUserService = Depends(get_system_user_service)
|