Skip to content

Commit 5bac1a6

Browse files
guillep2ktechknowlogick
authored andcommitted
Allow users with explicit read access to give approvals (#8398)
1 parent b6fba5b commit 5bac1a6

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

models/branches.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, opts
192192
}
193193
protectBranch.MergeWhitelistUserIDs = whitelist
194194

195-
whitelist, err = updateUserWhitelist(repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
195+
whitelist, err = updateApprovalWhitelist(repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
196196
if err != nil {
197197
return err
198198
}
@@ -298,6 +298,27 @@ func (repo *Repository) IsProtectedBranchForMerging(pr *PullRequest, branchName
298298
return false, nil
299299
}
300300

301+
// updateApprovalWhitelist checks whether the user whitelist changed and returns a whitelist with
302+
// the users from newWhitelist which have explicit read or write access to the repo.
303+
func updateApprovalWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
304+
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
305+
if !hasUsersChanged {
306+
return currentWhitelist, nil
307+
}
308+
309+
whitelist = make([]int64, 0, len(newWhitelist))
310+
for _, userID := range newWhitelist {
311+
if reader, err := repo.IsReader(userID); err != nil {
312+
return nil, err
313+
} else if !reader {
314+
continue
315+
}
316+
whitelist = append(whitelist, userID)
317+
}
318+
319+
return
320+
}
321+
301322
// updateUserWhitelist checks whether the user whitelist changed and returns a whitelist with
302323
// the users from newWhitelist which have write access to the repo.
303324
func updateUserWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {

models/repo.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,11 +707,24 @@ func (repo *Repository) CanEnableEditor() bool {
707707
return !repo.IsMirror
708708
}
709709

710+
// GetReaders returns all users that have explicit read access or higher to the repository.
711+
func (repo *Repository) GetReaders() (_ []*User, err error) {
712+
return repo.getUsersWithAccessMode(x, AccessModeRead)
713+
}
714+
710715
// GetWriters returns all users that have write access to the repository.
711716
func (repo *Repository) GetWriters() (_ []*User, err error) {
712717
return repo.getUsersWithAccessMode(x, AccessModeWrite)
713718
}
714719

720+
// IsReader returns true if user has explicit read access or higher to the repository.
721+
func (repo *Repository) IsReader(userID int64) (bool, error) {
722+
if repo.OwnerID == userID {
723+
return true, nil
724+
}
725+
return x.Where("repo_id = ? AND user_id = ? AND mode >= ?", repo.ID, userID, AccessModeRead).Get(&Access{})
726+
}
727+
715728
// getUsersWithAccessMode returns users that have at least given access mode to the repository.
716729
func (repo *Repository) getUsersWithAccessMode(e Engine, mode AccessMode) (_ []*User, err error) {
717730
if err = repo.getOwner(e); err != nil {

routers/repo/setting_protected_branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ func SettingsProtectedBranch(c *context.Context) {
116116
}
117117
}
118118

119-
users, err := c.Repo.Repository.GetWriters()
119+
users, err := c.Repo.Repository.GetReaders()
120120
if err != nil {
121-
c.ServerError("Repo.Repository.GetWriters", err)
121+
c.ServerError("Repo.Repository.GetReaders", err)
122122
return
123123
}
124124
c.Data["Users"] = users

0 commit comments

Comments
 (0)