Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list groups in non inherited filter of my projects #431

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions core/policy/filter.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package policy

type Filter struct {
OrgID string
ProjectID string
GroupID string
RoleID string

PrincipalType string
PrincipalID string
OrgID string
ProjectID string
GroupID string
RoleID string
PrincipalIDs []string
ResourceType string
}
28 changes: 25 additions & 3 deletions core/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type AuthnService interface {

type GroupService interface {
GetByIDs(ctx context.Context, ids []string) ([]group.Group, error)
ListByUser(ctx context.Context, userID string, flt group.Filter) ([]group.Group, error)
}

type Service struct {
Expand Down Expand Up @@ -123,18 +124,37 @@ func (s Service) ListByUser(ctx context.Context, userID string, flt Filter) ([]P

var projIDs []string
if flt.NonInherited == true {
// direct added users
policies, err := s.policyService.List(ctx, policy.Filter{
PrincipalType: schema.UserPrincipal,
PrincipalID: userID,
ResourceType: schema.ProjectNamespace,
})
if err != nil {
return nil, err
}
for _, pol := range policies {
projIDs = append(projIDs, pol.ResourceID)
}

// added via groups
groups, err := s.groupService.ListByUser(ctx, userID, group.Filter{})
if err != nil {
return nil, err
}
groupIDs := utils.Map(groups, func(g group.Group) string {
return g.ID
})
policies, err = s.policyService.List(ctx, policy.Filter{
PrincipalType: schema.GroupPrincipal,
PrincipalIDs: groupIDs,
ResourceType: schema.ProjectNamespace,
})
if err != nil {
return nil, err
}
for _, pol := range policies {
if pol.ResourceType == schema.ProjectNamespace {
projIDs = append(projIDs, pol.ResourceID)
}
projIDs = append(projIDs, pol.ResourceID)
}
} else {
projIDs, err = s.relationService.LookupResources(ctx, relation.Relation{
Expand All @@ -152,6 +172,8 @@ func (s Service) ListByUser(ctx context.Context, userID string, flt Filter) ([]P
}
}

// de-duplicate project IDs
projIDs = utils.Deduplicate(projIDs)
if len(projIDs) == 0 {
return []Project{}, nil
}
Expand Down
9 changes: 9 additions & 0 deletions internal/store/postgres/policy_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,21 @@ func (r PolicyRepository) List(ctx context.Context, flt policy.Filter) ([]policy
stmt = stmt.Where(goqu.Ex{
"principal_id": flt.PrincipalID,
})
} else if len(flt.PrincipalIDs) > 0 {
stmt = stmt.Where(goqu.Ex{
"principal_id": flt.PrincipalIDs,
})
}
if flt.PrincipalType != "" {
stmt = stmt.Where(goqu.Ex{
"principal_type": flt.PrincipalType,
})
}
if flt.ResourceType != "" {
stmt = stmt.Where(goqu.Ex{
"resource_type": flt.ResourceType,
})
}
if flt.RoleID != "" {
stmt = stmt.Where(goqu.Ex{
"role_id": flt.RoleID,
Expand Down
17 changes: 17 additions & 0 deletions pkg/utils/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,20 @@ func Filter[T any](s []T, f func(T) bool) []T {
}
return result
}

func Deduplicate[T comparable](s []T) []T {
// Create a map of all unique elements.
seen := make(map[T]struct{})
j := 0
for _, v := range s {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
s[j] = v
j++
}

// Slice the map to create a new slice of unique elements.
return s[:j]
}
2 changes: 1 addition & 1 deletion test/e2e/regression/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func (s *APIRegressionTestSuite) TestProjectAPI() {
NonInherited: true,
})
s.Assert().NoError(err)
s.Assert().Equal(0, len(listCurrentUserProjectsNonInheritedResp.GetProjects()))
s.Assert().Equal(1, len(listCurrentUserProjectsNonInheritedResp.GetProjects()))
})
}

Expand Down
Loading