-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Refactor rename user and rename organization #24052
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
Changes from 9 commits
cdd32a1
7f5af9c
07445ed
7aeb9ab
9edd592
f024d8f
3889aee
a4465d8
d9269e3
4f630eb
5f81de3
a53d3eb
7b053b5
7eeb6ca
fb439e2
a72707e
6d9870a
0d08560
c6f01e3
67ec820
5a87f6e
b8e748f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,26 @@ | |
package org | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/models/organization" | ||
org_model "code.gitea.io/gitea/models/organization" | ||
packages_model "code.gitea.io/gitea/models/packages" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/storage" | ||
"code.gitea.io/gitea/modules/util" | ||
"code.gitea.io/gitea/services/agit" | ||
container_service "code.gitea.io/gitea/services/packages/container" | ||
) | ||
|
||
// DeleteOrganization completely and permanently deletes everything of organization. | ||
func DeleteOrganization(org *organization.Organization) error { | ||
func DeleteOrganization(org *org_model.Organization) error { | ||
ctx, commiter, err := db.TxContext(db.DefaultContext) | ||
if err != nil { | ||
return err | ||
|
@@ -39,7 +45,7 @@ func DeleteOrganization(org *organization.Organization) error { | |
return models.ErrUserOwnPackages{UID: org.ID} | ||
} | ||
|
||
if err := organization.DeleteOrganization(ctx, org); err != nil { | ||
if err := org_model.DeleteOrganization(ctx, org); err != nil { | ||
return fmt.Errorf("DeleteOrganization: %w", err) | ||
} | ||
|
||
|
@@ -53,15 +59,94 @@ func DeleteOrganization(org *organization.Organization) error { | |
path := user_model.UserPath(org.Name) | ||
|
||
if err := util.RemoveAll(path); err != nil { | ||
return fmt.Errorf("Failed to RemoveAll %s: %w", path, err) | ||
return fmt.Errorf("failed to RemoveAll %s: %w", path, err) | ||
} | ||
|
||
if len(org.Avatar) > 0 { | ||
avatarPath := org.CustomAvatarRelativePath() | ||
if err := storage.Avatars.Delete(avatarPath); err != nil { | ||
return fmt.Errorf("Failed to remove %s: %w", avatarPath, err) | ||
return fmt.Errorf("failed to remove %s: %w", avatarPath, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// RenameOrganization renames an organization. | ||
func RenameOrganization(ctx context.Context, org *org_model.Organization, newName string, onlyCapitalization bool) error { | ||
if !org.AsUser().IsOrganization() { | ||
return fmt.Errorf("cannot rename user") | ||
} | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err := user_model.IsUsableUsername(newName); err != nil { | ||
return err | ||
} | ||
|
||
ctx, committer, err := db.TxContext(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer committer.Close() | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
oldName := org.Name | ||
|
||
if onlyCapitalization { | ||
org.Name = newName | ||
if err := user_model.UpdateUserCols(ctx, org.AsUser(), "name"); err != nil { | ||
org.Name = oldName | ||
return err | ||
} | ||
if err := committer.Commit(); err != nil { | ||
org.Name = oldName | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
isExist, err := user_model.IsUserExist(ctx, org.ID, newName) | ||
if err != nil { | ||
return err | ||
} | ||
if isExist { | ||
return user_model.ErrUserAlreadyExist{ | ||
Name: newName, | ||
} | ||
} | ||
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. It looks like
Then remove the 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. I don't understand what's your meaning. I have updated this PR. |
||
|
||
if err = repo_model.UpdateRepositoryOwnerName(ctx, oldName, newName); err != nil { | ||
return err | ||
} | ||
|
||
if err = user_model.NewUserRedirect(ctx, org.ID, oldName, newName); err != nil { | ||
return err | ||
} | ||
|
||
if err := agit.UserNameChanged(ctx, org.AsUser(), newName); err != nil { | ||
return err | ||
} | ||
if err := container_service.UpdateRepositoryNames(ctx, org.AsUser(), newName); err != nil { | ||
return err | ||
} | ||
|
||
org.Name = newName | ||
org.LowerName = strings.ToLower(newName) | ||
if err := user_model.UpdateUserCols(ctx, org.AsUser(), "name", "lower_name"); err != nil { | ||
return err | ||
} | ||
|
||
// Do not fail if directory does not exist | ||
if err = util.Rename(user_model.UserPath(oldName), user_model.UserPath(newName)); err != nil && !os.IsNotExist(err) { | ||
return fmt.Errorf("rename user directory: %w", err) | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if err = committer.Commit(); err != nil { | ||
if err2 := util.Rename(user_model.UserPath(newName), user_model.UserPath(oldName)); err2 != nil && !os.IsNotExist(err2) { | ||
log.Critical("Unable to rollback directory change during failed username change from: %s to: %s. DB Error: %v. Filesystem Error: %v", oldName, newName, err, err2) | ||
return fmt.Errorf("failed to rollback directory change during failed username change from: %s to: %s. DB Error: %w. Filesystem Error: %v", oldName, newName, err, err2) | ||
} | ||
return err | ||
} | ||
|
||
log.Trace("Org name changed: %s -> %s", oldName, newName) | ||
return nil | ||
} |
Uh oh!
There was an error while loading. Please reload this page.