@@ -5,89 +5,88 @@ import (
5
5
6
6
"entgo.io/ent"
7
7
8
- "github.com/rs/zerolog"
9
8
"github.com/theopenlane/iam/auth"
10
- "github.com/theopenlane/iam/fgax"
11
9
12
10
"github.com/theopenlane/core/internal/ent/generated"
13
11
"github.com/theopenlane/core/internal/ent/generated/intercept"
14
12
"github.com/theopenlane/core/internal/ent/generated/predicate"
15
13
"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"
16
16
"github.com/theopenlane/core/internal/ent/privacy/rule"
17
- "github.com/theopenlane/core/internal/ent/privacy/utils"
18
17
)
19
18
20
19
// TraverseStandard only returns public standards and standards owned by the organization
21
20
func TraverseStandard () ent.Interceptor {
22
21
return intercept .TraverseStandard (func (ctx context.Context , q * generated.StandardQuery ) error {
23
- zerolog .Ctx (ctx ).Debug ().Msg ("traversing standard" )
24
-
25
22
anon , isAnon := auth .AnonymousTrustCenterUserFromContext (ctx )
26
23
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
55
24
q .Where (
56
- standard .Or (
57
- standard . And (
58
- systemStandardPredicates ... ,
25
+ standard .HasTrustCenterCompliancesWith (
26
+ trustcentercompliance . HasTrustCenterWith (
27
+ trustcenter . OwnerID ( anon . OrganizationID ) ,
59
28
),
60
- standard .OwnerIDIn (orgIDs ... ),
61
29
),
62
30
)
31
+ return nil
63
32
}
64
33
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
+ }
76
38
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
+ }
78
43
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 )
86
45
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 ))
88
52
}
89
- standardIDs = append (standardIDs , entity .Identifier )
90
- }
91
53
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
+ })
93
66
}
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
+ // }
0 commit comments