generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.go
255 lines (241 loc) · 6.94 KB
/
user.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package schema
import (
"net/mail"
"net/url"
"strings"
"time"
"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/datum/internal/ent/enums"
"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 (
urlMaxLen = 255
nameMaxLen = 64
)
// User holds the schema definition for the User entity.
type User struct {
ent.Schema
}
// Mixin of the User
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
emixin.AuditMixin{},
mixin.SoftDeleteMixin{},
emixin.IDMixin{},
emixin.TagMixin{},
}
}
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
// NOTE: the created_at and updated_at fields are automatically created by the AuditMixin, you do not need to re-declare / add them in these fields
field.String("email").
Validate(func(email string) error {
_, err := mail.ParseAddress(email)
return err
}),
field.String("first_name").
Optional().
MaxLen(nameMaxLen).
Annotations(
entgql.OrderField("first_name"),
),
field.String("last_name").
Optional().
MaxLen(nameMaxLen).
Annotations(
entgql.OrderField("last_name"),
),
field.String("display_name").
Comment("The user's displayed 'friendly' name").
MaxLen(nameMaxLen).
NotEmpty().
Annotations(
entgql.OrderField("display_name"),
).
Validate(
func(s string) error {
if strings.Contains(s, " ") {
return ErrContainsSpaces
}
return nil
},
),
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.String("avatar_local_file").
Comment("The user's local avatar file").
MaxLen(urlMaxLen).
Optional().
Nillable(),
field.Time("avatar_updated_at").
Comment("The time the user's (local) avatar was last updated").
UpdateDefault(time.Now).
Optional().
Nillable(),
field.Time("last_seen").
Comment("the time the user was last seen").
UpdateDefault(time.Now).
Annotations(entoas.Annotation{ReadOnly: true}).
Optional().
Nillable(),
field.String("password").
Comment("user password hash").
Nillable().
Sensitive().
Annotations(entoas.Skip(true)).
Optional(),
field.String("sub").
Comment("the Subject of the user JWT").
Unique().
Annotations(entoas.Skip(true)).
Optional(),
field.Enum("auth_provider").
Comment("auth provider used to register the account").
GoType(enums.AuthProvider("")).
Default(string(enums.AuthProviderCredentials)),
field.Enum("role").
Comment("the user's role").
GoType(enums.Role("")).
Default(string(enums.RoleUser)).
Optional(),
}
}
// Indexes of the User
func (User) Indexes() []ent.Index {
return []ent.Index{
index.Fields("id").
Unique(), // enforce globally unique ids
index.Fields("email", "auth_provider").
Unique().Annotations(entsql.IndexWhere("deleted_at is NULL")),
}
}
// Edges of the User
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.To("personal_access_tokens", PersonalAccessToken.Type).
Annotations(entx.CascadeAnnotationField("Owner")),
edge.To("tfa_settings", TFASetting.Type).
Annotations(entx.CascadeAnnotationField("Owner")),
edge.To("setting", UserSetting.Type).
Required().
Unique().
Annotations(
entx.CascadeAnnotationField("User"),
entoas.Skip(true),
),
edge.To("email_verification_tokens", EmailVerificationToken.Type).
Annotations(
entx.CascadeAnnotationField("Owner"),
entoas.Skip(true),
entoas.CreateOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.ReadOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.UpdateOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.DeleteOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.ListOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
),
edge.To("password_reset_tokens", PasswordResetToken.Type).
Annotations(
entx.CascadeAnnotationField("Owner"),
entoas.Skip(true),
entoas.CreateOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.ReadOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.UpdateOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.DeleteOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
entoas.ListOperation(entoas.OperationPolicy(entoas.PolicyExclude)),
),
edge.To("groups", Group.Type).
Through("group_memberships", GroupMembership.Type),
edge.To("organizations", Organization.Type).
Through("org_memberships", OrgMembership.Type),
edge.To("webauthn", Webauthn.Type).
Annotations(entx.CascadeAnnotationField("Owner")),
edge.To("files", File.Type),
edge.To("events", Event.Type),
edge.To("features", Feature.Type),
}
}
// Annotations of the User
func (User) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.QueryField(),
entgql.RelayConnection(),
entgql.Mutations(entgql.MutationCreate(), (entgql.MutationUpdate())),
// Delete users from groups and orgs when the user is deleted
entx.CascadeThroughAnnotationField(
[]entx.ThroughCleanup{
{
Field: "User",
Through: "OrgMembership",
},
{
Field: "User",
Through: "GroupMembership",
},
},
),
}
}
// Policy of the User
func (User) Policy() ent.Policy {
return privacy.Policy{
Mutation: privacy.MutationPolicy{
privacy.OnMutationOperation(
privacy.MutationPolicy{
rule.AllowIfContextHasPrivacyTokenOfType(&token.SignUpToken{}),
rule.AllowIfContextHasPrivacyTokenOfType(&token.OrgInviteToken{}),
rule.AllowIfContextHasPrivacyTokenOfType(&token.OauthTooToken{}),
rule.AllowIfSelf(),
privacy.AlwaysDenyRule(),
},
// the user hook has update operations on user create so we need to allow email token sign up for update
// operations as well
ent.OpCreate|ent.OpUpdateOne,
),
privacy.OnMutationOperation(
privacy.MutationPolicy{
rule.AllowIfSelf(),
privacy.AlwaysDenyRule(),
},
ent.OpUpdateOne|ent.OpUpdate|ent.OpDeleteOne|ent.OpDelete,
),
},
Query: privacy.QueryPolicy{
// Privacy will be always allow, but interceptors will filter the queries
privacy.AlwaysAllowRule(),
},
}
}
func (User) Hooks() []ent.Hook {
return []ent.Hook{
hooks.HookUser(),
hooks.HookDeleteUser(),
}
}
// Interceptors of the User.
func (d User) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
interceptors.InterceptorUser(),
}
}