Skip to content

Commit f1d968b

Browse files
AmanGIT07claude
andauthored
refactor(project): migrate ListByUser callers to membership, delete legacy method (#1648)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e24bcb7 commit f1d968b

22 files changed

Lines changed: 625 additions & 875 deletions

‎cmd/serve.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,13 @@ func buildAPIDependencies(
441441
authnService, serviceUserService, groupService, roleService)
442442

443443
membershipService := membership.NewService(logger, policyService, relationService, roleService, organizationService, userService, projectService, groupService, serviceUserService, auditRecordRepository)
444-
// Setter injection: org/group → membership is circular (membership needs them
445-
// for validation; they need membership for Create). Break the cycle post-init.
444+
// Setter injection: org/group/project → membership is circular (membership
445+
// needs them for validation; they need membership for resource-by-principal
446+
// listing). Break the cycle post-init.
446447
organizationService.SetMembershipService(membershipService)
447448
serviceUserService.SetMembershipService(membershipService)
448449
groupService.SetMembershipService(membershipService)
450+
projectService.SetMembershipService(membershipService)
449451

450452
orgKycRepository := postgres.NewOrgKycRepository(dbc)
451453
orgKycService := kyc.NewService(orgKycRepository)

‎core/aggregates/orgpats/mocks/project_service.go‎

Lines changed: 20 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/aggregates/orgpats/service.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Repository interface {
1717
}
1818

1919
type ProjectService interface {
20-
ListByUser(ctx context.Context, principal authenticate.Principal, flt project.Filter) ([]project.Project, error)
20+
List(ctx context.Context, flt project.Filter) ([]project.Project, error)
2121
}
2222

2323
type Service struct {
@@ -80,8 +80,9 @@ func (s *Service) Search(ctx context.Context, orgID string, query *rql.Query) (O
8080
return result, nil
8181
}
8282

83-
// resolveAllProjectsScope populates ResourceIDs for all-projects scopes by calling SpiceDB.
84-
// Groups PATs by user_id to minimize SpiceDB calls.
83+
// resolveAllProjectsScope populates ResourceIDs for all-projects scopes by
84+
// listing projects the underlying user can see via membership. Groups PATs by
85+
// user_id to minimize project-service calls.
8586
func (s *Service) resolveAllProjectsScope(ctx context.Context, orgID string, pats []AggregatedPAT) error {
8687
// Collect users that have all-projects scopes
8788
type allProjectsRef struct {
@@ -108,7 +109,7 @@ func (s *Service) resolveAllProjectsScope(ctx context.Context, orgID string, pat
108109
ID: userID,
109110
Type: schema.UserPrincipal,
110111
}
111-
projects, err := s.projectService.ListByUser(ctx, principal, project.Filter{OrgID: orgID})
112+
projects, err := s.projectService.List(ctx, project.Filter{Principal: &principal, OrgID: orgID})
112113
if err != nil {
113114
return err
114115
}

‎core/aggregates/orgpats/service_test.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,15 @@ func TestService_Search(t *testing.T) {
9090
}
9191

9292
repo.EXPECT().Search(mock.Anything, orgID, query).Return(repoResult, nil)
93-
projSvc.EXPECT().ListByUser(mock.Anything, mock.Anything, mock.Anything).
94-
Return([]project.Project{{ID: "proj-1"}, {ID: "proj-2"}}, nil).Maybe()
93+
projSvc.EXPECT().List(mock.Anything, mock.MatchedBy(func(f project.Filter) bool {
94+
return f.OrgID == orgID && f.Principal != nil && f.Principal.ID == "user-1" && f.Principal.Type == schema.UserPrincipal
95+
})).Return([]project.Project{{ID: "proj-1"}, {ID: "proj-2"}}, nil).Once()
9596

9697
svc := orgpats.NewService(repo, projSvc)
9798
result, err := svc.Search(ctx, orgID, query)
9899
assert.NoError(t, err)
99100
assert.Len(t, result.PATs, 1)
100-
// After resolution, the all-projects scope should have project IDs
101-
if len(result.PATs[0].Scopes[0].ResourceIDs) > 0 {
102-
assert.Contains(t, result.PATs[0].Scopes[0].ResourceIDs, "proj-1")
103-
}
101+
assert.ElementsMatch(t, []string{"proj-1", "proj-2"}, result.PATs[0].Scopes[0].ResourceIDs)
104102
})
105103

106104
t.Run("skips resolution when no all-projects scopes", func(t *testing.T) {
@@ -127,7 +125,7 @@ func TestService_Search(t *testing.T) {
127125
}
128126

129127
repo.EXPECT().Search(mock.Anything, orgID, query).Return(repoResult, nil)
130-
// ProjectService.ListByUser should NOT be called
128+
// ProjectService.List should NOT be called
131129
svc := orgpats.NewService(repo, projSvc)
132130
result, err := svc.Search(ctx, orgID, query)
133131
assert.NoError(t, err)

‎core/membership/service.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,12 @@ func (s *Service) ListGroupsByPrincipal(ctx context.Context, principal authentic
15751575
return s.listResourcesForPrincipal(ctx, subjectID, subjectType, schema.GroupNamespace, ResourceFilter{OrgID: orgID})
15761576
}
15771577

1578+
// ListProjectsByPrincipal Shim for the project package (project → membership
1579+
// would cycle). Delegates to ListResourcesByPrincipal so PAT scope is intersected.
1580+
func (s *Service) ListProjectsByPrincipal(ctx context.Context, principal authenticate.Principal, orgID string, nonInherited bool) ([]string, error) {
1581+
return s.ListResourcesByPrincipal(ctx, principal, schema.ProjectNamespace, ResourceFilter{OrgID: orgID, NonInherited: nonInherited})
1582+
}
1583+
15781584
// ListResourcesByPrincipal returns the resource IDs of the given type on which
15791585
// the principal has at least one policy. Reads Postgres policies — no SpiceDB.
15801586
// With a PAT, runs the algorithm twice (user, then PAT-as-principal) and

‎core/membership/service_test.go‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,3 +2393,124 @@ func TestService_ListGroupsByPrincipal(t *testing.T) {
23932393
})
23942394
}
23952395
}
2396+
2397+
func TestService_ListProjectsByPrincipal(t *testing.T) {
2398+
ctx := context.Background()
2399+
2400+
userID := uuid.New().String()
2401+
patID := uuid.New().String()
2402+
orgA := uuid.New().String()
2403+
projDirect := uuid.New().String()
2404+
projPATScope := uuid.New().String()
2405+
2406+
t.Run("user principal with NonInherited=true skips org-inheritance branch", func(t *testing.T) {
2407+
mp := mocks.NewPolicyService(t)
2408+
// Direct project policies fetch.
2409+
mp.EXPECT().List(ctx, policy.Filter{
2410+
PrincipalID: userID,
2411+
PrincipalType: schema.UserPrincipal,
2412+
ResourceType: schema.ProjectNamespace,
2413+
RolePermissions: schema.ProjectDirectVisibilityPerms,
2414+
}).Return([]policy.Policy{{ResourceID: projDirect}}, nil)
2415+
// Group expansion: principal has no groups (NonInherited=true on inner call).
2416+
mp.EXPECT().List(ctx, policy.Filter{
2417+
PrincipalID: userID,
2418+
PrincipalType: schema.UserPrincipal,
2419+
ResourceType: schema.GroupNamespace,
2420+
}).Return([]policy.Policy{}, nil)
2421+
// NO org-inheritance fetch must happen — that's the NonInherited contract.
2422+
2423+
svc := membership.NewService(
2424+
slog.New(slog.NewTextHandler(io.Discard, nil)),
2425+
mp,
2426+
mocks.NewRelationService(t),
2427+
mocks.NewRoleService(t),
2428+
mocks.NewOrgService(t),
2429+
mocks.NewUserService(t),
2430+
mocks.NewProjectService(t),
2431+
mocks.NewGroupService(t),
2432+
mocks.NewServiceuserService(t),
2433+
mocks.NewAuditRecordRepository(t),
2434+
)
2435+
2436+
got, err := svc.ListProjectsByPrincipal(
2437+
ctx,
2438+
authenticate.Principal{ID: userID, Type: schema.UserPrincipal},
2439+
"",
2440+
true,
2441+
)
2442+
assert.NoError(t, err)
2443+
assert.ElementsMatch(t, []string{projDirect}, got)
2444+
})
2445+
2446+
t.Run("PAT principal — runs both user-side and PAT-side queries and intersects (unlike groups)", func(t *testing.T) {
2447+
mp := mocks.NewPolicyService(t)
2448+
// User-side: direct project policies + (no groups) + org-inheritance branch.
2449+
mp.EXPECT().List(ctx, policy.Filter{
2450+
PrincipalID: userID,
2451+
PrincipalType: schema.UserPrincipal,
2452+
ResourceType: schema.ProjectNamespace,
2453+
RolePermissions: schema.ProjectDirectVisibilityPerms,
2454+
}).Return([]policy.Policy{
2455+
{ResourceID: projDirect},
2456+
{ResourceID: projPATScope},
2457+
}, nil)
2458+
mp.EXPECT().List(ctx, policy.Filter{
2459+
PrincipalID: userID,
2460+
PrincipalType: schema.UserPrincipal,
2461+
ResourceType: schema.GroupNamespace,
2462+
}).Return([]policy.Policy{}, nil)
2463+
mp.EXPECT().List(ctx, policy.Filter{
2464+
PrincipalID: userID,
2465+
PrincipalType: schema.UserPrincipal,
2466+
ResourceType: schema.OrganizationNamespace,
2467+
RolePermissions: schema.OrganizationProjectInheritPerms,
2468+
}).Return([]policy.Policy{}, nil)
2469+
2470+
// PAT-side: same fanout under PAT principal type — PAT only scopes projPATScope.
2471+
mp.EXPECT().List(ctx, policy.Filter{
2472+
PrincipalID: patID,
2473+
PrincipalType: schema.PATPrincipal,
2474+
ResourceType: schema.ProjectNamespace,
2475+
RolePermissions: schema.ProjectDirectVisibilityPerms,
2476+
}).Return([]policy.Policy{{ResourceID: projPATScope}}, nil)
2477+
mp.EXPECT().List(ctx, policy.Filter{
2478+
PrincipalID: patID,
2479+
PrincipalType: schema.PATPrincipal,
2480+
ResourceType: schema.GroupNamespace,
2481+
}).Return([]policy.Policy{}, nil)
2482+
mp.EXPECT().List(ctx, policy.Filter{
2483+
PrincipalID: patID,
2484+
PrincipalType: schema.PATPrincipal,
2485+
ResourceType: schema.OrganizationNamespace,
2486+
RolePermissions: schema.OrganizationProjectInheritPerms,
2487+
}).Return([]policy.Policy{}, nil)
2488+
2489+
svc := membership.NewService(
2490+
slog.New(slog.NewTextHandler(io.Discard, nil)),
2491+
mp,
2492+
mocks.NewRelationService(t),
2493+
mocks.NewRoleService(t),
2494+
mocks.NewOrgService(t),
2495+
mocks.NewUserService(t),
2496+
mocks.NewProjectService(t),
2497+
mocks.NewGroupService(t),
2498+
mocks.NewServiceuserService(t),
2499+
mocks.NewAuditRecordRepository(t),
2500+
)
2501+
2502+
got, err := svc.ListProjectsByPrincipal(
2503+
ctx,
2504+
authenticate.Principal{
2505+
ID: userID,
2506+
Type: schema.UserPrincipal,
2507+
PAT: &pat.PAT{ID: patID, UserID: userID, OrgID: orgA},
2508+
},
2509+
"",
2510+
false,
2511+
)
2512+
assert.NoError(t, err)
2513+
// PAT narrows: user sees [direct, patScope]; PAT sees [patScope]; intersect → [patScope].
2514+
assert.ElementsMatch(t, []string{projPATScope}, got)
2515+
})
2516+
}

‎core/project/filter.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package project
22

3-
import "github.com/raystack/frontier/pkg/pagination"
3+
import (
4+
"github.com/raystack/frontier/core/authenticate"
5+
"github.com/raystack/frontier/pkg/pagination"
6+
)
47

58
type Filter struct {
69
OrgID string
@@ -17,4 +20,10 @@ type Filter struct {
1720
// are set, projects must satisfy both (intersection) — typically yields
1821
// no rows unless OrgID is one of OrgIDs.
1922
OrgIDs []string
23+
24+
// Principal narrows results to projects on which the principal has a
25+
// policy (direct, via group membership, or org-inheritance unless
26+
// NonInherited is set). When combined with ProjectIDs the two are
27+
// intersected. Resolved by membership.Service.
28+
Principal *authenticate.Principal
2029
}

0 commit comments

Comments
 (0)