-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
4f608ad
commit bc05ddc
Showing
25 changed files
with
325 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- | ||
id: 1 | ||
lower_name: olduser1 | ||
redirect_user_id: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.