Spaces:
Sleeping
Sleeping
Commit
·
aece130
1
Parent(s):
50aa6f9
db update from id to user_id
Browse files
main.py
CHANGED
|
@@ -91,7 +91,7 @@ def is_token_expired(expiration_time: datetime) -> bool:
|
|
| 91 |
# Initialize system user
|
| 92 |
async def init_system_user():
|
| 93 |
system_user_data = {
|
| 94 |
-
"
|
| 95 |
"username": SYSTEM_USER,
|
| 96 |
"password": hash_password(SYSTEM_PASSWORD),
|
| 97 |
"email": None,
|
|
@@ -121,7 +121,7 @@ async def signup(request: SignupRequest):
|
|
| 121 |
)
|
| 122 |
|
| 123 |
user_data = {
|
| 124 |
-
"
|
| 125 |
"username": request.username,
|
| 126 |
"password": hash_password(request.password),
|
| 127 |
"email": request.email,
|
|
@@ -154,7 +154,7 @@ async def login(request: LoginRequest, user_agent: str = Header(...)):
|
|
| 154 |
|
| 155 |
# Prepare session data with the expiration time in ISO 8601 format (UTC)
|
| 156 |
session_data = {
|
| 157 |
-
"user_id": user["
|
| 158 |
"token": token,
|
| 159 |
"expires": expiration_time.isoformat(), # ISO 8601 format ensures the timezone is stored
|
| 160 |
"device": user_agent
|
|
@@ -165,7 +165,7 @@ async def login(request: LoginRequest, user_agent: str = Header(...)):
|
|
| 165 |
|
| 166 |
# Return the login response with relevant user details
|
| 167 |
return LoginResponse(
|
| 168 |
-
user_id=user["
|
| 169 |
username=user["username"],
|
| 170 |
email=user["email"],
|
| 171 |
access_level=user["access_level"],
|
|
@@ -243,7 +243,7 @@ async def search_users(query: str):
|
|
| 243 |
|
| 244 |
@auth_router.get("/get-user-id", response_model=str)
|
| 245 |
async def get_user_id(username: str):
|
| 246 |
-
user_query = supabase.table("users").select("
|
| 247 |
|
| 248 |
if not user_query.data:
|
| 249 |
raise HTTPException(
|
|
@@ -251,14 +251,14 @@ async def get_user_id(username: str):
|
|
| 251 |
detail="Username not found"
|
| 252 |
)
|
| 253 |
|
| 254 |
-
return user_query.data[0]["
|
| 255 |
|
| 256 |
# Admin Routes
|
| 257 |
@admin_router.get("/users", response_model=List[UserResponse])
|
| 258 |
async def get_all_users(user_id: str, token: str, user_agent: str = Header(...)):
|
| 259 |
await validate_token(user_id, token, user_agent)
|
| 260 |
|
| 261 |
-
admin_query = supabase.table("users").select("access_level").eq("
|
| 262 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 263 |
raise HTTPException(
|
| 264 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
|
@@ -279,13 +279,13 @@ async def get_all_users(user_id: str, token: str, user_agent: str = Header(...))
|
|
| 279 |
async def get_user(admin_id: str, token: str, user_id: str, user_agent: str = Header(...)):
|
| 280 |
await validate_token(admin_id, token, user_agent)
|
| 281 |
|
| 282 |
-
admin_query = supabase.table("users").select("access_level").eq("
|
| 283 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 284 |
raise HTTPException(
|
| 285 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
| 286 |
)
|
| 287 |
|
| 288 |
-
user_query = supabase.table("users").select("*").eq("
|
| 289 |
if not user_query.data:
|
| 290 |
raise HTTPException(
|
| 291 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
@@ -303,7 +303,7 @@ async def get_user(admin_id: str, token: str, user_id: str, user_agent: str = He
|
|
| 303 |
async def update_user(admin_id: str, token: str, user_id: str, request: UpdateUserRequest, user_agent: str = Header(...)):
|
| 304 |
await validate_token(admin_id, token, user_agent)
|
| 305 |
|
| 306 |
-
admin_query = supabase.table("users").select("access_level").eq("
|
| 307 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 308 |
raise HTTPException(
|
| 309 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
|
@@ -317,7 +317,7 @@ async def update_user(admin_id: str, token: str, user_id: str, request: UpdateUs
|
|
| 317 |
if request.username:
|
| 318 |
update_data["username"] = request.username
|
| 319 |
|
| 320 |
-
updated_user = supabase.table("users").update(update_data).eq("
|
| 321 |
if not updated_user.data:
|
| 322 |
raise HTTPException(
|
| 323 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
@@ -335,13 +335,13 @@ async def update_user(admin_id: str, token: str, user_id: str, request: UpdateUs
|
|
| 335 |
async def update_access_level(admin_id: str, token: str, user_id: str, request: UpdateAccessLevelRequest, user_agent: str = Header(...)):
|
| 336 |
await validate_token(admin_id, token, user_agent)
|
| 337 |
|
| 338 |
-
admin_query = supabase.table("users").select("access_level").eq("
|
| 339 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 340 |
raise HTTPException(
|
| 341 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
| 342 |
)
|
| 343 |
|
| 344 |
-
user_query = supabase.table("users").select("*").eq("
|
| 345 |
if not user_query.data:
|
| 346 |
raise HTTPException(
|
| 347 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
@@ -356,7 +356,7 @@ async def update_access_level(admin_id: str, token: str, user_id: str, request:
|
|
| 356 |
detail="Cannot downgrade a user or change to the same level",
|
| 357 |
)
|
| 358 |
|
| 359 |
-
updated_user = supabase.table("users").update({"access_level": new_access_level}).eq("
|
| 360 |
user = updated_user.data[0]
|
| 361 |
return UserResponse(
|
| 362 |
username=user["username"],
|
|
@@ -377,7 +377,7 @@ async def update_own_data(user_id: str, request: UpdateUserRequest, token: str =
|
|
| 377 |
if request.username:
|
| 378 |
update_data["username"] = request.username
|
| 379 |
|
| 380 |
-
updated_user = supabase.table("users").update(update_data).eq("
|
| 381 |
if not updated_user.data:
|
| 382 |
raise HTTPException(
|
| 383 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
|
|
| 91 |
# Initialize system user
|
| 92 |
async def init_system_user():
|
| 93 |
system_user_data = {
|
| 94 |
+
"user_id": str(uuid.uuid4()),
|
| 95 |
"username": SYSTEM_USER,
|
| 96 |
"password": hash_password(SYSTEM_PASSWORD),
|
| 97 |
"email": None,
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
user_data = {
|
| 124 |
+
"user_id": str(uuid.uuid4()),
|
| 125 |
"username": request.username,
|
| 126 |
"password": hash_password(request.password),
|
| 127 |
"email": request.email,
|
|
|
|
| 154 |
|
| 155 |
# Prepare session data with the expiration time in ISO 8601 format (UTC)
|
| 156 |
session_data = {
|
| 157 |
+
"user_id": user["user_id"],
|
| 158 |
"token": token,
|
| 159 |
"expires": expiration_time.isoformat(), # ISO 8601 format ensures the timezone is stored
|
| 160 |
"device": user_agent
|
|
|
|
| 165 |
|
| 166 |
# Return the login response with relevant user details
|
| 167 |
return LoginResponse(
|
| 168 |
+
user_id=user["user_id"],
|
| 169 |
username=user["username"],
|
| 170 |
email=user["email"],
|
| 171 |
access_level=user["access_level"],
|
|
|
|
| 243 |
|
| 244 |
@auth_router.get("/get-user-id", response_model=str)
|
| 245 |
async def get_user_id(username: str):
|
| 246 |
+
user_query = supabase.table("users").select("user_id").eq("username", username).execute()
|
| 247 |
|
| 248 |
if not user_query.data:
|
| 249 |
raise HTTPException(
|
|
|
|
| 251 |
detail="Username not found"
|
| 252 |
)
|
| 253 |
|
| 254 |
+
return user_query.data[0]["user_id"]
|
| 255 |
|
| 256 |
# Admin Routes
|
| 257 |
@admin_router.get("/users", response_model=List[UserResponse])
|
| 258 |
async def get_all_users(user_id: str, token: str, user_agent: str = Header(...)):
|
| 259 |
await validate_token(user_id, token, user_agent)
|
| 260 |
|
| 261 |
+
admin_query = supabase.table("users").select("access_level").eq("user_id", user_id).execute()
|
| 262 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 263 |
raise HTTPException(
|
| 264 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
|
|
|
| 279 |
async def get_user(admin_id: str, token: str, user_id: str, user_agent: str = Header(...)):
|
| 280 |
await validate_token(admin_id, token, user_agent)
|
| 281 |
|
| 282 |
+
admin_query = supabase.table("users").select("access_level").eq("user_id", admin_id).execute()
|
| 283 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 284 |
raise HTTPException(
|
| 285 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
| 286 |
)
|
| 287 |
|
| 288 |
+
user_query = supabase.table("users").select("*").eq("user_id", user_id).execute()
|
| 289 |
if not user_query.data:
|
| 290 |
raise HTTPException(
|
| 291 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
|
|
| 303 |
async def update_user(admin_id: str, token: str, user_id: str, request: UpdateUserRequest, user_agent: str = Header(...)):
|
| 304 |
await validate_token(admin_id, token, user_agent)
|
| 305 |
|
| 306 |
+
admin_query = supabase.table("users").select("access_level").eq("user_id", admin_id).execute()
|
| 307 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 308 |
raise HTTPException(
|
| 309 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
|
|
|
| 317 |
if request.username:
|
| 318 |
update_data["username"] = request.username
|
| 319 |
|
| 320 |
+
updated_user = supabase.table("users").update(update_data).eq("user_id", user_id).execute()
|
| 321 |
if not updated_user.data:
|
| 322 |
raise HTTPException(
|
| 323 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
|
|
| 335 |
async def update_access_level(admin_id: str, token: str, user_id: str, request: UpdateAccessLevelRequest, user_agent: str = Header(...)):
|
| 336 |
await validate_token(admin_id, token, user_agent)
|
| 337 |
|
| 338 |
+
admin_query = supabase.table("users").select("access_level").eq("user_id", admin_id).execute()
|
| 339 |
if not admin_query.data or admin_query.data[0]["access_level"] != "hush":
|
| 340 |
raise HTTPException(
|
| 341 |
status_code=status.HTTP_403_FORBIDDEN, detail="Insufficient permissions"
|
| 342 |
)
|
| 343 |
|
| 344 |
+
user_query = supabase.table("users").select("*").eq("user_id", user_id).execute()
|
| 345 |
if not user_query.data:
|
| 346 |
raise HTTPException(
|
| 347 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
|
|
|
| 356 |
detail="Cannot downgrade a user or change to the same level",
|
| 357 |
)
|
| 358 |
|
| 359 |
+
updated_user = supabase.table("users").update({"access_level": new_access_level}).eq("user_id", user_id).execute()
|
| 360 |
user = updated_user.data[0]
|
| 361 |
return UserResponse(
|
| 362 |
username=user["username"],
|
|
|
|
| 377 |
if request.username:
|
| 378 |
update_data["username"] = request.username
|
| 379 |
|
| 380 |
+
updated_user = supabase.table("users").update(update_data).eq("user_id", user_id).execute()
|
| 381 |
if not updated_user.data:
|
| 382 |
raise HTTPException(
|
| 383 |
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|