diff options
author | Andrew Bezold <andrew.bezold@gmail.com> | 2021-01-24 10:23:05 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-24 16:23:05 +0100 |
commit | bc05ddc0ebd6fdc826ef2beec99304bac60ddd8a (patch) | |
tree | 03d52e07feedd37e169a04980f9efbb8bd1aca65 /models/user.go | |
parent | 4f608ad31f538cb45411ff9a2238db812b5ca414 (diff) | |
download | gitea-bc05ddc0ebd6fdc826ef2beec99304bac60ddd8a.tar.gz gitea-bc05ddc0ebd6fdc826ef2beec99304bac60ddd8a.zip |
Redirect on changed user and org name (#11649)
* Add redirect for user
* Add redirect for orgs
* Add user redirect test
* Appease linter
* Add comment to DeleteUserRedirect function
* Fix locale changes
* Fix GetUserByParams
* Fix orgAssignment
* Remove debug logging
* Add redirect prompt
* Dont Export DeleteUserRedirect & only use it within a session
* Unexport newUserRedirect
* cleanup
* Fix & Dedub API code
* Format Template
* Add Migration & rm dublicat
* Refactor: unexport newRepoRedirect() & rm dedub del exec
* if this fails we'll need to re-rename the user directory
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/models/user.go b/models/user.go index 746608aaa4..8147c9f626 100644 --- a/models/user.go +++ b/models/user.go @@ -863,6 +863,10 @@ func CreateUser(u *User) (err error) { return ErrUserAlreadyExist{u.Name} } + if err = deleteUserRedirect(sess, u.Name); err != nil { + return err + } + u.Email = strings.ToLower(u.Email) isExist, err = sess. Where("email=?", u.Email). @@ -973,6 +977,7 @@ func VerifyActiveEmailCode(code, email string) *EmailAddress { // ChangeUserName changes all corresponding setting from old user name to new one. func ChangeUserName(u *User, newUserName string) (err error) { + oldUserName := u.Name if err = IsUsableUsername(newUserName); err != nil { return err } @@ -990,16 +995,28 @@ func ChangeUserName(u *User, newUserName string) (err error) { return ErrUserAlreadyExist{newUserName} } - if _, err = sess.Exec("UPDATE `repository` SET owner_name=? WHERE owner_name=?", newUserName, u.Name); err != nil { + if _, err = sess.Exec("UPDATE `repository` SET owner_name=? WHERE owner_name=?", newUserName, oldUserName); err != nil { return fmt.Errorf("Change repo owner name: %v", err) } // Do not fail if directory does not exist - if err = os.Rename(UserPath(u.Name), UserPath(newUserName)); err != nil && !os.IsNotExist(err) { + if err = os.Rename(UserPath(oldUserName), UserPath(newUserName)); err != nil && !os.IsNotExist(err) { return fmt.Errorf("Rename user directory: %v", err) } - return sess.Commit() + if err = newUserRedirect(sess, u.ID, oldUserName, newUserName); err != nil { + return err + } + + if err = sess.Commit(); err != nil { + if err2 := os.Rename(UserPath(newUserName), UserPath(oldUserName)); 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", oldUserName, newUserName, err, err2) + return fmt.Errorf("failed to rollback directory change during failed username change from: %s to: %s. DB Error: %w. Filesystem Error: %v", oldUserName, newUserName, err, err2) + } + return err + } + + return nil } // checkDupEmail checks whether there are the same email with the user |