Skip to content

Commit 7e073ec

Browse files
committed
Move permission check to router layer
1 parent 697d238 commit 7e073ec

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

routers/api/v1/repo/collaborators.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/routers/api/v1/utils"
1818
"code.gitea.io/gitea/services/context"
1919
"code.gitea.io/gitea/services/convert"
20+
issue_service "code.gitea.io/gitea/services/issue"
2021
pull_service "code.gitea.io/gitea/services/pull"
2122
repo_service "code.gitea.io/gitea/services/repository"
2223
)
@@ -321,6 +322,12 @@ func GetReviewers(ctx *context.APIContext) {
321322
// "404":
322323
// "$ref": "#/responses/notFound"
323324

325+
canChooseReviewer := issue_service.CanDoerChangeReviewRequests(ctx, ctx.Doer, ctx.Repo.Repository, 0)
326+
if !canChooseReviewer {
327+
ctx.Error(http.StatusForbidden, "GetReviewers", errors.New("doer has no permission to get reviewers"))
328+
return
329+
}
330+
324331
reviewers, err := pull_service.GetReviewers(ctx, ctx.Repo.Repository, ctx.Doer.ID, 0)
325332
if err != nil {
326333
ctx.Error(http.StatusInternalServerError, "ListCollaborators", err)

routers/web/repo/issue_page_meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (d *IssuePageMetaData) retrieveReviewersData(ctx *context.Context) {
186186
if d.Issue == nil {
187187
data.CanChooseReviewer = true
188188
} else {
189-
data.CanChooseReviewer = issue_service.CanDoerChangeReviewRequests(ctx, ctx.Doer, repo, d.Issue)
189+
data.CanChooseReviewer = issue_service.CanDoerChangeReviewRequests(ctx, ctx.Doer, repo, d.Issue.PosterID)
190190
}
191191
}
192192

services/issue/assignee.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func isValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User,
119119
return err
120120
}
121121

122-
canDoerChangeReviewRequests := CanDoerChangeReviewRequests(ctx, doer, issue.Repo, issue)
122+
canDoerChangeReviewRequests := CanDoerChangeReviewRequests(ctx, doer, issue.Repo, issue.PosterID)
123123

124124
if isAdd {
125125
if !permReviewer.CanAccessAny(perm.AccessModeRead, unit.TypePullRequests) {
@@ -178,7 +178,7 @@ func isValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team,
178178
}
179179
}
180180

181-
canDoerChangeReviewRequests := CanDoerChangeReviewRequests(ctx, doer, issue.Repo, issue)
181+
canDoerChangeReviewRequests := CanDoerChangeReviewRequests(ctx, doer, issue.Repo, issue.PosterID)
182182

183183
if isAdd {
184184
if issue.Repo.IsPrivate {
@@ -276,12 +276,12 @@ func teamReviewRequestNotify(ctx context.Context, issue *issues_model.Issue, doe
276276
}
277277

278278
// CanDoerChangeReviewRequests returns if the doer can add/remove review requests of a PR
279-
func CanDoerChangeReviewRequests(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue) bool {
279+
func CanDoerChangeReviewRequests(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, posterID int64) bool {
280280
if repo.IsArchived {
281281
return false
282282
}
283283
// The poster of the PR can change the reviewers
284-
if doer.ID == issue.PosterID {
284+
if doer.ID == posterID {
285285
return true
286286
}
287287

services/pull/reviewer.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package pull
55

66
import (
77
"context"
8-
"fmt"
98

109
"code.gitea.io/gitea/models/db"
1110
"code.gitea.io/gitea/models/organization"
@@ -53,14 +52,6 @@ func GetReviewers(ctx context.Context, repo *repo_model.Repository, doerID, post
5352
return nil, err
5453
}
5554
uniqueUserIDs.AddMultiple(additionalUserIDs...)
56-
57-
if repo.Owner.Visibility.IsLimited() && doerID == 0 {
58-
return nil, fmt.Errorf("permission denied")
59-
}
60-
61-
if (repo.IsPrivate || repo.Owner.Visibility.IsPrivate()) && !uniqueUserIDs.Contains(doerID) {
62-
return nil, fmt.Errorf("permission denied")
63-
}
6455
} else {
6556
userIDs := make([]int64, 0, 10)
6657
if err := e.Table("access").
@@ -70,9 +61,6 @@ func GetReviewers(ctx context.Context, repo *repo_model.Repository, doerID, post
7061
return nil, err
7162
}
7263
uniqueUserIDs.AddMultiple(userIDs...)
73-
if repo.IsPrivate && !uniqueUserIDs.Contains(doerID) && doerID != repo.OwnerID {
74-
return nil, fmt.Errorf("permission denied")
75-
}
7664
}
7765

7866
uniqueUserIDs.Remove(posterID) // posterID should not be in the list of reviewers

0 commit comments

Comments
 (0)