Skip to content

Commit 1a48694

Browse files
update AccessibleGroupCondition function to take a minimum perm.AccessMode as a parameter
1 parent 5ad45e7 commit 1a48694

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

models/group/group.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (g *Group) LoadSubgroups(ctx context.Context, recursive bool) error {
9494
}
9595

9696
func (g *Group) LoadAccessibleSubgroups(ctx context.Context, recursive bool, doer *user_model.User) error {
97-
return g.doLoadSubgroups(ctx, recursive, AccessibleGroupCondition(doer, unit.TypeInvalid), 0)
97+
return g.doLoadSubgroups(ctx, recursive, AccessibleGroupCondition(doer, unit.TypeInvalid, perm.AccessModeRead), 0)
9898
}
9999

100100
func (g *Group) LoadAttributes(ctx context.Context) error {
@@ -129,13 +129,12 @@ func (g *Group) LoadOwner(ctx context.Context) error {
129129
return err
130130
}
131131

132-
func (g *Group) CanAccess(ctx context.Context, userID int64) (bool, error) {
133-
return g.CanAccessAtLevel(ctx, userID, perm.AccessModeRead)
132+
func (g *Group) CanAccess(ctx context.Context, user *user_model.User) (bool, error) {
133+
return g.CanAccessAtLevel(ctx, user, perm.AccessModeRead)
134134
}
135135

136-
func (g *Group) CanAccessAtLevel(ctx context.Context, userID int64, level perm.AccessMode) (bool, error) {
137-
return db.GetEngine(ctx).
138-
Where(UserOrgTeamPermCond("id", userID, level)).Table("repo_group").Exist()
136+
func (g *Group) CanAccessAtLevel(ctx context.Context, user *user_model.User, level perm.AccessMode) (bool, error) {
137+
return db.GetEngine(ctx).Where(AccessibleGroupCondition(user, unit.TypeInvalid, level).And(builder.Eq{"`repo_group`.id": g.ID})).Exist(&Group{})
139138
}
140139

141140
func (g *Group) IsOwnedBy(ctx context.Context, userID int64) (bool, error) {
@@ -337,9 +336,10 @@ func UpdateGroup(ctx context.Context, group *Group) error {
337336
func MoveGroup(ctx context.Context, group *Group, newParent int64, newSortOrder int) error {
338337
sess := db.GetEngine(ctx)
339338
ng, err := GetGroupByID(ctx, newParent)
340-
if !IsErrGroupNotExist(err) {
339+
if err != nil && !IsErrGroupNotExist(err) {
341340
return err
342341
}
342+
343343
if ng != nil {
344344
if ng.OwnerID != group.OwnerID {
345345
return fmt.Errorf("group[%d]'s ownerID is not equal to new parent group[%d]'s owner ID", group.ID, ng.ID)

models/group/group_list.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func userOrgTeamGroupBuilder(userID int64) *builder.Builder {
3333
Where(builder.Eq{"`team_user`.uid": userID})
3434
}
3535

36+
// UserOrgTeamPermCond returns a condition to select ids of groups that a user can access at the level described by `level`
3637
func UserOrgTeamPermCond(idStr string, userID int64, level perm.AccessMode) builder.Cond {
3738
selCond := userOrgTeamGroupBuilder(userID)
3839
selCond = selCond.InnerJoin("team", "`team`.id = `repo_group_team`.team_id").
@@ -60,22 +61,23 @@ func userOrgTeamUnitGroupBuilder(userID int64, unitType unit.Type) *builder.Buil
6061
}
6162

6263
// AccessibleGroupCondition returns a condition that matches groups which a user can access via the specified unit
63-
func AccessibleGroupCondition(user *user_model.User, unitType unit.Type) builder.Cond {
64+
func AccessibleGroupCondition(user *user_model.User, unitType unit.Type, minMode perm.AccessMode) builder.Cond {
6465
cond := builder.NewCond()
6566
if user == nil || !user.IsRestricted || user.ID <= 0 {
6667
orgVisibilityLimit := []structs.VisibleType{structs.VisibleTypePrivate}
6768
if user == nil || user.ID <= 0 {
6869
orgVisibilityLimit = append(orgVisibilityLimit, structs.VisibleTypeLimited)
6970
}
7071
cond = cond.Or(builder.And(
71-
builder.Eq{"`repo_group`.is_private": false},
72+
builder.Eq{"`repo_group`.visibility": structs.VisibleTypePublic},
7273
builder.NotIn("`repo_group`.owner_id", builder.Select("id").From("`user`").Where(
7374
builder.And(
7475
builder.Eq{"type": user_model.UserTypeOrganization},
7576
builder.In("visibility", orgVisibilityLimit)),
7677
))))
7778
}
7879
if user != nil {
80+
cond = cond.Or(UserOrgTeamPermCond("`repo_group`.id", user.ID, minMode))
7981
if unitType == unit.TypeInvalid {
8082
cond = cond.Or(
8183
UserOrgTeamGroupCond("`repo_group`.id", user.ID),

routers/api/v1/api.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,8 @@ func reqGroupMembership(mode perm.AccessMode, needsCreatePerm bool) func(ctx *co
529529
ctx.APIErrorInternal(err)
530530
return
531531
}
532-
var canAccess bool
533-
if ctx.IsSigned {
534-
canAccess, err = g.CanAccessAtLevel(ctx, ctx.Doer.ID, mode)
535-
} else {
536-
canAccess, err = g.CanAccessAtLevel(ctx, 0, mode)
537-
}
532+
canAccess, err := g.CanAccessAtLevel(ctx, ctx.Doer, mode)
533+
538534
if err != nil {
539535
ctx.APIErrorInternal(err)
540536
return

0 commit comments

Comments
 (0)