Browse Source
Redirect on changed user and org name (#11649)
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>mj-v1.14.3
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 325 additions and 64 deletions
-
15models/error.go
-
4models/fixtures/user_redirect.yml
-
2models/migrations/migrations.go
-
24models/migrations/v167.go
-
1models/models.go
-
4models/org.go
-
11models/repo.go
-
8models/repo_redirect.go
-
6models/repo_redirect_test.go
-
23models/user.go
-
52models/user_redirect.go
-
69models/user_redirect_test.go
-
20modules/context/context.go
-
9modules/context/org.go
-
15modules/context/repo.go
-
2options/locale/locale_en-US.ini
-
17routers/api/v1/api.go
-
36routers/api/v1/user/helper.go
-
19routers/api/v1/user/key.go
-
19routers/api/v1/user/user.go
-
11routers/repo/http.go
-
6routers/user/profile.go
-
5templates/org/settings/options.tmpl
-
5templates/user/settings/profile.tmpl
-
6web_src/js/index.js
@ -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") |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue