-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
282 lines (230 loc) · 6.29 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
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
package hexa
import (
"errors"
"fmt"
"github.com/kamva/gutil"
"github.com/kamva/tracer"
)
// UserType is type of a user. possible values is :
// guest: Use for guest users.
// regular: Use for regular users of app (real registered users)
// service: Use for service users (microservices,...)
type UserType string
const (
UserTypeGuest UserType = "_guest"
UserTypeRegular UserType = "_regular"
UserTypeService UserType = "_service"
)
// User meta keys.
const (
UserMetaKeyUserType = "_user_type"
UserMetaKeyIdentifier = "_user_identifier"
UserMetaKeyEmail = "_user_email"
UserMetaKeyPhone = "_user_phone"
UserMetaKeyName = "_user_name"
UserMetaKeyUsername = "_user_username"
UserMetaKeyIsActive = "_user_is_active"
UserMetaKeyRoles = "_user_roles"
)
var userMetaKeys = []string{
UserMetaKeyUserType,
UserMetaKeyIdentifier,
UserMetaKeyEmail,
UserMetaKeyPhone,
UserMetaKeyName,
UserMetaKeyUsername,
UserMetaKeyIsActive,
UserMetaKeyRoles,
}
// guestUserID is the guest user's id
const guestUserID = "_guest_id"
// User who sends request to the app (can be guest,regular user,service user,...)
type User interface {
// Type specifies user's type (guest,regular,...)
Type() UserType
// Identifier returns user's identifier
Identifier() string
// Email returns the user's email.
// This value can be empty.
Email() string
// Phone returns the user's phone number.
// This value can be empty.
Phone() string
// Name returns the user name.
Name() string
// Username can be unique username,email,phone number or
// everything else which can be used as username.
Username() string
// IsActive specify that user is active or no.
IsActive() bool
// Roles returns user's roles.
Roles() []string
Meta(key string) (val any, exists bool)
SetMeta(key string, val any) (User, error)
// User must be able be export and import using this meta data.
// Meta data must be json serializable.
MetaData() Map
}
// user is default implementation of hexa User for real users.
type user struct {
meta map[string]any
}
func (u *user) Meta(key string) (val any, exists bool) {
val, exists = u.meta[key]
return
}
func (u *user) copyMeta() (map[string]any, error) {
m := make(map[string]any)
if err := gutil.UnmarshalStruct(u.meta, &m); err != nil {
return nil, tracer.Trace(err)
}
return m, tracer.Trace(userMetaInterfaceToTrueTypedMeta(m))
}
func (u *user) SetMeta(key string, val any) (User, error) {
m, err := u.copyMeta()
if err != nil {
return nil, tracer.Trace(err)
}
m[key] = val
if err := validateUserMetaData(m); err != nil {
return nil, tracer.Trace(err)
}
return NewUserFromMeta(m)
}
func (u *user) MetaData() Map {
m := make(map[string]any)
for k, v := range u.meta {
m[k] = v
}
return m
}
func (u *user) metaString(k string) string {
return u.meta[k].(string)
}
func (u *user) Type() UserType {
return u.meta[UserMetaKeyUserType].(UserType)
}
func (u *user) Identifier() string {
return u.metaString(UserMetaKeyIdentifier)
}
func (u *user) Email() string {
return u.metaString(UserMetaKeyEmail)
}
func (u *user) Phone() string {
return u.metaString(UserMetaKeyPhone)
}
func (u *user) Name() string {
return u.metaString(UserMetaKeyName)
}
func (u *user) Username() string {
return u.metaString(UserMetaKeyUsername)
}
func (u *user) IsActive() bool {
return u.meta[UserMetaKeyIsActive].(bool)
}
func (u *user) Roles() []string {
return u.meta[UserMetaKeyRoles].([]string)
}
// NewUserFromMeta creates new user from the meta keys.
func NewUserFromMeta(meta Map) (User, error) {
if err := validateUserMetaData(meta); err != nil {
return nil, tracer.Trace(err)
}
return &user{meta: meta}, nil
}
func MustNewUserFromMeta(meta Map) User {
u, err := NewUserFromMeta(meta)
if err != nil {
panic(err)
}
return u
}
type UserParams struct {
Id string
Type UserType
Email string
Phone string
Name string
UserName string
IsActive bool
Roles []string
}
// NewUser returns new hexa user instance.
func NewUser(p UserParams) User {
meta := map[string]any{
UserMetaKeyIdentifier: p.Id,
UserMetaKeyUserType: p.Type,
UserMetaKeyEmail: p.Email,
UserMetaKeyPhone: p.Phone,
UserMetaKeyName: p.Name,
UserMetaKeyUsername: p.UserName,
UserMetaKeyIsActive: p.IsActive,
UserMetaKeyRoles: p.Roles,
}
return MustNewUserFromMeta(meta)
}
// NewGuest returns new instance of guest user.
func NewGuest() User {
return NewUser(UserParams{
Id: guestUserID,
Type: UserTypeGuest,
Email: "",
Phone: "",
Name: "_guest",
UserName: "_guest_username",
IsActive: false,
Roles: []string{},
})
}
// NewServiceUser returns new instance of Service user.
func NewServiceUser(id, name string, isActive bool, roles []string) User {
return NewUser(UserParams{
Id: id,
Type: UserTypeService,
Email: "",
Phone: "",
Name: name,
UserName: "_service_username",
IsActive: isActive,
Roles: roles,
})
}
func validateUserMetaData(meta map[string]any) error {
// validate meta keys: all required meta keys must exists.
for _, k := range userMetaKeys {
if _, ok := meta[k]; !ok {
errStr := fmt.Sprintf("key %s not found in user's meta keys", k)
return tracer.Trace(errors.New(errStr))
}
}
// Validate userType field
if _, ok := meta[UserMetaKeyUserType].(UserType); !ok {
return tracer.Trace(errors.New("invalid type for usertype field in user's meta data"))
}
// Validate IsActive field
if _, ok := meta[UserMetaKeyIsActive].(bool); !ok {
return tracer.Trace(errors.New("invalid type for isActive field in user's meta data"))
}
// Validate roles field:
if _, ok := meta[UserMetaKeyRoles].([]string); !ok {
return tracer.Trace(errors.New("invalid type for roles field in user's meta data"))
}
return nil
}
func WithUserRole(u User, role string) User {
roles := append(u.Roles(), role)
return gutil.Must(u.SetMeta(UserMetaKeyRoles, roles)).(User)
}
func userMetaInterfaceToTrueTypedMeta(meta map[string]any) error {
meta[UserMetaKeyUserType] = UserType(meta[UserMetaKeyUserType].(string))
// Convert user roles from []any to []string:
roles := make([]string, 0)
err := gutil.UnmarshalStruct(meta[UserMetaKeyRoles], &roles)
if err != nil {
return tracer.Trace(err)
}
meta[UserMetaKeyRoles] = roles
return nil
}
// Assertion
var _ User = &user{}