-
Notifications
You must be signed in to change notification settings - Fork 28
/
model.go
314 lines (281 loc) · 8.14 KB
/
model.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package user
import (
"fmt"
"time"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/lib/infra/db"
)
type ListOptions struct {
SortOption SortOption
}
type FilterOptions struct {
GroupKeys []string
RoleKeys []string
}
func (o FilterOptions) IsFilterEnabled() bool {
return len(o.GroupKeys) > 0 || len(o.RoleKeys) > 0
}
type SortBy string
const (
SortByDefault SortBy = ""
SortByCreatedAt SortBy = "created_at"
SortByLastLoginAt SortBy = "last_login_at"
)
type SortOption struct {
SortBy SortBy
SortDirection model.SortDirection
}
func (o SortOption) Apply(builder db.SelectBuilder) db.SelectBuilder {
sortBy := o.SortBy
if sortBy == SortByDefault {
sortBy = SortByCreatedAt
}
sortDirection := o.SortDirection
if sortDirection == model.SortDirectionDefault {
sortDirection = model.SortDirectionDesc
}
return builder.OrderBy(fmt.Sprintf("%s %s NULLS LAST", sortBy, sortDirection))
}
var InvalidAccountStatusTransition = apierrors.Invalid.WithReason("InvalidAccountStatusTransition")
type AccountStatusType string
const (
AccountStatusTypeNormal AccountStatusType = "normal"
AccountStatusTypeDisabled AccountStatusType = "disabled"
AccountStatusTypeDeactivated AccountStatusType = "deactivated"
AccountStatusTypeScheduledDeletionDisabled AccountStatusType = "scheduled_deletion_disabled"
AccountStatusTypeScheduledDeletionDeactivated AccountStatusType = "scheduled_deletion_deactivated"
AccountStatusTypeAnonymized AccountStatusType = "anonymized"
AccountStatusTypeScheduledAnonymizationDisabled AccountStatusType = "scheduled_anonymization_disabled"
)
// AccountStatus represents disabled, deactivated, or scheduled deletion state.
// The zero value means normal.
type AccountStatus struct {
IsDisabled bool
IsDeactivated bool
DisableReason *string
DeleteAt *time.Time
IsAnonymized bool
AnonymizeAt *time.Time
}
func (s AccountStatus) Type() AccountStatusType {
if !s.IsDisabled {
return AccountStatusTypeNormal
}
if s.DeleteAt != nil {
if s.IsDeactivated {
return AccountStatusTypeScheduledDeletionDeactivated
}
return AccountStatusTypeScheduledDeletionDisabled
}
if s.IsAnonymized {
return AccountStatusTypeAnonymized
}
if s.AnonymizeAt != nil {
return AccountStatusTypeScheduledAnonymizationDisabled
}
if s.IsDeactivated {
return AccountStatusTypeDeactivated
}
return AccountStatusTypeDisabled
}
func (s AccountStatus) Check() error {
// This method must be in sync with IsAccountStatusError.
typ := s.Type()
switch typ {
case AccountStatusTypeNormal:
return nil
case AccountStatusTypeDisabled:
return NewErrDisabledUser(s.DisableReason)
case AccountStatusTypeDeactivated:
return ErrDeactivatedUser
case AccountStatusTypeAnonymized:
return ErrAnonymizedUser
case AccountStatusTypeScheduledDeletionDisabled:
return NewErrScheduledDeletionByAdmin(*s.DeleteAt)
case AccountStatusTypeScheduledDeletionDeactivated:
return NewErrScheduledDeletionByEndUser(*s.DeleteAt)
case AccountStatusTypeScheduledAnonymizationDisabled:
return NewErrScheduledAnonymizationByAdmin(*s.AnonymizeAt)
default:
panic(fmt.Errorf("unknown account status type: %v", typ))
}
}
func (s AccountStatus) Reenable() (*AccountStatus, error) {
target := AccountStatus{}
if s.Type() == AccountStatusTypeDisabled {
return &target, nil
}
return nil, s.makeTransitionError(target.Type())
}
func (s AccountStatus) Disable(reason *string) (*AccountStatus, error) {
target := AccountStatus{
IsDisabled: true,
DisableReason: reason,
}
if s.Type() == AccountStatusTypeNormal {
return &target, nil
}
return nil, s.makeTransitionError(target.Type())
}
func (s AccountStatus) ScheduleDeletionByEndUser(deleteAt time.Time) (*AccountStatus, error) {
target := AccountStatus{
IsDisabled: true,
IsDeactivated: true,
DeleteAt: &deleteAt,
}
if s.Type() != AccountStatusTypeNormal {
return nil, s.makeTransitionError(target.Type())
}
return &target, nil
}
func (s AccountStatus) ScheduleDeletionByAdmin(deleteAt time.Time) (*AccountStatus, error) {
target := AccountStatus{
IsDisabled: true,
IsAnonymized: s.IsAnonymized,
DeleteAt: &deleteAt,
}
if s.DeleteAt != nil {
return nil, s.makeTransitionError(target.Type())
}
return &target, nil
}
func (s AccountStatus) UnscheduleDeletionByAdmin() (*AccountStatus, error) {
target := AccountStatus{
IsDisabled: s.IsAnonymized,
IsAnonymized: s.IsAnonymized,
DeleteAt: nil,
}
if s.DeleteAt == nil {
return nil, s.makeTransitionError(target.Type())
}
return &target, nil
}
func (s AccountStatus) Anonymize() (*AccountStatus, error) {
target := AccountStatus{
IsDisabled: true,
IsAnonymized: true,
AnonymizeAt: s.AnonymizeAt,
}
if s.Type() == AccountStatusTypeNormal {
return &target, nil
}
return nil, s.makeTransitionError(target.Type())
}
func (s AccountStatus) ScheduleAnonymizationByAdmin(anonymizeAt time.Time) (*AccountStatus, error) {
target := AccountStatus{
IsDisabled: true,
AnonymizeAt: &anonymizeAt,
}
if s.AnonymizeAt != nil {
return nil, s.makeTransitionError(target.Type())
}
return &target, nil
}
func (s AccountStatus) UnscheduleAnonymizationByAdmin() (*AccountStatus, error) {
var target AccountStatus
if s.AnonymizeAt == nil {
return nil, s.makeTransitionError(target.Type())
}
return &target, nil
}
func (s AccountStatus) makeTransitionError(targetType AccountStatusType) error {
return InvalidAccountStatusTransition.NewWithInfo(
fmt.Sprintf("invalid account status transition: %v -> %v", s.Type(), targetType),
map[string]interface{}{
"from": s.Type(),
"to": targetType,
},
)
}
type User struct {
ID string
CreatedAt time.Time
UpdatedAt time.Time
MostRecentLoginAt *time.Time
LessRecentLoginAt *time.Time
IsDisabled bool
DisableReason *string
IsDeactivated bool
DeleteAt *time.Time
IsAnonymized bool
AnonymizeAt *time.Time
StandardAttributes map[string]interface{}
CustomAttributes map[string]interface{}
LastIndexedAt *time.Time
RequireReindexAfter *time.Time
}
func (u *User) GetMeta() model.Meta {
return model.Meta{
ID: u.ID,
CreatedAt: u.CreatedAt,
UpdatedAt: u.UpdatedAt,
}
}
func (u *User) ToRef() *model.UserRef {
return &model.UserRef{
Meta: u.GetMeta(),
}
}
func (u *User) AccountStatus() AccountStatus {
return AccountStatus{
IsDisabled: u.IsDisabled,
DisableReason: u.DisableReason,
IsDeactivated: u.IsDeactivated,
DeleteAt: u.DeleteAt,
IsAnonymized: u.IsAnonymized,
AnonymizeAt: u.AnonymizeAt,
}
}
func newUserModel(
user *User,
identities []*identity.Info,
authenticators []*authenticator.Info,
isVerified bool,
derivedStandardAttributes map[string]interface{},
customAttributes map[string]interface{},
web3Info *model.UserWeb3Info,
roles []string,
groups []string,
) *model.User {
isAnonymous := false
for _, i := range identities {
if i.Type == model.IdentityTypeAnonymous {
isAnonymous = true
break
}
}
canReauthenticate := false
for _, i := range authenticators {
if i.Kind == authenticator.KindPrimary {
canReauthenticate = true
}
if i.Kind == authenticator.KindSecondary {
canReauthenticate = true
}
}
return &model.User{
Meta: model.Meta{
ID: user.ID,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
},
LastLoginAt: user.MostRecentLoginAt,
IsAnonymous: isAnonymous,
IsVerified: isVerified,
IsDisabled: user.IsDisabled,
DisableReason: user.DisableReason,
IsDeactivated: user.IsDeactivated,
DeleteAt: user.DeleteAt,
IsAnonymized: user.IsAnonymized,
AnonymizeAt: user.AnonymizeAt,
CanReauthenticate: canReauthenticate,
StandardAttributes: derivedStandardAttributes,
CustomAttributes: customAttributes,
Web3: web3Info,
Roles: roles,
Groups: groups,
}
}