/
user.go
89 lines (82 loc) · 1.99 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
package schema
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"entgo.io/contrib/entgql"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
// User holds the schema definition for the User entity.
type User struct {
ent.Schema
}
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{
field.String("name").
MinLen(3).
MaxLen(25).
Comment("The name displayed for the user"),
field.String("oauth_id").
Sensitive().
Unique().
Immutable().
Comment("OAuth Subject ID of the user"),
field.String("photo_url").
Comment("URL to the user's profile photo.").
SchemaType(map[string]string{
dialect.MySQL: "MEDIUMTEXT", // Override MySQL, improve length maximum
}),
field.String("session_token").
DefaultFunc(newSecureToken).
Sensitive().
MaxLen(200).
Annotations(
entgql.Skip(),
).
Comment("The session token currently authenticating the user"),
field.String("access_token").
DefaultFunc(newSecureToken).
Sensitive().
MaxLen(200).
Annotations(
entgql.Skip(),
).
Comment("The token used by applications to authenticate as the user"),
field.Bool("is_activated").
Default(false).
Comment("True if the user is active and able to authenticate"),
field.Bool("is_admin").
Default(false).
Comment("True if the user is an Admin"),
}
}
// Edges of the User.
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.From("tomes", Tome.Type).
Ref("uploader").
Comment("Tomes uploaded by the user."),
}
}
// Annotations describes additional information for the ent.
func (User) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.Mutations(
entgql.MutationUpdate(),
),
}
}
func newSecureToken() string {
buf := make([]byte, 64)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
panic(fmt.Errorf("failed to generate random token: %w", err))
}
return base64.StdEncoding.EncodeToString(buf)
}