generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
organization.go
221 lines (207 loc) · 6.26 KB
/
organization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package schema
import (
"context"
"net/url"
"entgo.io/contrib/entgql"
"entgo.io/contrib/entoas"
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"github.com/datumforge/entx"
emixin "github.com/datumforge/entx/mixin"
"github.com/datumforge/fgax/entfga"
"github.com/ogen-go/ogen"
"github.com/datumforge/datum/internal/ent/generated"
"github.com/datumforge/datum/internal/ent/generated/privacy"
"github.com/datumforge/datum/internal/ent/hooks"
"github.com/datumforge/datum/internal/ent/interceptors"
"github.com/datumforge/datum/internal/ent/mixin"
"github.com/datumforge/datum/internal/ent/privacy/rule"
"github.com/datumforge/datum/internal/ent/privacy/token"
)
const (
orgNameMaxLen = 160
)
// Organization holds the schema definition for the Organization entity - organizations are the top level tenancy construct in the system
type Organization struct {
ent.Schema
}
// Fields of the Organization
func (Organization) Fields() []ent.Field {
return []ent.Field{
field.String("name").
Comment("the name of the organization").
MaxLen(orgNameMaxLen).
NotEmpty().
Annotations(
entgql.OrderField("name"),
entgql.Skip(entgql.SkipWhereInput),
),
field.String("display_name").
Comment("The organization's displayed 'friendly' name").
MaxLen(nameMaxLen).
Default("").
Annotations(
entgql.OrderField("display_name"),
),
field.String("description").
Comment("An optional description of the organization").
Optional().
Annotations(
entgql.Skip(entgql.SkipWhereInput),
),
field.String("parent_organization_id").Optional().Immutable().
Comment("The ID of the parent organization for the organization.").
Annotations(
entgql.Type("ID"),
entgql.Skip(entgql.SkipMutationUpdateInput, entgql.SkipType),
entoas.Schema(ogen.String()),
),
field.Bool("personal_org").
Comment("orgs directly associated with a user").
Optional().
Default(false).
Immutable(),
field.String("avatar_remote_url").
Comment("URL of the user's remote avatar").
MaxLen(urlMaxLen).
Validate(func(s string) error {
_, err := url.Parse(s)
return err
}).
Optional().
Nillable(),
field.Bool("dedicated_db").
Comment("Whether the organization has a dedicated database").
Default(false). // default to shared db
// TODO: https://github.com/datumforge/datum/issues/734
// update this once feature functionality is enabled
// Annotations(
// entgql.Skip(),
// ),
Annotations(
entgql.Skip(entgql.SkipWhereInput, entgql.SkipMutationUpdateInput, entgql.SkipOrderField),
),
}
}
// Edges of the Organization
func (Organization) Edges() []ent.Edge {
return []ent.Edge{
edge.To("children", Organization.Type).
Annotations(
entgql.RelayConnection(),
entgql.Skip(entgql.SkipMutationCreateInput, entgql.SkipMutationUpdateInput),
).
From("parent").
Field("parent_organization_id").
Immutable().
Unique().
Annotations(
entx.CascadeAnnotationField("Child"),
),
edge.To("groups", Group.Type).
Annotations(
entx.CascadeAnnotationField("Owner"),
),
edge.To("templates", Template.Type).
Annotations(
entx.CascadeAnnotationField("Owner"),
),
edge.To("integrations", Integration.Type).
Annotations(
entx.CascadeAnnotationField("Owner"),
),
edge.To("setting", OrganizationSetting.Type).
Unique().
Annotations(
entx.CascadeAnnotationField("Organization"),
entoas.Skip(true),
),
edge.To("entitlements", Entitlement.Type),
edge.To("personal_access_tokens", PersonalAccessToken.Type),
edge.To("api_tokens", APIToken.Type),
edge.To("oauthprovider", OauthProvider.Type),
edge.From("users", User.Type).
Ref("organizations").
Through("members", OrgMembership.Type),
edge.To("invites", Invite.Type).
Annotations(entx.CascadeAnnotationField("Owner")),
edge.To("subscribers", Subscriber.Type).
Annotations(entx.CascadeAnnotationField("Owner")),
edge.To("webhooks", Webhook.Type).
Annotations(entx.CascadeAnnotationField("Owner")),
edge.To("events", Event.Type),
edge.To("secrets", Hush.Type),
edge.To("features", Feature.Type),
edge.To("files", File.Type),
}
}
func (Organization) Indexes() []ent.Index {
return []ent.Index{
// names should be unique, but ignore deleted names
index.Fields("name").
Unique().Annotations(entsql.IndexWhere("deleted_at is NULL")),
}
}
// Annotations of the Organization
func (Organization) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.QueryField(),
entgql.RelayConnection(),
entgql.Mutations(entgql.MutationCreate(), (entgql.MutationUpdate())),
// Delete org members when orgs are deleted
entx.CascadeThroughAnnotationField(
[]entx.ThroughCleanup{
{
Field: "Organization",
Through: "OrgMembership",
},
},
),
entfga.Annotations{
ObjectType: "organization",
IncludeHooks: false,
},
}
}
// Mixin of the Organization
func (Organization) Mixin() []ent.Mixin {
return []ent.Mixin{
emixin.AuditMixin{},
emixin.IDMixin{},
emixin.TagMixin{},
mixin.SoftDeleteMixin{},
}
}
// Policy defines the privacy policy of the Organization.
func (Organization) Policy() ent.Policy {
return privacy.Policy{
Mutation: privacy.MutationPolicy{
rule.HasOrgMutationAccess(), // Requires edit for Update, and delete for Delete mutations
privacy.AlwaysAllowRule(), // Allow all other users (e.g. a user with a JWT should be able to create a new org)
},
Query: privacy.QueryPolicy{
privacy.OrganizationQueryRuleFunc(func(ctx context.Context, q *generated.OrganizationQuery) error {
return q.CheckAccess(ctx)
}),
rule.AllowIfContextHasPrivacyTokenOfType(&token.OrgInviteToken{}), // Allow invite tokens to query the org ID they are invited to
rule.AllowIfContextHasPrivacyTokenOfType(&token.SignUpToken{}), // Allow sign-up tokens to query the org ID they are subscribing to
privacy.AlwaysDenyRule(), // Deny all other users
},
}
}
// Interceptors of the Organization
func (Organization) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
interceptors.InterceptorOrganization(),
}
}
// Hooks of the Organization
func (Organization) Hooks() []ent.Hook {
return []ent.Hook{
hooks.HookOrganization(),
}
}