mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
fix(repo): require the unit in AccessibleRepositoryCondition (#40059)
`AccessibleRepositoryCondition(user, unitType)` now requires the repo to have the requested unit enabled and not globally disabled, consistent with the per-repo permission checks. This affects code search, Actions run listings, LFS object reuse, recently pushed branches and org project milestones. Written by Claude.
This commit is contained in:
1 parent
6eb5efb6bb
commit
03fc2bdcc8
4 files changed
+26
-2
No files matched your search
@@ -656,8 +656,13 @@ func userAllPublicRepoCond(cond builder.Cond, ownerVisibilityLimit []structs.Vis
|
||||
|
||||
// AccessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
|
||||
func AccessibleRepositoryCondition(user *user_model.User, unitType unit.Type) builder.Cond {
|
||||
unitCond := builder.NewCond()
|
||||
if unitType != unit.TypeInvalid {
|
||||
unitCond = builder.Exists(builder.Select("id").From("repo_unit").Where(builder.Expr("repo_unit.repo_id = `repository`.id").
|
||||
And(builder.Eq{"repo_unit.type": unitType}, builder.NotIn("repo_unit.type", unit.DisabledRepoUnitsGet()))))
|
||||
}
|
||||
if user_model.IsPublicOnlyDoer(user) && !user.IsRestricted {
|
||||
return PublicRepoUnderPublicOwnerCond()
|
||||
return PublicRepoUnderPublicOwnerCond().And(unitCond)
|
||||
}
|
||||
cond := builder.NewCond()
|
||||
|
||||
@@ -696,7 +701,7 @@ func AccessibleRepositoryCondition(user *user_model.User, unitType unit.Type) bu
|
||||
cond = userAllPublicRepoCond(cond, orgVisibilityLimit)
|
||||
}
|
||||
}
|
||||
return cond.And(DoerTokenRepoCond(user))
|
||||
return cond.And(DoerTokenRepoCond(user), unitCond)
|
||||
}
|
||||
|
||||
// SearchRepositoryByName takes keyword and part of repository name to search,
|
||||
|
||||
@@ -485,6 +485,19 @@ func TestSearchRepositoryByTopicName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindUserCodeAccessibleRepoIDsRequiresCodeUnit(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
publicOnlyUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
publicOnlyUser.ExtDoerData = user_model.NewTokenExtDoerData(user_model.CredentialAccessToken, 1, "public-only,read:repository")
|
||||
for _, user := range []*user_model.User{nil, publicOnlyUser} {
|
||||
repoIDs, err := repo_model.FindUserCodeAccessibleRepoIDs(t.Context(), user)
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, repoIDs, int64(1))
|
||||
assert.NotContains(t, repoIDs, int64(50))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindUserActionsAccessibleOwnerRepoIDs(t *testing.T) {
|
||||
require.NoError(t, unittest.PrepareTestDatabase())
|
||||
// user2 is on org3's owner team, so it can access org3's private repo3 (which has the actions unit)
|
||||
@@ -492,6 +505,7 @@ func TestFindUserActionsAccessibleOwnerRepoIDs(t *testing.T) {
|
||||
|
||||
// org3 is a public org owning repo3 (private) and repo32 (public), both with the actions unit
|
||||
const orgID = 3
|
||||
require.NoError(t, db.Insert(t.Context(), &repo_model.RepoUnit{RepoID: 32, Type: unit.TypeActions, Config: &repo_model.ActionsConfig{}}))
|
||||
|
||||
all, err := repo_model.SearchRepositoryIDsByCondition(t.Context(), repo_model.UserActionsAccessibleOwnerRepoCond(orgID, user))
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"gitea.dev/models/db"
|
||||
git_model "gitea.dev/models/git"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unit"
|
||||
"gitea.dev/models/unittest"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/actions"
|
||||
@@ -581,6 +582,7 @@ func testAPIActionsApproveWorkflowRun(t *testing.T) {
|
||||
|
||||
func testAPIActionsListUserWorkflows(t *testing.T) {
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
require.NoError(t, db.Insert(t.Context(), &repo_model.RepoUnit{RepoID: 2, Type: unit.TypeActions, Config: &repo_model.ActionsConfig{}}))
|
||||
session := loginUser(t, user.Name)
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
actions_model "gitea.dev/models/actions"
|
||||
auth_model "gitea.dev/models/auth"
|
||||
"gitea.dev/models/db"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/models/unit"
|
||||
api "gitea.dev/modules/structs"
|
||||
webhook_module "gitea.dev/modules/webhook"
|
||||
"gitea.dev/tests"
|
||||
@@ -134,6 +136,7 @@ func testAPIWorkflowRunsByWorkflowID(t *testing.T, owner, repo, workflowID, user
|
||||
|
||||
func testAPIWorkflowRunBasic(t *testing.T, apiRootURL, userUsername string, runID int64, scope ...auth_model.AccessTokenScope) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
require.NoError(t, db.Insert(t.Context(), &repo_model.RepoUnit{RepoID: 2, Type: unit.TypeActions, Config: &repo_model.ActionsConfig{}}))
|
||||
token := getUserToken(t, userUsername, scope...)
|
||||
|
||||
apiRunsURL := fmt.Sprintf("%s/%s", apiRootURL, "runs")
|
||||
|
||||
Reference in new issue
Block a user