Skip to content

Commit d898a24

Browse files
committed
fix maximum permission granted to doer
1 parent 350d70e commit d898a24

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

models/perm/access/repo_permission.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
267267
perm.units = repo.Units
268268

269269
// anonymous user visit private repo.
270-
// TODO: anonymous user visit public unit of private repo???
271270
if user == nil && repo.IsPrivate {
272271
perm.AccessMode = perm_model.AccessModeNone
273272
return perm, nil
@@ -286,7 +285,8 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
286285
}
287286

288287
// Prevent strangers from checking out public repo of private organization/users
289-
// Allow user if they are collaborator of a repo within a private user or a private organization but not a member of the organization itself
288+
// Allow user if they are a collaborator of a repo within a private user or a private organization but not a member of the organization itself
289+
// TODO: rename it to "IsOwnerVisibleToDoer"
290290
if !organization.HasOrgOrUserVisible(ctx, repo.Owner, user) && !isCollaborator {
291291
perm.AccessMode = perm_model.AccessModeNone
292292
return perm, nil
@@ -304,7 +304,7 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
304304
return perm, nil
305305
}
306306

307-
// plain user
307+
// plain user TODO: this check should be replaced, only need to check collaborator access mode
308308
perm.AccessMode, err = accessLevel(ctx, user, repo)
309309
if err != nil {
310310
return perm, err
@@ -314,6 +314,19 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
314314
return perm, nil
315315
}
316316

317+
// now: the owner is visible to doer, if the repo is public, then the min access mode is read
318+
minAccessMode := util.Iif(!repo.IsPrivate && !user.IsRestricted, perm_model.AccessModeRead, perm_model.AccessModeNone)
319+
perm.AccessMode = max(perm.AccessMode, minAccessMode)
320+
321+
// get units mode from teams
322+
teams, err := organization.GetUserRepoTeams(ctx, repo.OwnerID, user.ID, repo.ID)
323+
if err != nil {
324+
return perm, err
325+
}
326+
if len(teams) == 0 {
327+
return perm, nil
328+
}
329+
317330
perm.unitsMode = make(map[unit.Type]perm_model.AccessMode)
318331

319332
// Collaborators on organization
@@ -323,12 +336,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
323336
}
324337
}
325338

326-
// get units mode from teams
327-
teams, err := organization.GetUserRepoTeams(ctx, repo.OwnerID, user.ID, repo.ID)
328-
if err != nil {
329-
return perm, err
330-
}
331-
332339
// if user in an owner team
333340
for _, team := range teams {
334341
if team.HasAdminAccess() {
@@ -339,19 +346,12 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
339346
}
340347

341348
for _, u := range repo.Units {
342-
var found bool
343349
for _, team := range teams {
350+
unitAccessMode := minAccessMode
344351
if teamMode, exist := team.UnitAccessModeEx(ctx, u.Type); exist {
345-
perm.unitsMode[u.Type] = max(perm.unitsMode[u.Type], teamMode)
346-
found = true
347-
}
348-
}
349-
350-
// for a public repo on an organization, a non-restricted user has read permission on non-team defined units.
351-
if !found && !repo.IsPrivate && !user.IsRestricted {
352-
if _, ok := perm.unitsMode[u.Type]; !ok {
353-
perm.unitsMode[u.Type] = perm_model.AccessModeRead
352+
unitAccessMode = max(perm.unitsMode[u.Type], unitAccessMode, teamMode)
354353
}
354+
perm.unitsMode[u.Type] = unitAccessMode
355355
}
356356
}
357357

0 commit comments

Comments
 (0)