-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add option to purge users #18064
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
Add option to purge users #18064
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a00fcb2
Add option to purge users
zeripath 49a5232
fix paging and pre-disable the user
zeripath 4655372
fix test
zeripath be5f6a2
Merge remote-tracking branch 'origin/main' into really-delete-user
zeripath 2df52bd
Add force logout
zeripath 2c52a89
Merge branch 'main' into really-delete-user
zeripath 71f2fbb
Merge remote-tracking branch 'origin/main' into really-delete-user
zeripath eaea431
use FormBool
zeripath 68aaddc
Add some comments as to why a transaction is inappropriate
zeripath dae68c8
Delete Packages too
zeripath 7af3e8e
update locale_en-US.ini to include that we delete packages
zeripath fdb5002
Merge remote-tracking branch 'origin/main' into really-delete-user
zeripath a53d2cc
as per review
zeripath d4354b4
Cope with DELETE JOINS
zeripath 6af6907
Merge remote-tracking branch 'origin/main' into really-delete-user
zeripath 037692b
Merge remote-tracking branch 'origin/main' into really-delete-user
zeripath 3ecaeb7
fix test
zeripath 9e5685f
attempt to fix mysql query
zeripath 3cb1a1d
Apply suggestions from code review
zeripath 2b88a7b
Merge remote-tracking branch 'origin/main' into really-delete-user
zeripath f9280a8
Merge branch 'main' into really-delete-user
lafriks 79b7137
Merge branch 'main' into really-delete-user
zeripath 2125491
Merge branch 'main' into really-delete-user
zeripath 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
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
repo_model "code.gitea.io/gitea/models/repo" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/avatar" | ||
"code.gitea.io/gitea/modules/eventsource" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/storage" | ||
"code.gitea.io/gitea/modules/util" | ||
|
@@ -27,19 +28,94 @@ import ( | |
// DeleteUser completely and permanently deletes everything of a user, | ||
// but issues/comments/pulls will be kept and shown as someone has been deleted, | ||
// unless the user is younger than USER_DELETE_WITH_COMMENTS_MAX_DAYS. | ||
func DeleteUser(u *user_model.User) error { | ||
func DeleteUser(ctx context.Context, u *user_model.User, purge bool) error { | ||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if u.IsOrganization() { | ||
return fmt.Errorf("%s is an organization not a user", u.Name) | ||
} | ||
|
||
if purge { | ||
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. Is this code block covered by tests? |
||
// Disable the user first | ||
if err := user_model.UpdateUserCols(ctx, &user_model.User{ | ||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ID: u.ID, | ||
IsActive: false, | ||
IsRestricted: true, | ||
IsAdmin: false, | ||
ProhibitLogin: true, | ||
Passwd: "", | ||
Salt: "", | ||
PasswdHashAlgo: "", | ||
MaxRepoCreation: 0, | ||
}, "is_active", "is_restricted", "is_admin", "prohibit_login", "max_repo_creation", "passwd", "salt", "passwd_hash_algo"); err != nil { | ||
return fmt.Errorf("unable to disable user: %s[%d] prior to purge. UpdateUserCols: %w", u.Name, u.ID, err) | ||
} | ||
|
||
// Force any logged in sessions to log out | ||
// FIXME: We also need to tell the session manager to log them out too. | ||
eventsource.GetManager().SendMessage(u.ID, &eventsource.Event{ | ||
Name: "logout", | ||
}) | ||
|
||
// Delete all repos belonging to this user | ||
for { | ||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{ | ||
ListOptions: db.ListOptions{ | ||
PageSize: models.RepositoryListDefaultPageSize, | ||
Page: 1, | ||
}, | ||
Private: true, | ||
OwnerID: u.ID, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("SearchRepositoryByName: %v", err) | ||
} | ||
if len(repos) == 0 { | ||
break | ||
} | ||
for _, repo := range repos { | ||
if err := models.DeleteRepository(u, u.ID, repo.ID); err != nil { | ||
return fmt.Errorf("unable to delete repositories for %s[%d]. Error: %v", u.Name, u.ID, err) | ||
} | ||
} | ||
} | ||
|
||
// Delete Orgs | ||
for { | ||
orgs, err := models.FindOrgs(models.FindOrgOptions{ | ||
ListOptions: db.ListOptions{ | ||
PageSize: models.RepositoryListDefaultPageSize, | ||
Page: 1, | ||
}, | ||
UserID: u.ID, | ||
IncludePrivate: true, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("unable to find org list for %s[%d]. Error: %v", u.Name, u.ID, err) | ||
} | ||
if len(orgs) == 0 { | ||
break | ||
} | ||
for _, org := range orgs { | ||
if err := models.RemoveOrgUser(org.ID, u.ID); err != nil { | ||
if models.IsErrLastOrgOwner(err) { | ||
err = models.DeleteOrganization(ctx, org) | ||
} | ||
if err != nil { | ||
return fmt.Errorf("unable to remove user %s[%d] from org %s[%d]. Error: %v", u.Name, u.ID, org.Name, org.ID, err) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
ctx, committer, err := db.TxContext() | ||
if err != nil { | ||
return err | ||
} | ||
defer committer.Close() | ||
|
||
// Note: A user owns any repository or belongs to any organization | ||
// cannot perform delete operation. | ||
// cannot perform delete operation. This causes a race with the purge above | ||
// however consistency requires that we ensure that this is the case | ||
|
||
// Check ownership of repository. | ||
count, err := repo_model.GetRepositoryCount(ctx, u.ID) | ||
|
@@ -57,7 +133,7 @@ func DeleteUser(u *user_model.User) error { | |
return models.ErrUserHasOrgs{UID: u.ID} | ||
} | ||
|
||
if err := models.DeleteUser(ctx, u); err != nil { | ||
if err := models.DeleteUser(ctx, u, purge); err != nil { | ||
return fmt.Errorf("DeleteUser: %v", err) | ||
} | ||
|
||
|
@@ -77,15 +153,15 @@ func DeleteUser(u *user_model.User) error { | |
// so just keep error logs of those operations. | ||
path := user_model.UserPath(u.Name) | ||
if err := util.RemoveAll(path); err != nil { | ||
err = fmt.Errorf("Failed to RemoveAll %s: %v", path, err) | ||
err = fmt.Errorf("failed to RemoveAll %s: %v", path, err) | ||
_ = admin_model.CreateNotice(db.DefaultContext, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err)) | ||
return err | ||
} | ||
|
||
if u.Avatar != "" { | ||
avatarPath := u.CustomAvatarRelativePath() | ||
if err := storage.Avatars.Delete(avatarPath); err != nil { | ||
err = fmt.Errorf("Failed to remove %s: %v", avatarPath, err) | ||
err = fmt.Errorf("failed to remove %s: %v", avatarPath, err) | ||
_ = admin_model.CreateNotice(db.DefaultContext, admin_model.NoticeTask, fmt.Sprintf("delete user '%s': %v", u.Name, err)) | ||
return err | ||
} | ||
|
@@ -108,7 +184,7 @@ func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) error { | |
return db.ErrCancelledf("Before delete inactive user %s", u.Name) | ||
default: | ||
} | ||
if err := DeleteUser(u); err != nil { | ||
if err := DeleteUser(ctx, u, false); err != nil { | ||
// Ignore users that were set inactive by admin. | ||
if models.IsErrUserOwnRepos(err) || models.IsErrUserHasOrgs(err) { | ||
continue | ||
|
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
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.