Skip to content

Commit c3a672d

Browse files
committed
tc stuff
1 parent e1cb520 commit c3a672d

File tree

4 files changed

+311
-0
lines changed

4 files changed

+311
-0
lines changed

internal/ent/hooks/trustcenter.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package hooks
2+
3+
// import (
4+
// "context"
5+
// "fmt"
6+
// "regexp"
7+
// "strings"
8+
9+
// "entgo.io/ent"
10+
11+
// "github.com/theopenlane/core/internal/ent/generated"
12+
// "github.com/theopenlane/core/internal/ent/generated/hook"
13+
// )
14+
15+
// // ErrMissingOrgID is returned when the organization ID is missing from the trust center mutation
16+
// var ErrMissingOrgID = fmt.Errorf("missing organization id from trust center mutation")
17+
18+
// // HookTrustCenter runs on trust center create mutations
19+
// func HookTrustCenter() ent.Hook {
20+
// return hook.On(func(next ent.Mutator) ent.Mutator {
21+
// return hook.TrustCenterFunc(func(ctx context.Context, m *generated.TrustCenterMutation) (generated.Value, error) {
22+
// orgID, ok := m.OwnerID()
23+
// if !ok {
24+
// return nil, ErrMissingOrgID
25+
// }
26+
27+
// org, err := m.Client().Organization.Get(ctx, orgID)
28+
// if err != nil {
29+
// return nil, err
30+
// }
31+
// // Remove all spaces and non-alphanumeric characters from org.Name, then lowercase
32+
// reg := regexp.MustCompile(`[^a-zA-Z0-9]`)
33+
// cleanedName := reg.ReplaceAllString(org.Name, "")
34+
// slug := strings.ToLower(cleanedName)
35+
36+
// m.SetSlug(slug)
37+
38+
// retVal, err := next.Mutate(ctx, m)
39+
// if err != nil {
40+
// return nil, err
41+
// }
42+
43+
// trustCenter := retVal.(*generated.TrustCenter)
44+
45+
// id, err := GetObjectIDFromEntValue(retVal)
46+
// if err != nil {
47+
// return retVal, err
48+
// }
49+
50+
// setting, err := m.Client().TrustCenterSetting.Create().
51+
// SetTrustCenterID(id).
52+
// SetTitle(fmt.Sprintf("%s Trust Center", org.Name)).
53+
// SetOverview(defaultOverview).
54+
// SetOwnerID(orgID).
55+
// SetLogoURL(*org.AvatarRemoteURL).
56+
// Save(ctx)
57+
// if err != nil {
58+
// return nil, err
59+
// }
60+
// trustCenter.Edges.Setting = setting
61+
62+
// return trustCenter, nil
63+
// })
64+
// }, ent.OpCreate)
65+
// }
66+
67+
// const defaultOverview = `
68+
// # Welcome to your Trust Center
69+
70+
// This is the default overview for your trust center. You can customize this by editing the trust center settings.
71+
// `

internal/ent/schema/organization.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ func (o Organization) Edges() []ent.Edge {
358358
edgeSchema: ScheduledJobRun{},
359359
cascadeDeleteOwner: true,
360360
}),
361+
edgeToWithPagination(&edgeDefinition{
362+
fromSchema: o,
363+
edgeSchema: TrustCenter{},
364+
cascadeDeleteOwner: true,
365+
}),
361366
}
362367
}
363368

internal/ent/schema/trustcenter.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package schema
2+
3+
import (
4+
"entgo.io/contrib/entgql"
5+
"entgo.io/ent"
6+
"entgo.io/ent/dialect/entsql"
7+
"entgo.io/ent/schema"
8+
"entgo.io/ent/schema/field"
9+
"entgo.io/ent/schema/index"
10+
"github.com/gertd/go-pluralize"
11+
12+
"github.com/theopenlane/core/internal/ent/generated/privacy"
13+
"github.com/theopenlane/core/internal/ent/privacy/policy"
14+
"github.com/theopenlane/entx"
15+
)
16+
17+
const (
18+
trustCenterNameMaxLen = 160
19+
trustCenterDescriptionMaxLen = 1024
20+
trustCenterURLMaxLen = 2048
21+
)
22+
23+
// TrustCenter holds the schema definition for the TrustCenter entity
24+
type TrustCenter struct {
25+
SchemaFuncs
26+
27+
ent.Schema
28+
}
29+
30+
// SchemaTrustCenter is the name of the TrustCenter schema.
31+
const SchemaTrustCenter = "trust_center"
32+
33+
// Name returns the name of the TrustCenter schema.
34+
func (TrustCenter) Name() string {
35+
return SchemaTrustCenter
36+
}
37+
38+
// GetType returns the type of the TrustCenter schema.
39+
func (TrustCenter) GetType() any {
40+
return TrustCenter.Type
41+
}
42+
43+
// PluralName returns the plural name of the TrustCenter schema.
44+
func (TrustCenter) PluralName() string {
45+
return pluralize.NewClient().Plural(SchemaTrustCenter)
46+
}
47+
48+
// Fields of the TrustCenter
49+
func (TrustCenter) Fields() []ent.Field {
50+
return []ent.Field{
51+
field.String("slug").
52+
Comment("Slug for the trust center").
53+
MaxLen(trustCenterNameMaxLen).
54+
Annotations(
55+
entgql.Skip(entgql.SkipMutationCreateInput, entgql.SkipMutationUpdateInput),
56+
).
57+
Optional(),
58+
field.String("custom_domain_id").
59+
Comment("custom domain id for the trust center").
60+
Optional(),
61+
}
62+
}
63+
64+
// Mixin of the TrustCenter
65+
func (t TrustCenter) Mixin() []ent.Mixin {
66+
return mixinConfig{
67+
additionalMixins: []ent.Mixin{
68+
newOrgOwnedMixin(t),
69+
},
70+
}.getMixins()
71+
}
72+
73+
// Edges of the TrustCenter
74+
func (t TrustCenter) Edges() []ent.Edge {
75+
return []ent.Edge{
76+
// Add relationships to other entities as needed
77+
// Example: defaultEdgeToWithPagination(t, File{}),
78+
uniqueEdgeTo(&edgeDefinition{
79+
fromSchema: t,
80+
edgeSchema: CustomDomain{},
81+
field: "custom_domain_id",
82+
required: false,
83+
immutable: false,
84+
}),
85+
uniqueEdgeTo(&edgeDefinition{
86+
fromSchema: t,
87+
name: "setting",
88+
t: TrustCenterSetting.Type,
89+
annotations: []schema.Annotation{
90+
entx.CascadeAnnotationField("TrustCenter"),
91+
},
92+
}),
93+
}
94+
}
95+
96+
// Hooks of the TrustCenter
97+
func (TrustCenter) Hooks() []ent.Hook {
98+
return []ent.Hook{
99+
// hooks.HookTrustCenter(),
100+
}
101+
}
102+
103+
// Policy of the TrustCenter
104+
func (TrustCenter) Policy() ent.Policy {
105+
return policy.NewPolicy(
106+
policy.WithQueryRules(
107+
privacy.AlwaysAllowRule(),
108+
),
109+
policy.WithMutationRules(
110+
policy.CheckOrgWriteAccess(),
111+
),
112+
)
113+
}
114+
115+
// Indexes of the TrustCenter
116+
func (TrustCenter) Indexes() []ent.Index {
117+
return []ent.Index{
118+
index.Fields("slug").
119+
Unique().Annotations(entsql.IndexWhere("deleted_at is NULL")),
120+
index.Fields(ownerFieldName).
121+
Annotations(entsql.IndexWhere("deleted_at is NULL")),
122+
}
123+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package schema
2+
3+
import (
4+
"entgo.io/ent"
5+
"entgo.io/ent/dialect/entsql"
6+
"entgo.io/ent/schema"
7+
"entgo.io/ent/schema/field"
8+
"entgo.io/ent/schema/index"
9+
"github.com/gertd/go-pluralize"
10+
"github.com/theopenlane/core/internal/ent/generated/privacy"
11+
"github.com/theopenlane/core/internal/ent/privacy/policy"
12+
"github.com/theopenlane/iam/entfga"
13+
)
14+
15+
// TrustCenterSetting holds the schema definition for the TrustCenterSetting entity
16+
type TrustCenterSetting struct {
17+
SchemaFuncs
18+
19+
ent.Schema
20+
}
21+
22+
const SchemaTrustCenterSetting = "trust_center_setting"
23+
24+
func (TrustCenterSetting) Name() string {
25+
return SchemaTrustCenterSetting
26+
}
27+
func (TrustCenterSetting) GetType() any {
28+
return TrustCenterSetting.Type
29+
}
30+
31+
func (TrustCenterSetting) PluralName() string {
32+
return pluralize.NewClient().Plural(SchemaTrustCenterSetting)
33+
}
34+
35+
// Fields of the TrustCenterSetting
36+
func (TrustCenterSetting) Fields() []ent.Field {
37+
return []ent.Field{
38+
field.String("trust_center_id").
39+
Comment("the ID of the trust center the settings belong to").
40+
// Actually required, but i have to make codegen happy
41+
Optional(),
42+
// field.String("title").
43+
// Comment("title of the trust center").
44+
// MaxLen(trustCenterNameMaxLen).
45+
// Optional(),
46+
// field.Text("overview").
47+
// Comment("overview of the trust center").
48+
// MaxLen(trustCenterDescriptionMaxLen).
49+
// Optional(),
50+
// field.String("logo_url").
51+
// Comment("logo url for the trust center").
52+
// MaxLen(trustCenterURLMaxLen).
53+
// Validate(validator.ValidateURL()).
54+
// Optional(),
55+
// field.String("favicon_url").
56+
// Comment("favicon url for the trust center").
57+
// MaxLen(trustCenterURLMaxLen).
58+
// Validate(validator.ValidateURL()).
59+
// Optional(),
60+
// field.String("primary_color").
61+
// Comment("primary color for the trust center").
62+
// Optional(),
63+
}
64+
}
65+
66+
// Mixin of the TrustCenterSetting
67+
func (t TrustCenterSetting) Mixin() []ent.Mixin {
68+
return mixinConfig{
69+
excludeTags: true,
70+
}.getMixins()
71+
}
72+
73+
// Edges of the TrustCenterSetting
74+
func (t TrustCenterSetting) Edges() []ent.Edge {
75+
return []ent.Edge{
76+
uniqueEdgeFrom(&edgeDefinition{
77+
fromSchema: t,
78+
edgeSchema: TrustCenter{},
79+
field: "trust_center_id",
80+
ref: "setting",
81+
}),
82+
}
83+
}
84+
85+
// Hooks of the TrustCenterSetting
86+
func (TrustCenterSetting) Hooks() []ent.Hook {
87+
return []ent.Hook{}
88+
}
89+
90+
func (TrustCenterSetting) Policy() ent.Policy {
91+
return policy.NewPolicy(
92+
policy.WithQueryRules(
93+
privacy.AlwaysAllowRule(),
94+
),
95+
// policy.WithMutationRules(
96+
// policy.CheckOrgWriteAccess(),
97+
// ),
98+
)
99+
}
100+
101+
func (TrustCenterSetting) Indexes() []ent.Index {
102+
return []ent.Index{
103+
index.Fields("trust_center_id").
104+
Unique().Annotations(entsql.IndexWhere("deleted_at is NULL")),
105+
}
106+
}
107+
108+
func (TrustCenterSetting) Annotations() []schema.Annotation {
109+
return []schema.Annotation{
110+
entfga.SettingsChecks("trust_center"),
111+
}
112+
}

0 commit comments

Comments
 (0)