-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Redirect on changed user and org name #11649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
6543
merged 26 commits into
go-gitea:master
from
AndrewBezold:Redirect-user-and-org-#9531
Jan 24, 2021
+325
−64
Merged
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
6772e89
Add redirect for user
AndrewBezold 1524fef
Add redirect for orgs
AndrewBezold b4ecccf
Add user redirect test
AndrewBezold 4156980
Appease linter
AndrewBezold 9112703
Add comment to DeleteUserRedirect function
AndrewBezold aecbb0d
Fix locale changes
AndrewBezold b4eea72
Fix GetUserByParams
AndrewBezold 14fa02b
Fix orgAssignment
AndrewBezold 387ddec
Remove debug logging
AndrewBezold 0167007
Add redirect prompt
AndrewBezold cd4c6eb
Merge branch 'master' into Redirect-user-and-org-#9531
6543 b1c38d1
Merge branch 'master' into pr-11649
6543 b4ac0d6
Dont Export DeleteUserRedirect & only use it within a session
6543 988075b
Unexport newUserRedirect
6543 adb0bca
cleanup
6543 6909535
Fix & Dedub API code
6543 68c7c78
Format Template
6543 51f1194
Add Migration & rm dublicat
6543 10d3ec9
Refactor: unexport newRepoRedirect() & rm dedub del exec
6543 b47f66f
Merge branch 'master' into Redirect-user-and-org-#9531
6543 4c7f0f0
if this fails we'll need to re-rename the user directory
6543 7d3849f
fmt
6543 6d1b5fd
Merge branch 'master' into Redirect-user-and-org-#9531
6543 f11bb7e
Update models/user.go
6543 aaef772
Merge branch 'master' into Redirect-user-and-org-#9531
6543 df25bf1
Merge branch 'master' into Redirect-user-and-org-#9531
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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") | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.