mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-10-10 19:32:02 +00:00
[v11.0/forgejo] fix: only redirect to a new owner (organization or user) if the user has permissions to view the new owner (#9089)
**Backport: https://codeberg.org/forgejo/forgejo/pulls/9072** Co-authored-by: Gusted <postmaster@gusted.xyz> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9089
This commit is contained in:
parent
3de4b351a2
commit
a040ef4b0d
18 changed files with 252 additions and 67 deletions
|
@ -3,3 +3,9 @@
|
|||
owner_id: 2
|
||||
lower_name: oldrepo1
|
||||
redirect_repo_id: 1
|
||||
|
||||
-
|
||||
id: 2
|
||||
owner_id: 17
|
||||
lower_name: oldrepo24
|
||||
redirect_repo_id: 24
|
||||
|
|
|
@ -3,3 +3,15 @@
|
|||
lower_name: olduser1
|
||||
redirect_user_id: 1
|
||||
created_unix: 1730000000
|
||||
|
||||
-
|
||||
id: 2
|
||||
lower_name: oldorg22
|
||||
redirect_user_id: 22
|
||||
created_unix: 1730000000
|
||||
|
||||
-
|
||||
id: 3
|
||||
lower_name: oldorg23
|
||||
redirect_user_id: 23
|
||||
created_unix: 1730000000
|
||||
|
|
|
@ -14,8 +14,9 @@ import (
|
|||
|
||||
// ErrRedirectNotExist represents a "RedirectNotExist" kind of error.
|
||||
type ErrRedirectNotExist struct {
|
||||
OwnerID int64
|
||||
RepoName string
|
||||
OwnerID int64
|
||||
RepoName string
|
||||
MissingPermission bool
|
||||
}
|
||||
|
||||
// IsErrRedirectNotExist check if an error is an ErrRepoRedirectNotExist.
|
||||
|
@ -49,8 +50,8 @@ func init() {
|
|||
db.RegisterModel(new(Redirect))
|
||||
}
|
||||
|
||||
// LookupRedirect look up if a repository has a redirect name
|
||||
func LookupRedirect(ctx context.Context, ownerID int64, repoName string) (int64, error) {
|
||||
// GetRedirect returns the redirect for a given pair of ownerID and repository name.
|
||||
func GetRedirect(ctx context.Context, ownerID int64, repoName string) (int64, error) {
|
||||
repoName = strings.ToLower(repoName)
|
||||
redirect := &Redirect{OwnerID: ownerID, LowerName: repoName}
|
||||
if has, err := db.GetEngine(ctx).Get(redirect); err != nil {
|
||||
|
|
|
@ -10,21 +10,9 @@ import (
|
|||
repo_model "forgejo.org/models/repo"
|
||||
"forgejo.org/models/unittest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLookupRedirect(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
repoID, err := repo_model.LookupRedirect(db.DefaultContext, 2, "oldrepo1")
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 1, repoID)
|
||||
|
||||
_, err = repo_model.LookupRedirect(db.DefaultContext, unittest.NonexistentID, "doesnotexist")
|
||||
assert.True(t, repo_model.IsErrRedirectNotExist(err))
|
||||
}
|
||||
|
||||
func TestNewRedirect(t *testing.T) {
|
||||
// redirect to a completely new name
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
|
|
@ -21,7 +21,8 @@ import (
|
|||
|
||||
// ErrUserRedirectNotExist represents a "UserRedirectNotExist" kind of error.
|
||||
type ErrUserRedirectNotExist struct {
|
||||
Name string
|
||||
Name string
|
||||
MissingPermission bool
|
||||
}
|
||||
|
||||
// IsErrUserRedirectNotExist check if an error is an ErrUserRedirectNotExist.
|
||||
|
@ -81,15 +82,6 @@ func GetUserRedirect(ctx context.Context, userName string) (*Redirect, error) {
|
|||
return redirect, nil
|
||||
}
|
||||
|
||||
// LookupUserRedirect look up userID if a user has a redirect name
|
||||
func LookupUserRedirect(ctx context.Context, userName string) (int64, error) {
|
||||
redirect, err := GetUserRedirect(ctx, userName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return redirect.RedirectUserID, nil
|
||||
}
|
||||
|
||||
// NewUserRedirect create a new user redirect
|
||||
func NewUserRedirect(ctx context.Context, ID int64, oldUserName, newUserName string) error {
|
||||
oldUserName = strings.ToLower(oldUserName)
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forgejo.org/models/db"
|
||||
"forgejo.org/models/unittest"
|
||||
user_model "forgejo.org/models/user"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLookupUserRedirect(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
userID, err := user_model.LookupUserRedirect(db.DefaultContext, "olduser1")
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 1, userID)
|
||||
|
||||
_, err = user_model.LookupUserRedirect(db.DefaultContext, "doesnotexist")
|
||||
assert.True(t, user_model.IsErrUserRedirectNotExist(err))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue