update for pc server
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from fastapi import FastAPI, Depends, HTTPException, status, WebSocket, WebSocketDisconnect, BackgroundTasks
|
||||
from pydantic import BaseModel
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
@@ -33,17 +34,20 @@ def login_for_access_token(form_data: schemas.UserCreate, db: Session = Depends(
|
||||
)
|
||||
return {"access_token": access_token, "token_type": "bearer"}
|
||||
|
||||
class PasswordResetRequest(BaseModel):
|
||||
username: str
|
||||
new_password: str
|
||||
|
||||
@app.post("/auth/reset-password")
|
||||
def reset_password(
|
||||
username: str,
|
||||
new_password: str,
|
||||
request: PasswordResetRequest,
|
||||
current_user: models.User = Depends(auth.get_current_admin_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
user = crud.get_user_by_username(db, username)
|
||||
user = crud.get_user_by_username(db, request.username)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
user.hashed_password = auth.get_password_hash(new_password)
|
||||
user.hashed_password = auth.get_password_hash(request.new_password)
|
||||
db.commit()
|
||||
return {"message": "Password reset successfully"}
|
||||
|
||||
|
||||
34
asf-cloud-server/testarena/backend/scripts/reset_admin.py
Normal file
34
asf-cloud-server/testarena/backend/scripts/reset_admin.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the parent directory to sys.path to import app modules
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from app import database, crud, schemas, auth, models
|
||||
|
||||
def reset_admin():
|
||||
db = database.SessionLocal()
|
||||
try:
|
||||
username = "admin"
|
||||
password = "admin123"
|
||||
|
||||
user = crud.get_user_by_username(db, username)
|
||||
if user:
|
||||
print(f"Updating password for {username}...")
|
||||
user.hashed_password = auth.get_password_hash(password)
|
||||
db.commit()
|
||||
print("Password updated successfully.")
|
||||
else:
|
||||
print(f"Creating user {username}...")
|
||||
user_in = schemas.UserCreate(username=username, password=password, role=models.UserRole.admin)
|
||||
hashed_password = auth.get_password_hash(password)
|
||||
crud.create_user(db, user_in, hashed_password)
|
||||
print("User created successfully.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
reset_admin()
|
||||
Reference in New Issue
Block a user