-
Notifications
You must be signed in to change notification settings - Fork 460
feat(dashboard-api): add internal admin route for deleting a user #2986
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5db7e00
feat(dashboard-api): add admin DELETE /admin/users/{userId} route
devin-ai-integration[bot] c39d9a3
fix: add DeleteUser to fakeProvider in test
devin-ai-integration[bot] 9001703
fix: use errors.New instead of fmt.Errorf for static strings (perfspr…
devin-ai-integration[bot] 5234fc9
fix: two-phase user deletion — resolve identity before DB delete
devin-ai-integration[bot] 751beda
fix: remove unused noopDeleteHandle (lint)
devin-ai-integration[bot] 95b7223
fix: add retry logic for identity deletion + proper error responses
devin-ai-integration[bot] 6c8d4c1
fix: add blank line before return (nlreturn lint)
devin-ai-integration[bot] 84e09a0
fix: return 500 for PrepareDeleteUser server errors + add 409 to Open…
devin-ai-integration[bot] 8e49de6
fix: return 404 when user has no identity mapping, 500 for server errors
devin-ai-integration[bot] 006226a
chore: auto-commit generated changes
github-actions[bot] f6284ab
fix: use detached context for IdP cleanup after DB delete
devin-ai-integration[bot] d272095
merge: resolve conflict with main — regenerate api.gen.go
devin-ai-integration[bot] 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
packages/dashboard-api/internal/handlers/admin_users_delete.go
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,92 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/dashboard-api/internal/api" | ||
| "github.com/e2b-dev/infra/packages/dashboard-api/internal/userprofile" | ||
| "github.com/e2b-dev/infra/packages/db/pkg/dberrors" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/logger" | ||
| ) | ||
|
|
||
| const identityDeleteMaxRetries = 3 | ||
|
|
||
| func (s *APIStore) DeleteAdminUsersUserId(c *gin.Context, userId api.UserId) { | ||
| ctx := c.Request.Context() | ||
|
|
||
| // Resolve the external identity references while user_identities still exists. | ||
| handle, err := s.userProfiles.PrepareDeleteUser(ctx, userId) | ||
| if err != nil { | ||
| if errors.Is(err, userprofile.ErrUserNotFound) { | ||
| s.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("User %s not found or has no identity provider record", userId)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| logger.L().Error(ctx, "failed to prepare user deletion", zap.String("user_id", userId.String()), zap.Error(err)) | ||
| s.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to resolve identity provider record for user") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Delete from public.users (cascades to user_identities via FK). | ||
| // Done before the IdP removal so a DB failure does not orphan the identity. | ||
| if err := s.authDB.Write.DeletePublicUser(ctx, userId); err != nil { | ||
| if dberrors.IsNotFoundError(err) { | ||
| s.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("User %s not found", userId)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if dberrors.IsForeignKeyViolation(err) { | ||
| logger.L().Warn(ctx, "cannot delete user due to existing references", zap.String("user_id", userId.String()), zap.Error(err)) | ||
| s.sendAPIStoreError(c, http.StatusConflict, "Cannot delete user: existing references (e.g. addons) must be removed first") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| logger.L().Error(ctx, "failed to delete public user", zap.String("user_id", userId.String()), zap.Error(err)) | ||
| s.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to delete public user record") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Remove the external identity (e.g. Ory) using pre-fetched references. | ||
| // Retry since the DB rows are already gone and we must not leave the IdP identity active. | ||
| // Use a detached context so a client disconnect does not cancel the cleanup. | ||
| cleanupCtx, cleanupCancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) | ||
| defer cleanupCancel() | ||
|
|
||
| var identityErr error | ||
| for attempt := range identityDeleteMaxRetries { | ||
| identityErr = handle.Execute(cleanupCtx) | ||
| if identityErr == nil { | ||
| break | ||
| } | ||
|
|
||
| logger.L().Warn(ctx, "retrying identity deletion", | ||
| zap.String("user_id", userId.String()), | ||
| zap.Int("attempt", attempt+1), | ||
| zap.Error(identityErr), | ||
| ) | ||
|
|
||
| time.Sleep(time.Duration(attempt+1) * 200 * time.Millisecond) | ||
| } | ||
|
|
||
| if identityErr != nil { | ||
| logger.L().Error(ctx, "failed to delete user identity provider record after retries", zap.String("user_id", userId.String()), zap.Error(identityErr)) | ||
| s.sendAPIStoreError(c, http.StatusInternalServerError, | ||
| "User DB records deleted but identity provider removal failed — the IdP identity may need manual cleanup") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| c.Status(http.StatusNoContent) | ||
| } | ||
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.
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.