-
-
Notifications
You must be signed in to change notification settings - Fork 50
Feature/delete account endpoint #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Jayant-kernel
wants to merge
20
commits into
openml:main
from
Jayant-kernel:feature/delete-account-endpoint
Closed
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1a5dea6
fix(flows): replace GET /flows/exists with POST to support URI-unsafe…
2f60ac4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3ff845f
style: fix trailing comma in flow_exists db call
a771b69
refactor(flows): move FlowExistsBody to schemas and keep GET as depre…
9662c1f
feat: add DELETE /users/{user_id} endpoint (Phase 1, fixes #194)
Jayant-kernel be4980e
fix(review): address PR feedback on account deletion
Jayant-kernel 6449a2e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ec59247
fix(types): specify generic type parameters for dict in users router
Jayant-kernel e8fd89f
style: remove inline comments to adhere to contribution guidelines
Jayant-kernel aa4ed9c
fix(review): implement session locking and add missing regression tests
Jayant-kernel 11e3c99
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d175de1
style: fix ruff E501 and PLR0913 in users tests
Jayant-kernel 171c9b3
style: fix ruff ARG001 unused argument lint error
Jayant-kernel f0999e5
fix(review): address CodeRabbit actionable comments
Jayant-kernel 932a2dc
Fix user delete lock restore race and tighten resource-block tests
0978b8d
fix(users): commit deletion lock before resource check
1a60e2e
Merge upstream/main into feature/delete-account-endpoint
Jayant-kernel 3345fe1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7888523
Fix docstring imperative mood for deprecated flows endpoint
Jayant-kernel 0ecb8b1
Add docstrings for users router
Jayant-kernel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| from http import HTTPStatus | ||
| from typing import Annotated, Any | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException | ||
| from sqlalchemy import Connection | ||
|
|
||
| from core.errors import UserError | ||
| from database.users import User, UserGroup, delete_user, get_user_resource_count | ||
| from routers.dependencies import expdb_connection, fetch_user, userdb_connection | ||
|
|
||
| router = APIRouter(prefix="/users", tags=["users"]) | ||
|
|
||
|
|
||
| @router.delete( | ||
| "/{user_id}", | ||
| summary="Delete a user account", | ||
| description=( | ||
| "Deletes the account of the specified user. " | ||
| "Only the account owner or an admin may perform this action. " | ||
| "Deletion is blocked if the user has uploaded any datasets, flows, or runs." | ||
| ), | ||
| ) | ||
| def delete_account( | ||
| user_id: int, | ||
| caller: Annotated[User | None, Depends(fetch_user)] = None, | ||
| user_db: Annotated[Connection, Depends(userdb_connection)] = None, | ||
| expdb: Annotated[Connection, Depends(expdb_connection)] = None, | ||
| ) -> dict[str, Any]: | ||
| if caller is None: | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.UNAUTHORIZED, | ||
| detail={"code": str(int(UserError.NO_ACCESS)), "message": "Authentication required"}, | ||
| ) | ||
|
|
||
| is_admin = UserGroup.ADMIN in caller.groups | ||
| is_self = caller.user_id == user_id | ||
|
|
||
| if not is_admin and not is_self: | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.FORBIDDEN, | ||
| detail={"code": str(int(UserError.NO_ACCESS)), "message": "No access granted"}, | ||
| ) | ||
|
|
||
| from sqlalchemy import text # noqa: PLC0415 | ||
|
|
||
| original = user_db.execute( | ||
| text("SELECT session_hash FROM users WHERE id = :id FOR UPDATE"), | ||
| parameters={"id": user_id}, | ||
| ).fetchone() | ||
|
|
||
| if original is None: | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.NOT_FOUND, | ||
| detail={"code": str(int(UserError.NOT_FOUND)), "message": "User not found"}, | ||
| ) | ||
|
|
||
| # Invalidate session immediately to prevent concurrent resource creation | ||
| # This serves as a 'deletion_pending' lock as suggested in code review | ||
| original_session_hash = original[0] | ||
| user_db.execute( | ||
| text("UPDATE users SET session_hash = 'DELETION_PENDING' WHERE id = :id"), | ||
| parameters={"id": user_id}, | ||
| ) | ||
| user_db.commit() | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| resource_count = get_user_resource_count(user_id=user_id, expdb=expdb) | ||
| if resource_count > 0: | ||
| # Restore session hash if deletion is blocked | ||
| user_db.execute( | ||
| text("UPDATE users SET session_hash = :hash WHERE id = :id"), | ||
| parameters={"hash": original_session_hash, "id": user_id}, | ||
| ) | ||
| user_db.commit() | ||
| raise HTTPException( | ||
| status_code=HTTPStatus.CONFLICT, | ||
| detail={ | ||
| "code": str(int(UserError.HAS_RESOURCES)), | ||
| "message": ( | ||
| f"User has {resource_count} resource(s). " | ||
| "Remove or transfer resources before deleting the account." | ||
| ), | ||
| }, | ||
| ) | ||
|
|
||
| delete_user(user_id=user_id, connection=user_db) | ||
| user_db.commit() | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| return {"user_id": user_id, "deleted": True} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.