Initial go

This commit is contained in:
2025-12-11 12:37:11 -05:00
parent 7efc2ef37a
commit 9cfd88d848
4 changed files with 145 additions and 1 deletions

View File

@@ -7,7 +7,7 @@ from app.api.deps import get_db, get_current_admin_user, get_current_read_user
from app.core.config import settings
from app.core.security import create_access_token
from app.crud.crud_user import user as crud_user
from app.schemas.ppr import Token, UserCreate, UserUpdate, User
from app.schemas.ppr import Token, UserCreate, UserUpdate, User, ChangePassword
router = APIRouter()
@@ -90,4 +90,22 @@ async def update_user(
detail="User not found"
)
user = crud_user.update(db, db_obj=user, obj_in=user_in)
return user
@router.post("/users/{user_id}/change-password", response_model=User)
async def change_user_password(
user_id: int,
password_data: ChangePassword,
db: Session = Depends(get_db),
current_user = Depends(get_current_admin_user)
):
"""Change a user's password (admin only)"""
user = crud_user.get(db, user_id=user_id)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
user = crud_user.change_password(db, db_obj=user, new_password=password_data.password)
return user

View File

@@ -50,5 +50,14 @@ class CRUDUser:
# For future use if we add user status
return True
def change_password(self, db: Session, db_obj: User, new_password: str) -> User:
"""Change a user's password (typically used by admins to reset another user's password)"""
hashed_password = get_password_hash(new_password)
db_obj.password = hashed_password
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
user = CRUDUser()

View File

@@ -135,6 +135,11 @@ class UserUpdate(BaseModel):
role: Optional[UserRole] = None
class ChangePassword(BaseModel):
"""Schema for admin-initiated password changes"""
password: str
class UserInDBBase(UserBase):
id: int