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 /modules | |
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 'modules')
-rw-r--r-- | modules/context/context.go | 20 | ||||
-rw-r--r-- | modules/context/org.go | 9 | ||||
-rw-r--r-- | modules/context/repo.go | 15 |
3 files changed, 39 insertions, 5 deletions
diff --git a/modules/context/context.go b/modules/context/context.go index 1ee31e0ebb..e4121649ae 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -90,6 +90,26 @@ func (ctx *Context) IsUserRepoReaderAny() bool { return ctx.Repo.HasAccess() } +// RedirectToUser redirect to a differently-named user +func RedirectToUser(ctx *Context, userName string, redirectUserID int64) { + user, err := models.GetUserByID(redirectUserID) + if err != nil { + ctx.ServerError("GetUserByID", err) + return + } + + redirectPath := strings.Replace( + ctx.Req.URL.Path, + userName, + user.Name, + 1, + ) + if ctx.Req.URL.RawQuery != "" { + redirectPath += "?" + ctx.Req.URL.RawQuery + } + ctx.Redirect(path.Join(setting.AppSubURL, redirectPath)) +} + // HasAPIError returns true if error occurs in form validation. func (ctx *Context) HasAPIError() bool { hasErr, ok := ctx.Data["HasError"] diff --git a/modules/context/org.go b/modules/context/org.go index 9b87fba9fd..f61a39c666 100644 --- a/modules/context/org.go +++ b/modules/context/org.go @@ -54,7 +54,14 @@ func HandleOrgAssignment(ctx *Context, args ...bool) { ctx.Org.Organization, err = models.GetUserByName(orgName) if err != nil { if models.IsErrUserNotExist(err) { - ctx.NotFound("GetUserByName", err) + redirectUserID, err := models.LookupUserRedirect(orgName) + if err == nil { + RedirectToUser(ctx, orgName, redirectUserID) + } else if models.IsErrUserRedirectNotExist(err) { + ctx.NotFound("GetUserByName", err) + } else { + ctx.ServerError("LookupUserRedirect", err) + } } else { ctx.ServerError("GetUserByName", err) } diff --git a/modules/context/repo.go b/modules/context/repo.go index 2aee6caca4..63cb02dc06 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -411,11 +411,18 @@ func RepoAssignment() macaron.Handler { owner, err = models.GetUserByName(userName) if err != nil { if models.IsErrUserNotExist(err) { - if ctx.Query("go-get") == "1" { - EarlyResponseForGoGetMeta(ctx) - return + redirectUserID, err := models.LookupUserRedirect(userName) + if err == nil { + RedirectToUser(ctx, userName, redirectUserID) + } else if models.IsErrUserRedirectNotExist(err) { + if ctx.Query("go-get") == "1" { + EarlyResponseForGoGetMeta(ctx) + return + } + ctx.NotFound("GetUserByName", nil) + } else { + ctx.ServerError("LookupUserRedirect", err) } - ctx.NotFound("GetUserByName", nil) } else { ctx.ServerError("GetUserByName", err) } |