-
Notifications
You must be signed in to change notification settings - Fork 310
/
user.go
209 lines (185 loc) · 7.79 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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"time"
"github.com/gogo/protobuf/types"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
// User model.
type User struct {
Model
SoftDelete
Account Account `gorm:"polymorphic:Account;polymorphic_value:user"`
// BEGIN common fields
Name string `gorm:"type:VARCHAR"`
Description string `gorm:"type:TEXT"`
Attributes []Attribute `gorm:"polymorphic:Entity;polymorphic_value:user"`
APIKeys []APIKey `gorm:"polymorphic:Entity;polymorphic_value:user"`
// END common fields
Sessions []*UserSession
PrimaryEmailAddress string `gorm:"type:VARCHAR;not null;unique_index"`
PrimaryEmailAddressValidatedAt *time.Time // should be cleared when email changes
Password string `gorm:"type:VARCHAR;not null"` // this is the hash
PasswordUpdatedAt time.Time `gorm:"not null"`
RequirePasswordUpdate bool `gorm:"not null"`
State int `gorm:"not null"`
Admin bool `gorm:"not null"`
TemporaryPassword string `gorm:"type:VARCHAR"`
TemporaryPasswordCreatedAt *time.Time
TemporaryPasswordExpiresAt *time.Time
ProfilePicture *Picture
ProfilePictureID *string `gorm:"type:UUID;index:user_profile_picture_index"`
}
func init() {
registerModel(&User{})
}
// functions to set fields from the user model into the user proto.
var userPBSetters = map[string]func(*ttnpb.User, *User){
nameField: func(pb *ttnpb.User, usr *User) { pb.Name = usr.Name },
descriptionField: func(pb *ttnpb.User, usr *User) { pb.Description = usr.Description },
attributesField: func(pb *ttnpb.User, usr *User) { pb.Attributes = attributes(usr.Attributes).toMap() },
primaryEmailAddressField: func(pb *ttnpb.User, usr *User) { pb.PrimaryEmailAddress = usr.PrimaryEmailAddress },
primaryEmailAddressValidatedAtField: func(pb *ttnpb.User, usr *User) {
pb.PrimaryEmailAddressValidatedAt = usr.PrimaryEmailAddressValidatedAt
},
passwordField: func(pb *ttnpb.User, usr *User) { pb.Password = usr.Password },
passwordUpdatedAtField: func(pb *ttnpb.User, usr *User) {
t := cleanTime(usr.PasswordUpdatedAt)
pb.PasswordUpdatedAt = &t
},
requirePasswordUpdateField: func(pb *ttnpb.User, usr *User) { pb.RequirePasswordUpdate = usr.RequirePasswordUpdate },
stateField: func(pb *ttnpb.User, usr *User) { pb.State = ttnpb.State(usr.State) },
adminField: func(pb *ttnpb.User, usr *User) { pb.Admin = usr.Admin },
temporaryPasswordField: func(pb *ttnpb.User, usr *User) {
pb.TemporaryPassword = usr.TemporaryPassword
},
temporaryPasswordCreatedAtField: func(pb *ttnpb.User, usr *User) {
pb.TemporaryPasswordCreatedAt = cleanTimePtr(usr.TemporaryPasswordCreatedAt)
},
temporaryPasswordExpiresAtField: func(pb *ttnpb.User, usr *User) {
pb.TemporaryPasswordExpiresAt = cleanTimePtr(usr.TemporaryPasswordExpiresAt)
},
profilePictureField: func(pb *ttnpb.User, usr *User) {
if usr.ProfilePicture == nil {
pb.ProfilePicture = nil
} else {
pb.ProfilePicture = usr.ProfilePicture.toPB()
}
},
}
// functions to set fields from the user proto into the user model.
var userModelSetters = map[string]func(*User, *ttnpb.User){
nameField: func(usr *User, pb *ttnpb.User) { usr.Name = pb.Name },
descriptionField: func(usr *User, pb *ttnpb.User) { usr.Description = pb.Description },
attributesField: func(usr *User, pb *ttnpb.User) {
usr.Attributes = attributes(usr.Attributes).updateFromMap(pb.Attributes)
},
primaryEmailAddressField: func(usr *User, pb *ttnpb.User) { usr.PrimaryEmailAddress = pb.PrimaryEmailAddress },
primaryEmailAddressValidatedAtField: func(usr *User, pb *ttnpb.User) {
usr.PrimaryEmailAddressValidatedAt = pb.PrimaryEmailAddressValidatedAt
},
passwordField: func(usr *User, pb *ttnpb.User) { usr.Password = pb.Password },
passwordUpdatedAtField: func(usr *User, pb *ttnpb.User) {
if pb.PasswordUpdatedAt != nil {
usr.PasswordUpdatedAt = cleanTime(*pb.PasswordUpdatedAt)
} else {
usr.PasswordUpdatedAt = cleanTime(time.Now())
}
},
requirePasswordUpdateField: func(usr *User, pb *ttnpb.User) { usr.RequirePasswordUpdate = pb.RequirePasswordUpdate },
stateField: func(usr *User, pb *ttnpb.User) { usr.State = int(pb.State) },
adminField: func(usr *User, pb *ttnpb.User) { usr.Admin = pb.Admin },
temporaryPasswordField: func(usr *User, pb *ttnpb.User) {
usr.TemporaryPassword = pb.TemporaryPassword
},
temporaryPasswordCreatedAtField: func(usr *User, pb *ttnpb.User) {
usr.TemporaryPasswordCreatedAt = cleanTimePtr(pb.TemporaryPasswordCreatedAt)
},
temporaryPasswordExpiresAtField: func(usr *User, pb *ttnpb.User) {
usr.TemporaryPasswordExpiresAt = cleanTimePtr(pb.TemporaryPasswordExpiresAt)
},
profilePictureField: func(usr *User, pb *ttnpb.User) {
usr.ProfilePictureID, usr.ProfilePicture = nil, nil
if pb.ProfilePicture != nil {
usr.ProfilePicture = &Picture{}
usr.ProfilePicture.fromPB(pb.ProfilePicture)
}
},
}
// fieldMask to use if a nil or empty fieldmask is passed.
var defaultUserFieldMask = &types.FieldMask{}
func init() {
paths := make([]string, 0, len(userPBSetters))
for _, path := range ttnpb.UserFieldPathsNested {
if _, ok := userPBSetters[path]; ok {
paths = append(paths, path)
}
}
defaultUserFieldMask.Paths = paths
}
// fieldmask path to column name in users table.
var userColumnNames = map[string][]string{
attributesField: {},
contactInfoField: {},
nameField: {nameField},
descriptionField: {descriptionField},
primaryEmailAddressField: {primaryEmailAddressField},
primaryEmailAddressValidatedAtField: {primaryEmailAddressValidatedAtField},
passwordField: {passwordField},
passwordUpdatedAtField: {passwordUpdatedAtField},
requirePasswordUpdateField: {requirePasswordUpdateField},
stateField: {stateField},
adminField: {adminField},
temporaryPasswordField: {temporaryPasswordField},
temporaryPasswordCreatedAtField: {temporaryPasswordCreatedAtField},
temporaryPasswordExpiresAtField: {temporaryPasswordExpiresAtField},
}
func (usr User) toPB(pb *ttnpb.User, fieldMask *types.FieldMask) {
pb.UserIdentifiers.UserID = usr.Account.UID
pb.CreatedAt = cleanTime(usr.CreatedAt)
pb.UpdatedAt = cleanTime(usr.UpdatedAt)
if fieldMask == nil || len(fieldMask.Paths) == 0 {
fieldMask = defaultUserFieldMask
}
for _, path := range fieldMask.Paths {
if setter, ok := userPBSetters[path]; ok {
setter(pb, &usr)
}
}
}
func (usr *User) fromPB(pb *ttnpb.User, fieldMask *types.FieldMask) (columns []string) {
if fieldMask == nil || len(fieldMask.Paths) == 0 {
fieldMask = defaultUserFieldMask
}
for _, path := range fieldMask.Paths {
if setter, ok := userModelSetters[path]; ok {
setter(usr, pb)
if columnNames, ok := userColumnNames[path]; ok {
columns = append(columns, columnNames...)
}
continue
}
}
return
}
type userWithUID struct {
UID string
User `gorm:"embedded"`
}
func (userWithUID) TableName() string { return "users" }
func (u userWithUID) toPB(pb *ttnpb.User, fieldMask *types.FieldMask) {
u.User.Account.UID = u.UID
u.User.toPB(pb, fieldMask)
}