-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat(api): delete the authenticated user #20410
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
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1d950cf
feat(api): delete the authenticated user
dhruvmanila 59304aa
test: add test cases for delete user API
dhruvmanila 418b2bc
fix: lint errors
dhruvmanila 0c51dc9
docs(api): add query parameter info
dhruvmanila 42eb77c
test: API purge user testing
dhruvmanila 8dabe12
refactor: use auth middleware for delete user api
dhruvmanila 24ecbd0
refactor: fix test for the latest changes
dhruvmanila 9c14114
chore: remove unnecessary test file
dhruvmanila fe4e845
Merge branch 'main' into feat/delete-user-api
6543 fce394f
just use Combo
6543 d027fa5
Merge branch 'main' into feat/delete-user-api
dhruvmanila 196b96a
Merge branch 'main' into feat/delete-user-api
dhruvmanila aa612ce
Merge branch 'main' into feat/delete-user-api
lunny 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,17 @@ | |
package user | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/models" | ||
activities_model "code.gitea.io/gitea/models/activities" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/convert" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/routers/api/v1/utils" | ||
user_service "code.gitea.io/gitea/services/user" | ||
) | ||
|
||
// Search search users | ||
|
@@ -146,3 +150,46 @@ func GetUserHeatmapData(ctx *context.APIContext) { | |
} | ||
ctx.JSON(http.StatusOK, heatmap) | ||
} | ||
|
||
// DeleteAuthenticatedUser deletes the current user | ||
func DeleteAuthenticatedUser(ctx *context.APIContext) { | ||
// swagger:operation DELETE /user user userDeleteCurrent | ||
// --- | ||
// summary: Delete the authenticated user | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: purge | ||
// in: query | ||
// description: Purge user, all their repositories, organizations and comments | ||
// type: boolean | ||
// responses: | ||
// "204": | ||
// "$ref": "#/responses/empty" | ||
// "403": | ||
// "$ref": "#/responses/forbidden" | ||
// "422": | ||
// "$ref": "#/responses/validationError" | ||
|
||
// Extract out the username as it's unavailable after deleting the user. | ||
username := ctx.Doer.Name | ||
|
||
if ctx.Doer.IsOrganization() { | ||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("%s is an organization not a user", username)) | ||
return | ||
} | ||
|
||
if err := user_service.DeleteUser(ctx, ctx.Doer, ctx.FormBool("purge")); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should ensure only site administrator could do it. |
||
if models.IsErrUserOwnRepos(err) || | ||
models.IsErrUserHasOrgs(err) || | ||
models.IsErrUserOwnPackages(err) { | ||
ctx.Error(http.StatusUnprocessableEntity, "", err) | ||
} else { | ||
ctx.Error(http.StatusInternalServerError, "DeleteAuthenticatedUser", err) | ||
} | ||
return | ||
} | ||
log.Trace("Account deleted: %s", username) | ||
|
||
ctx.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
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,52 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integration | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/tests" | ||
) | ||
|
||
func TestAPIDeleteUser(t *testing.T) { | ||
defer tests.PrepareTestEnv(t)() | ||
|
||
// 1 -> Admin | ||
// 8 -> Normal user | ||
for _, userID := range []int64{1, 8} { | ||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userID}) | ||
t.Logf("Testing username %s", user.Name) | ||
|
||
req := NewRequest(t, "DELETE", "/api/v1/user") | ||
req = AddBasicAuthHeader(req, user.Name) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
|
||
assertUserDeleted(t, userID) | ||
unittest.CheckConsistencyFor(t, &user_model.User{}) | ||
} | ||
} | ||
|
||
func TestAPIPurgeUser(t *testing.T) { | ||
defer tests.PrepareTestEnv(t)() | ||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 5}) | ||
|
||
// Cannot delete the user as it still has ownership of repositories | ||
req := NewRequest(t, "DELETE", "/api/v1/user") | ||
req = AddBasicAuthHeader(req, user.Name) | ||
MakeRequest(t, req, http.StatusUnprocessableEntity) | ||
|
||
unittest.CheckConsistencyFor(t, &user_model.User{ID: 5}) | ||
|
||
req = NewRequest(t, "DELETE", "/api/v1/user?purge=true") | ||
req = AddBasicAuthHeader(req, user.Name) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
|
||
assertUserDeleted(t, 5) | ||
unittest.CheckConsistencyFor(t, &user_model.User{}, &repo_model.Repository{}) | ||
} |
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.