Skip to content

Commit 737ebb2

Browse files
golanglemonadeacookin
authored andcommitted
rebase, fix moved cilent, update interceptor
Signed-off-by: Sarah Funkhouser <[email protected]>
1 parent 56e4b08 commit 737ebb2

File tree

10 files changed

+890
-155
lines changed

10 files changed

+890
-155
lines changed

internal/ent/generated/accessmap.go

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/ent/generated/gql_collection.go

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

internal/ent/generated/runtime/runtime.go

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

internal/ent/generated/standard/standard.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/ent/interceptors/standard.go

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,89 +5,88 @@ import (
55

66
"entgo.io/ent"
77

8-
"github.com/rs/zerolog"
98
"github.com/theopenlane/iam/auth"
10-
"github.com/theopenlane/iam/fgax"
119

1210
"github.com/theopenlane/core/internal/ent/generated"
1311
"github.com/theopenlane/core/internal/ent/generated/intercept"
1412
"github.com/theopenlane/core/internal/ent/generated/predicate"
1513
"github.com/theopenlane/core/internal/ent/generated/standard"
14+
"github.com/theopenlane/core/internal/ent/generated/trustcenter"
15+
"github.com/theopenlane/core/internal/ent/generated/trustcentercompliance"
1616
"github.com/theopenlane/core/internal/ent/privacy/rule"
17-
"github.com/theopenlane/core/internal/ent/privacy/utils"
1817
)
1918

2019
// TraverseStandard only returns public standards and standards owned by the organization
2120
func TraverseStandard() ent.Interceptor {
2221
return intercept.TraverseStandard(func(ctx context.Context, q *generated.StandardQuery) error {
23-
zerolog.Ctx(ctx).Debug().Msg("traversing standard")
24-
2522
anon, isAnon := auth.AnonymousTrustCenterUserFromContext(ctx)
2623
if isAnon {
27-
standardIDs, err := getAllowedTrustCenterStandards(ctx, anon.TrustCenterID)
28-
if err != nil {
29-
return err
30-
}
31-
32-
q.Where(standard.IDIn(standardIDs...))
33-
} else {
34-
orgIDs, err := auth.GetOrganizationIDsFromContext(ctx)
35-
if err != nil {
36-
return err
37-
}
38-
39-
systemStandardPredicates := []predicate.Standard{
40-
standard.OwnerIDIsNil(),
41-
standard.SystemOwned(true),
42-
}
43-
44-
admin, err := rule.CheckIsSystemAdminWithContext(ctx)
45-
if err != nil {
46-
return err
47-
}
48-
49-
if !admin {
50-
// if the user is a not-system admin, restrict to only public standards
51-
systemStandardPredicates = append(systemStandardPredicates, standard.IsPublic(true))
52-
}
53-
54-
// filter to return system owned standards and standards owned by the organization
5524
q.Where(
56-
standard.Or(
57-
standard.And(
58-
systemStandardPredicates...,
25+
standard.HasTrustCenterCompliancesWith(
26+
trustcentercompliance.HasTrustCenterWith(
27+
trustcenter.OwnerID(anon.OrganizationID),
5928
),
60-
standard.OwnerIDIn(orgIDs...),
6129
),
6230
)
31+
return nil
6332
}
6433

65-
return nil
66-
})
67-
}
68-
69-
func getAllowedTrustCenterStandards(ctx context.Context, tcID string) ([]string, error) {
70-
req := fgax.ListRequest{
71-
SubjectID: tcID,
72-
SubjectType: "trust_center",
73-
ObjectType: "standard",
74-
Relation: "associated_with",
75-
}
34+
orgIDs, err := auth.GetOrganizationIDsFromContext(ctx)
35+
if err != nil {
36+
return err
37+
}
7638

77-
zerolog.Ctx(ctx).Debug().Interface("req", req).Msg("getting authorized object ids")
39+
systemStandardPredicates := []predicate.Standard{
40+
standard.OwnerIDIsNil(),
41+
standard.SystemOwned(true),
42+
}
7843

79-
resp, err := utils.AuthzClientFromContext(ctx).ListObjectsRequest(ctx, req)
80-
if err != nil {
81-
return []string{}, err
82-
}
83-
standardIDs := []string{}
84-
for _, obj := range resp.Objects {
85-
entity, err := fgax.ParseEntity(obj)
44+
admin, err := rule.CheckIsSystemAdminWithContext(ctx)
8645
if err != nil {
87-
return []string{}, nil
46+
return err
47+
}
48+
49+
if !admin {
50+
// if the user is a not-system admin, restrict to only public standards
51+
systemStandardPredicates = append(systemStandardPredicates, standard.IsPublic(true))
8852
}
89-
standardIDs = append(standardIDs, entity.Identifier)
90-
}
9153

92-
return standardIDs, nil
54+
// filter to return system owned standards and standards owned by the organization
55+
q.Where(
56+
standard.Or(
57+
standard.And(
58+
systemStandardPredicates...,
59+
),
60+
standard.OwnerIDIn(orgIDs...),
61+
),
62+
)
63+
64+
return nil
65+
})
9366
}
67+
68+
// func getAllowedTrustCenterStandards(ctx context.Context, tcID string) ([]string, error) {
69+
// req := fgax.ListRequest{
70+
// SubjectID: tcID,
71+
// SubjectType: "trust_center",
72+
// ObjectType: "standard",
73+
// Relation: "associated_with",
74+
// }
75+
76+
// zerolog.Ctx(ctx).Debug().Interface("req", req).Msg("getting authorized object ids")
77+
78+
// resp, err := utils.AuthzClientFromContext(ctx).ListObjectsRequest(ctx, req)
79+
// if err != nil {
80+
// return []string{}, err
81+
// }
82+
// standardIDs := []string{}
83+
// for _, obj := range resp.Objects {
84+
// entity, err := fgax.ParseEntity(obj)
85+
// if err != nil {
86+
// return []string{}, nil
87+
// }
88+
// standardIDs = append(standardIDs, entity.Identifier)
89+
// }
90+
91+
// return standardIDs, nil
92+
// }

internal/ent/schema/standard.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/gertd/go-pluralize"
99
"github.com/theopenlane/entx"
1010

11+
"github.com/theopenlane/core/internal/ent/generated/hook"
1112
"github.com/theopenlane/core/internal/ent/hooks"
1213
"github.com/theopenlane/core/internal/ent/interceptors"
1314
"github.com/theopenlane/core/internal/ent/mixin"
@@ -155,6 +156,10 @@ func (Standard) Hooks() []ent.Hook {
155156
hooks.HookStandardPublicAccessTuples(),
156157
hooks.HookStandardCreate(),
157158
hooks.HookStandardDelete(),
159+
hook.On(
160+
hooks.OrgOwnedTuplesHook(),
161+
ent.OpCreate,
162+
),
158163
}
159164
}
160165

internal/ent/schema/trustcenter_compliance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/theopenlane/core/internal/ent/hooks"
1717
"github.com/theopenlane/core/internal/ent/interceptors"
1818
"github.com/theopenlane/core/internal/ent/privacy/policy"
19+
"github.com/theopenlane/entx/accessmap"
1920
"github.com/theopenlane/iam/entfga"
2021
)
2122

@@ -75,6 +76,9 @@ func (t TrustCenterCompliance) Edges() []ent.Edge {
7576
edgeSchema: Standard{},
7677
field: "standard_id",
7778
required: true,
79+
annotations: []schema.Annotation{
80+
accessmap.EdgeViewCheck(Standard{}.Name()),
81+
},
7882
}),
7983
}
8084
}

0 commit comments

Comments
 (0)