Skip to content

Commit 4843723

Browse files
guillep2ktechknowlogick
authored andcommitted
Allow users with explicit read access to give approvals (#8382)
1 parent 736ad8f commit 4843723

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
@@ -195,7 +195,7 @@ func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, opts
195195
}
196196
protectBranch.MergeWhitelistUserIDs = whitelist
197197

198-
whitelist, err = updateUserWhitelist(repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
198+
whitelist, err = updateApprovalWhitelist(repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
199199
if err != nil {
200200
return err
201201
}
@@ -301,6 +301,27 @@ func (repo *Repository) IsProtectedBranchForMerging(pr *PullRequest, branchName
301301
return false, nil
302302
}
303303

304+
// updateApprovalWhitelist checks whether the user whitelist changed and returns a whitelist with
305+
// the users from newWhitelist which have explicit read or write access to the repo.
306+
func updateApprovalWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
307+
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
308+
if !hasUsersChanged {
309+
return currentWhitelist, nil
310+
}
311+
312+
whitelist = make([]int64, 0, len(newWhitelist))
313+
for _, userID := range newWhitelist {
314+
if reader, err := repo.IsReader(userID); err != nil {
315+
return nil, err
316+
} else if !reader {
317+
continue
318+
}
319+
whitelist = append(whitelist, userID)
320+
}
321+
322+
return
323+
}
324+
304325
// updateUserWhitelist checks whether the user whitelist changed and returns a whitelist with
305326
// the users from newWhitelist which have write access to the repo.
306327
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
@@ -735,11 +735,24 @@ func (repo *Repository) CanEnableEditor() bool {
735735
return !repo.IsMirror
736736
}
737737

738+
// GetReaders returns all users that have explicit read access or higher to the repository.
739+
func (repo *Repository) GetReaders() (_ []*User, err error) {
740+
return repo.getUsersWithAccessMode(x, AccessModeRead)
741+
}
742+
738743
// GetWriters returns all users that have write access to the repository.
739744
func (repo *Repository) GetWriters() (_ []*User, err error) {
740745
return repo.getUsersWithAccessMode(x, AccessModeWrite)
741746
}
742747

748+
// IsReader returns true if user has explicit read access or higher to the repository.
749+
func (repo *Repository) IsReader(userID int64) (bool, error) {
750+
if repo.OwnerID == userID {
751+
return true, nil
752+
}
753+
return x.Where("repo_id = ? AND user_id = ? AND mode >= ?", repo.ID, userID, AccessModeRead).Get(&Access{})
754+
}
755+
743756
// getUsersWithAccessMode returns users that have at least given access mode to the repository.
744757
func (repo *Repository) getUsersWithAccessMode(e Engine, mode AccessMode) (_ []*User, err error) {
745758
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
@@ -117,9 +117,9 @@ func SettingsProtectedBranch(c *context.Context) {
117117
}
118118
}
119119

120-
users, err := c.Repo.Repository.GetWriters()
120+
users, err := c.Repo.Repository.GetReaders()
121121
if err != nil {
122-
c.ServerError("Repo.Repository.GetWriters", err)
122+
c.ServerError("Repo.Repository.GetReaders", err)
123123
return
124124
}
125125
c.Data["Users"] = users

0 commit comments

Comments
 (0)