mirror of https://github.com/go-gitea/gitea
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>pull/14177/head^2
parent
4f608ad31f
commit
bc05ddc0eb
@ -0,0 +1,4 @@ |
||||
- |
||||
id: 1 |
||||
lower_name: olduser1 |
||||
redirect_user_id: 1 |
@ -0,0 +1,24 @@ |
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"xorm.io/xorm" |
||||
) |
||||
|
||||
func addUserRedirect(x *xorm.Engine) (err error) { |
||||
type UserRedirect struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"` |
||||
RedirectUserID int64 |
||||
} |
||||
|
||||
if err := x.Sync2(new(UserRedirect)); err != nil { |
||||
return fmt.Errorf("Sync2: %v", err) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,52 @@ |
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package models |
||||
|
||||
import "strings" |
||||
|
||||
// UserRedirect represents that a user name should be redirected to another
|
||||
type UserRedirect struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"` |
||||
RedirectUserID int64 // userID to redirect to
|
||||
} |
||||
|
||||
// LookupUserRedirect look up userID if a user has a redirect name
|
||||
func LookupUserRedirect(userName string) (int64, error) { |
||||
userName = strings.ToLower(userName) |
||||
redirect := &UserRedirect{LowerName: userName} |
||||
if has, err := x.Get(redirect); err != nil { |
||||
return 0, err |
||||
} else if !has { |
||||
return 0, ErrUserRedirectNotExist{Name: userName} |
||||
} |
||||
return redirect.RedirectUserID, nil |
||||
} |
||||
|
||||
// newUserRedirect create a new user redirect
|
||||
func newUserRedirect(e Engine, ID int64, oldUserName, newUserName string) error { |
||||
oldUserName = strings.ToLower(oldUserName) |
||||
newUserName = strings.ToLower(newUserName) |
||||
|
||||
if err := deleteUserRedirect(e, newUserName); err != nil { |
||||
return err |
||||
} |
||||
|
||||
if _, err := e.Insert(&UserRedirect{ |
||||
LowerName: oldUserName, |
||||
RedirectUserID: ID, |
||||
}); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// deleteUserRedirect delete any redirect from the specified user name to
|
||||
// anything else
|
||||
func deleteUserRedirect(e Engine, userName string) error { |
||||
userName = strings.ToLower(userName) |
||||
_, err := e.Delete(&UserRedirect{LowerName: userName}) |
||||
return err |
||||
} |
@ -0,0 +1,69 @@ |
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package models |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestLookupUserRedirect(t *testing.T) { |
||||
assert.NoError(t, PrepareTestDatabase()) |
||||
|
||||
userID, err := LookupUserRedirect("olduser1") |
||||
assert.NoError(t, err) |
||||
assert.EqualValues(t, 1, userID) |
||||
|
||||
_, err = LookupUserRedirect("doesnotexist") |
||||
assert.True(t, IsErrUserRedirectNotExist(err)) |
||||
} |
||||
|
||||
func TestNewUserRedirect(t *testing.T) { |
||||
// redirect to a completely new name
|
||||
assert.NoError(t, PrepareTestDatabase()) |
||||
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) |
||||
assert.NoError(t, newUserRedirect(x, user.ID, user.Name, "newusername")) |
||||
|
||||
AssertExistsAndLoadBean(t, &UserRedirect{ |
||||
LowerName: user.LowerName, |
||||
RedirectUserID: user.ID, |
||||
}) |
||||
AssertExistsAndLoadBean(t, &UserRedirect{ |
||||
LowerName: "olduser1", |
||||
RedirectUserID: user.ID, |
||||
}) |
||||
} |
||||
|
||||
func TestNewUserRedirect2(t *testing.T) { |
||||
// redirect to previously used name
|
||||
assert.NoError(t, PrepareTestDatabase()) |
||||
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User) |
||||
assert.NoError(t, newUserRedirect(x, user.ID, user.Name, "olduser1")) |
||||
|
||||
AssertExistsAndLoadBean(t, &UserRedirect{ |
||||
LowerName: user.LowerName, |
||||
RedirectUserID: user.ID, |
||||
}) |
||||
AssertNotExistsBean(t, &UserRedirect{ |
||||
LowerName: "olduser1", |
||||
RedirectUserID: user.ID, |
||||
}) |
||||
} |
||||
|
||||
func TestNewUserRedirect3(t *testing.T) { |
||||
// redirect for a previously-unredirected user
|
||||
assert.NoError(t, PrepareTestDatabase()) |
||||
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) |
||||
assert.NoError(t, newUserRedirect(x, user.ID, user.Name, "newusername")) |
||||
|
||||
AssertExistsAndLoadBean(t, &UserRedirect{ |
||||
LowerName: user.LowerName, |
||||
RedirectUserID: user.ID, |
||||
}) |
||||
} |
@ -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") |
||||
} |
Loading…
Reference in new issue