generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
personalaccesstoken.go
138 lines (126 loc) · 3.37 KB
/
personalaccesstoken.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
package schema
import (
"entgo.io/contrib/entgql"
"entgo.io/contrib/entoas"
"entgo.io/ent"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
"github.com/datumforge/enthistory"
emixin "github.com/datumforge/entx/mixin"
"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/pkg/keygen"
)
// PersonalAccessToken holds the schema definition for the PersonalAccessToken entity.
type PersonalAccessToken struct {
ent.Schema
}
// Fields of the PersonalAccessToken
func (PersonalAccessToken) Fields() []ent.Field {
return []ent.Field{
field.String("name").
Comment("the name associated with the token").
NotEmpty(),
field.String("token").
Unique().
Immutable().
Annotations(
entgql.Skip(^entgql.SkipType),
entoas.Skip(true),
).
DefaultFunc(func() string {
token := keygen.PrefixedSecret("dtmp") // datum token prefix
return token
}),
field.Time("expires_at").
Comment("when the token expires").
Annotations(
entgql.Skip(entgql.SkipMutationUpdateInput),
).
Nillable(),
field.String("description").
Comment("a description of the token's purpose").
Optional().
Nillable().
Annotations(
entgql.Skip(entgql.SkipWhereInput),
),
field.JSON("scopes", []string{}).
Optional(),
field.Time("last_used_at").
Optional().
Nillable(),
}
}
// Edges of the PersonalAccessToken
func (PersonalAccessToken) Edges() []ent.Edge {
return []ent.Edge{
edge.From("organizations", Organization.Type).
Ref("personal_access_tokens").
Annotations(entoas.Skip(true)).
Comment("the organization(s) the token is associated with"),
edge.To("events", Event.Type),
}
}
// Indexes of the PersonalAccessToken
func (PersonalAccessToken) Indexes() []ent.Index {
return []ent.Index{
// non-unique index.
index.Fields("token"),
}
}
// Mixin of the PersonalAccessToken
func (PersonalAccessToken) Mixin() []ent.Mixin {
return []ent.Mixin{
emixin.AuditMixin{},
mixin.SoftDeleteMixin{},
emixin.IDMixin{},
emixin.TagMixin{},
UserOwnedMixin{
Ref: "personal_access_tokens",
AllowUpdate: false,
SkipInterceptor: interceptors.SkipOnlyQuery,
},
}
}
// Annotations of the PersonalAccessToken
func (PersonalAccessToken) Annotations() []schema.Annotation {
return []schema.Annotation{
entgql.QueryField(),
entgql.RelayConnection(),
entgql.Mutations(entgql.MutationCreate(), (entgql.MutationUpdate())),
enthistory.Annotations{
Exclude: true,
},
}
}
// Hooks of the PersonalAccessToken
func (PersonalAccessToken) Hooks() []ent.Hook {
return []ent.Hook{
hooks.HookCreatePersonalAccessToken(),
hooks.HookUpdatePersonalAccessToken(),
}
}
// Interceptors of the PersonalAccessToken
func (PersonalAccessToken) Interceptors() []ent.Interceptor {
return []ent.Interceptor{
interceptors.InterceptorPat(),
}
}
// Policy of the PersonalAccessToken
func (PersonalAccessToken) Policy() ent.Policy {
return privacy.Policy{
Mutation: privacy.MutationPolicy{
rule.AllowMutationAfterApplyingOwnerFilter(),
privacy.AlwaysAllowRule(),
},
Query: privacy.QueryPolicy{
privacy.AlwaysAllowRule(),
},
}
}