Skip to content

Commit

Permalink
Merge pull request #192 from DocShow-AI/delete-button-admin-page
Browse files Browse the repository at this point in the history
add delete button admin page
  • Loading branch information
liberty-rising authored Jan 15, 2024
2 parents d02a51a + 4968371 commit 9548d94
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
16 changes: 16 additions & 0 deletions backend/database/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,19 @@ def update_user_requires_password_update(
self.db_session.commit()
self.db_session.refresh(db_user)
return db_user

def delete_user_by_username(self, username: str) -> User:
"""
Delete a user based on their username.
Args:
username (str): The username of the user.
Returns:
User: The deleted User object if found, else None.
"""
db_user = self.db_session.query(User).filter(User.username == username).first()
if db_user:
self.db_session.delete(db_user)
self.db_session.commit()
return db_user
4 changes: 4 additions & 0 deletions backend/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ class ForgotPasswordRequest(BaseModel):
email: EmailStr


class DeleteUserRequest(BaseModel):
username: str


class ResetPasswordRequest(BaseModel):
token: str = Field(...)
new_password: str = Field(...)
Expand Down
18 changes: 18 additions & 0 deletions backend/routes/user_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from models.user import (
ChangePasswordRequest,
DeleteUserRequest,
ForgotPasswordRequest,
ResetPasswordRequest,
SendVerificationEmailRequest,
Expand Down Expand Up @@ -212,3 +213,20 @@ async def verify_email(request: VerifyEmailRequest):
@user_router.get("/users/is-email-verified/")
async def is_user_verified(current_user: User = Depends(get_current_user)):
return {"email_verified": current_user.email_verified}


@user_router.delete("/users/delete/")
async def delete_user(
request: DeleteUserRequest,
current_admin_user: User = Depends(get_current_admin_user),
):
with DatabaseManager() as session:
user_manager = UserManager(session)

# Delete user
deleted_user = user_manager.delete_user_by_username(request.username)

if not deleted_user:
raise HTTPException(status_code=404, detail="User not found")

return {"message": f"Successfully deleted user {request.username}."}
14 changes: 14 additions & 0 deletions frontend/src/pages/admin/UserManagement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ function UserManagement() {
}
};

const handleDeleteUser = async () => {
try {
await axios.delete(`${API_URL}users/delete/`, {
data: {username: selectedUser.username}
});
// Refetch users after deletion
const response = await axios.get(`${API_URL}users/`);
setUsers(response.data);
} catch (error) {
console.error('Error deleting user', error);
}
};

const fetchRoles = async () => {
try {
const response = await axios.get(`${API_URL}users/roles/`);
Expand Down Expand Up @@ -155,6 +168,7 @@ function UserManagement() {
</Grid>
<Grid item xs={12} sm={4}>
<Button variant="contained" onClick={handleUpdateUser} disabled={!selectedUser.username}>Update</Button>
<Button variant="contained" onClick={handleDeleteUser} disabled={!selectedUser.username} style={{marginLeft:'10px'}}>Delete</Button>
</Grid>
</Grid>
</CardContent>
Expand Down

0 comments on commit 9548d94

Please sign in to comment.