-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathmodel.go
More file actions
270 lines (236 loc) · 7.88 KB
/
model.go
File metadata and controls
270 lines (236 loc) · 7.88 KB
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
package user
import (
"fmt"
"time"
"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) GetSortBy() SortBy {
switch o.SortBy {
case SortByCreatedAt:
fallthrough
case SortByLastLoginAt:
return o.SortBy
}
return SortByCreatedAt
}
func (o SortOption) GetSortDirection() model.SortDirection {
switch o.SortDirection {
case model.SortDirectionAsc:
fallthrough
case model.SortDirectionDesc:
return o.SortDirection
}
return model.SortDirectionDesc
}
func (o SortOption) Apply(builder db.SelectBuilder, after string) db.SelectBuilder {
sortBy := o.GetSortBy()
sortDirection := o.GetSortDirection()
q := builder.OrderBy(fmt.Sprintf("%s %s NULLS LAST", sortBy, sortDirection))
if after != "" {
switch sortDirection {
case model.SortDirectionDesc:
q = q.Where(fmt.Sprintf("%s < ?", sortBy), after)
case model.SortDirectionAsc:
q = q.Where(fmt.Sprintf("%s > ?", sortBy), after)
}
}
return q
}
type User struct {
ID string
CreatedAt time.Time
UpdatedAt time.Time
MostRecentLoginAt *time.Time
LessRecentLoginAt *time.Time
StandardAttributes map[string]interface{}
CustomAttributes map[string]interface{}
LastIndexedAt *time.Time
RequireReindexAfter *time.Time
MFAGracePeriodtEndAt *time.Time
OptOutPasskeyUpsell bool
// Account Status columns
//
// isDisabled tells if the account is disabled for whatever reason.
isDisabled bool
// accountStatusStaleFrom tells if IsDisabled is accurate.
// If accountStatusStaleFrom is null, then IsDisabled is accurate.
// If now < accountStatusStaleFrom, then IsDisabled is accurate, else IsDisabled is stale.
accountStatusStaleFrom *time.Time
// isIndefinitelyDisabled tells if the account is disabled indefinitely.
// If isIndefinitelyDisabled is nullable, then an algorithm is used to set it to non-null.
isIndefinitelyDisabled *bool
// isDeactivated tells if the account is disabled via Admin API, or is disabled by the end-user.
// If isDeactivated is true, then the account is disabled by the end-user.
isDeactivated *bool
// disableReason is an optional string to specify the reason.
// It can be specified via Admin API.
disableReason *string
// temporarilyDisabledFrom and TemporarilyDisabledUntil forms a temporarily disabled period.
// Temporarily Disabled is mutually exclusive with Indefinitely Disabled.
temporarilyDisabledFrom *time.Time
temporarilyDisabledUntil *time.Time
// accountValidFrom and AccountValidUntil forms account valid period.
accountValidFrom *time.Time
accountValidUntil *time.Time
// deleteAt is the scheduled time when the account will be deleted.
deleteAt *time.Time
// deleteReason is the reason the account scheduled deletion.
deleteReason *string
// anonymizeAt is the scheduled time when the account will be anonymized.
anonymizeAt *time.Time
// anonymizedAt is the actual time when the account was anonymized.
anonymizedAt *time.Time
// isAnonymized tells if the account is anonymized.
isAnonymized *bool
}
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(refTime time.Time) AccountStatusWithRefTime {
return AccountStatus{
isDisabled: u.isDisabled,
accountStatusStaleFrom: u.accountStatusStaleFrom,
isIndefinitelyDisabled: u.isIndefinitelyDisabled,
isDeactivated: u.isDeactivated,
disableReason: u.disableReason,
temporarilyDisabledFrom: u.temporarilyDisabledFrom,
temporarilyDisabledUntil: u.temporarilyDisabledUntil,
accountValidFrom: u.accountValidFrom,
accountValidUntil: u.accountValidUntil,
deleteAt: u.deleteAt,
deleteReason: u.deleteReason,
anonymizeAt: u.anonymizeAt,
anonymizedAt: u.anonymizedAt,
isAnonymized: u.isAnonymized,
}.WithRefTime(refTime)
}
func newUserModel(
user *User,
refTime time.Time,
identities []*identity.Info,
authenticators []*authenticator.Info,
isVerified bool,
derivedStandardAttributes map[string]interface{},
customAttributes map[string]interface{},
roles []string,
groups []string,
) *model.User {
if derivedStandardAttributes == nil {
derivedStandardAttributes = make(map[string]interface{})
}
if customAttributes == nil {
customAttributes = make(map[string]interface{})
}
if roles == nil {
roles = make([]string, 0)
}
if groups == nil {
groups = make([]string, 0)
}
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
}
}
accountStatus := user.AccountStatus(refTime)
return &model.User{
Meta: model.Meta{
ID: user.ID,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
},
LastLoginAt: user.MostRecentLoginAt,
IsAnonymous: isAnonymous,
IsVerified: isVerified,
IsDisabled: accountStatus.IsDisabled(),
DisableReason: accountStatus.DisableReason(),
IsDeactivated: accountStatus.IsDeactivated(),
DeleteAt: accountStatus.DeleteAt(),
DeleteReason: accountStatus.DeleteReason(),
IsAnonymized: accountStatus.IsAnonymized(),
AnonymizeAt: accountStatus.AnonymizeAt(),
TemporarilyDisabledFrom: accountStatus.TemporarilyDisabledFrom(),
TemporarilyDisabledUntil: accountStatus.TemporarilyDisabledUntil(),
AccountValidFrom: accountStatus.AccountValidFrom(),
AccountValidUntil: accountStatus.AccountValidUntil(),
AccountStatusStaleFrom: accountStatus.AccountStatusStaleFrom(),
CanReauthenticate: canReauthenticate,
StandardAttributes: derivedStandardAttributes,
CustomAttributes: customAttributes,
// For backwards compatibility, we always output an empty object here.
// The AdminAPI has marked this field non-null, so it MUST BE a map.
Web3: make(map[string]interface{}),
Roles: roles,
Groups: groups,
MFAGracePeriodtEndAt: user.MFAGracePeriodtEndAt,
EndUserAccountID: computeEndUserAccountID(derivedStandardAttributes, identities),
}
}
type UserForExport struct {
model.User
Identities []*identity.Info
Authenticators []*authenticator.Info
}
func computeEndUserAccountID(derivedStandardAttributes map[string]interface{}, identities []*identity.Info) string {
var endUserAccountID string
var ldapDisplayID string
for _, iden := range identities {
if iden.Type == model.IdentityTypeLDAP {
ldapDisplayID = iden.DisplayID()
break
}
}
if s, ok := derivedStandardAttributes[string(model.ClaimEmail)].(string); ok && s != "" {
endUserAccountID = s
} else if s, ok := derivedStandardAttributes[string(model.ClaimPreferredUsername)].(string); ok && s != "" {
endUserAccountID = s
} else if s, ok := derivedStandardAttributes[string(model.ClaimPhoneNumber)].(string); ok && s != "" {
endUserAccountID = s
} else if ldapDisplayID != "" {
endUserAccountID = ldapDisplayID
}
return endUserAccountID
}