/
settings_profile.go
274 lines (240 loc) · 7.89 KB
/
settings_profile.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
package viewmodels
import (
"github.com/iawaknahc/jsonschema/pkg/jsonpointer"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/lib/authn/stdattrs"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/accesscontrol"
"github.com/authgear/authgear-server/pkg/util/clock"
"github.com/authgear/authgear-server/pkg/util/labelutil"
"github.com/authgear/authgear-server/pkg/util/territoryutil"
"github.com/authgear/authgear-server/pkg/util/tzutil"
)
type CustomAttribute struct {
Value interface{}
Label string
EnumValueLabel string
Pointer string
Type string
IsEditable bool
Minimum *float64
Maximum *float64
Enum []CustomAttributeEnum
}
type CustomAttributeEnum struct {
Value string
Label string
}
type SettingsProfileViewModel struct {
FormattedName string
EndUserAccountID string
FormattedNames string
Today string
Timezones []tzutil.Timezone
Alpha2 []string
Languages []string
Emails []string
PhoneNumbers []string
PreferredUsernames []string
IsReadable func(jsonpointer string) bool
IsEditable func(jsonpointer string) bool
GetCustomAttributeByPointer func(jsonpointer string) *CustomAttribute
IsStandardAttributesAllHidden bool
Name string
GivenName string
FamilyName string
MiddleName string
Nickname string
Picture string
Profile string
Website string
Email string
PhoneNumber string
PreferredUsername string
Gender string
Birthdate string
Zoneinfo string
ZoneinfoTimezone *tzutil.Timezone
Locale string
AddressStreetAddress string
AddressLocality string
AddressRegion string
AddressPostalCode string
AddressCountry string
CustomAttributes []CustomAttribute
}
type SettingsProfileUserService interface {
Get(userID string, role accesscontrol.Role) (*model.User, error)
}
type SettingsProfileIdentityService interface {
ListByUser(userID string) ([]*identity.Info, error)
}
type SettingsProfileViewModeler struct {
Localization *config.LocalizationConfig
UserProfileConfig *config.UserProfileConfig
Users SettingsProfileUserService
Identities SettingsProfileIdentityService
Clock clock.Clock
}
// nolint: gocognit
func (m *SettingsProfileViewModeler) ViewModel(userID string) (*SettingsProfileViewModel, error) {
var emails []string
var phoneNumbers []string
var preferredUsernames []string
identities, err := m.Identities.ListByUser(userID)
if err != nil {
return nil, err
}
for _, iden := range identities {
standardClaims := iden.IdentityAwareStandardClaims()
if email, ok := standardClaims[model.ClaimEmail]; ok && email != "" {
emails = append(emails, email)
}
if phoneNumber, ok := standardClaims[model.ClaimPhoneNumber]; ok && phoneNumber != "" {
phoneNumbers = append(phoneNumbers, phoneNumber)
}
if preferredUsername, ok := standardClaims[model.ClaimPreferredUsername]; ok && preferredUsername != "" {
preferredUsernames = append(preferredUsernames, preferredUsername)
}
}
user, err := m.Users.Get(userID, config.RoleEndUser)
if err != nil {
return nil, err
}
stdAttrs := user.StandardAttributes
str := func(key string) string {
value, _ := stdAttrs[key].(string)
return value
}
addressStr := func(key string) string {
address, ok := stdAttrs[stdattrs.Address].(map[string]interface{})
if !ok {
return ""
}
value, _ := address[key].(string)
return value
}
now := m.Clock.NowUTC()
timezones, err := tzutil.List(now)
if err != nil {
return nil, err
}
accessControl := m.UserProfileConfig.StandardAttributes.GetAccessControl().MergedWith(
m.UserProfileConfig.CustomAttributes.GetAccessControl(),
)
isReadable := func(jsonpointer string) bool {
level := accessControl.GetLevel(
accesscontrol.Subject(jsonpointer),
config.RoleEndUser,
config.AccessControlLevelHidden,
)
return level >= config.AccessControlLevelReadonly
}
isEditable := func(jsonpointer string) bool {
level := accessControl.GetLevel(
accesscontrol.Subject(jsonpointer),
config.RoleEndUser,
config.AccessControlLevelHidden,
)
return level == config.AccessControlLevelReadwrite
}
zoneinfo := str(stdattrs.Zoneinfo)
var zoneinfoTimezone *tzutil.Timezone
if zoneinfo != "" {
zoneinfoTimezone, err = tzutil.AsTimezone(zoneinfo, now)
if err != nil {
return nil, err
}
}
var customAttrs []CustomAttribute
for _, c := range m.UserProfileConfig.CustomAttributes.Attributes {
level := accessControl.GetLevel(
accesscontrol.Subject(c.Pointer),
config.RoleEndUser,
config.AccessControlLevelHidden,
)
if level >= config.AccessControlLevelReadonly {
ptr, err := jsonpointer.Parse(c.Pointer)
if err != nil {
return nil, err
}
var value interface{}
if v, err := ptr.Traverse(user.CustomAttributes); err == nil {
value = v
}
var enumValueLabel string
var enum []CustomAttributeEnum
if c.Type == config.CustomAttributeTypeEnum {
if str, ok := value.(string); ok {
enumValueLabel = labelutil.Label(str)
}
for _, variant := range c.Enum {
enum = append(enum, CustomAttributeEnum{
Value: variant,
Label: labelutil.Label(variant),
})
}
}
customAttrs = append(customAttrs, CustomAttribute{
Value: value,
Label: labelutil.Label(ptr[0]),
EnumValueLabel: enumValueLabel,
Pointer: c.Pointer,
Type: string(c.Type),
IsEditable: level >= config.AccessControlLevelReadwrite,
Minimum: c.Minimum,
Maximum: c.Maximum,
Enum: enum,
})
}
}
getCustomAttributeByPointer := func(jsonpointer string) *CustomAttribute {
for _, ca := range customAttrs {
if ca.Pointer == jsonpointer {
out := ca
return &out
}
}
return nil
}
viewModel := &SettingsProfileViewModel{
FormattedName: stdattrs.T(stdAttrs).FormattedName(),
EndUserAccountID: user.EndUserAccountID(),
FormattedNames: stdattrs.T(stdAttrs).FormattedNames(),
Today: now.Format("2006-01-02"),
Timezones: timezones,
Alpha2: territoryutil.Alpha2,
Languages: m.Localization.SupportedLanguages,
Emails: emails,
PhoneNumbers: phoneNumbers,
PreferredUsernames: preferredUsernames,
IsReadable: isReadable,
IsEditable: isEditable,
GetCustomAttributeByPointer: getCustomAttributeByPointer,
IsStandardAttributesAllHidden: m.UserProfileConfig.StandardAttributes.IsEndUserAllHidden(),
Name: str(stdattrs.Name),
GivenName: str(stdattrs.GivenName),
FamilyName: str(stdattrs.FamilyName),
MiddleName: str(stdattrs.MiddleName),
Nickname: str(stdattrs.Nickname),
Picture: str(stdattrs.Picture),
Profile: str(stdattrs.Profile),
Website: str(stdattrs.Website),
Email: str(stdattrs.Email),
PhoneNumber: str(stdattrs.PhoneNumber),
PreferredUsername: str(stdattrs.PreferredUsername),
Gender: str(stdattrs.Gender),
Birthdate: str(stdattrs.Birthdate),
Zoneinfo: zoneinfo,
ZoneinfoTimezone: zoneinfoTimezone,
Locale: str(stdattrs.Locale),
AddressStreetAddress: addressStr(stdattrs.StreetAddress),
AddressLocality: addressStr(stdattrs.Locality),
AddressRegion: addressStr(stdattrs.Region),
AddressPostalCode: addressStr(stdattrs.PostalCode),
AddressCountry: addressStr(stdattrs.Country),
CustomAttributes: customAttrs,
}
return viewModel, nil
}