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 /routers/api/v1 | |
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 'routers/api/v1')
-rw-r--r-- | routers/api/v1/api.go | 17 | ||||
-rw-r--r-- | routers/api/v1/user/helper.go | 36 | ||||
-rw-r--r-- | routers/api/v1/user/key.go | 19 | ||||
-rw-r--r-- | routers/api/v1/user/user.go | 19 |
4 files changed, 55 insertions, 36 deletions
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 02ad8ab360..876f48ca5c 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -134,7 +134,13 @@ func repoAssignment() macaron.Handler { owner, err = models.GetUserByName(userName) if err != nil { if models.IsErrUserNotExist(err) { - ctx.NotFound() + if redirectUserID, err := models.LookupUserRedirect(userName); err == nil { + context.RedirectToUser(ctx.Context, userName, redirectUserID) + } else if models.IsErrUserRedirectNotExist(err) { + ctx.NotFound("GetUserByName", err) + } else { + ctx.Error(http.StatusInternalServerError, "LookupUserRedirect", err) + } } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) } @@ -393,7 +399,14 @@ func orgAssignment(args ...bool) macaron.Handler { ctx.Org.Organization, err = models.GetOrgByName(ctx.Params(":org")) if err != nil { if models.IsErrOrgNotExist(err) { - ctx.NotFound() + redirectUserID, err := models.LookupUserRedirect(ctx.Params(":org")) + if err == nil { + context.RedirectToUser(ctx.Context, ctx.Params(":org"), redirectUserID) + } else if models.IsErrUserRedirectNotExist(err) { + ctx.NotFound("GetOrgByName", err) + } else { + ctx.Error(http.StatusInternalServerError, "LookupUserRedirect", err) + } } else { ctx.Error(http.StatusInternalServerError, "GetOrgByName", err) } diff --git a/routers/api/v1/user/helper.go b/routers/api/v1/user/helper.go new file mode 100644 index 0000000000..fcdac257ed --- /dev/null +++ b/routers/api/v1/user/helper.go @@ -0,0 +1,36 @@ +// Copyright 2021 The Gitea Authors. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package user + +import ( + "net/http" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" +) + +// GetUserByParamsName get user by name +func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { + username := ctx.Params(name) + user, err := models.GetUserByName(username) + if err != nil { + if models.IsErrUserNotExist(err) { + if redirectUserID, err := models.LookupUserRedirect(username); err == nil { + context.RedirectToUser(ctx.Context, username, redirectUserID) + } else { + ctx.NotFound("GetUserByName", err) + } + } else { + ctx.Error(http.StatusInternalServerError, "GetUserByName", err) + } + return nil + } + return user +} + +// GetUserByParams returns user whose name is presented in URL (":username"). +func GetUserByParams(ctx *context.APIContext) *models.User { + return GetUserByParamsName(ctx, ":username") +} diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index 8069660653..fa16df1836 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -39,25 +39,6 @@ func appendPrivateInformation(apiKey *api.PublicKey, key *models.PublicKey, defa return apiKey, nil } -// GetUserByParamsName get user by name -func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { - user, err := models.GetUserByName(ctx.Params(name)) - if err != nil { - if models.IsErrUserNotExist(err) { - ctx.NotFound() - } else { - ctx.Error(http.StatusInternalServerError, "GetUserByName", err) - } - return nil - } - return user -} - -// GetUserByParams returns user whose name is presented in URL paramenter. -func GetUserByParams(ctx *context.APIContext) *models.User { - return GetUserByParamsName(ctx, ":username") -} - func composePublicKeysAPILink() string { return setting.AppURL + "api/v1/user/keys/" } diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index b860219e62..ecc149fe52 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -107,13 +107,8 @@ func GetInfo(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - u, err := models.GetUserByName(ctx.Params(":username")) - if err != nil { - if models.IsErrUserNotExist(err) { - ctx.NotFound() - } else { - ctx.Error(http.StatusInternalServerError, "GetUserByName", err) - } + u := GetUserByParams(ctx) + if ctx.Written() { return } @@ -153,14 +148,8 @@ func GetUserHeatmapData(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - // Get the user to throw an error if it does not exist - user, err := models.GetUserByName(ctx.Params(":username")) - if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Status(http.StatusNotFound) - } else { - ctx.Error(http.StatusInternalServerError, "GetUserByName", err) - } + user := GetUserByParams(ctx) + if ctx.Written() { return } |