-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuser-sql.go
388 lines (315 loc) · 10.9 KB
/
user-sql.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package db
import (
"database/sql"
"errors"
"fmt"
"github.com/chadweimer/gomp/models"
"github.com/jmoiron/sqlx"
"golang.org/x/crypto/bcrypt"
)
type sqlUserDriver struct {
Db *sqlx.DB
}
func (d *sqlUserDriver) Authenticate(username, password string) (*models.User, error) {
return get(d.Db, func(db sqlx.Queryer) (*models.User, error) {
user := new(UserWithPasswordHash)
if err := sqlx.Get(db, user, "SELECT * FROM app_user WHERE username = $1", username); err != nil {
return nil, err
}
if err := verifyPassword(user, password); err != nil {
return nil, err
}
return &user.User, nil
})
}
func (d *sqlUserDriver) Create(user *UserWithPasswordHash) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.createImpl(user, db)
})
}
func (*sqlUserDriver) createImpl(user *UserWithPasswordHash, db sqlx.Queryer) error {
stmt := "INSERT INTO app_user (username, password_hash, access_level) " +
"VALUES ($1, $2, $3) RETURNING id"
return sqlx.Get(db, user, stmt, user.Username, user.PasswordHash, user.AccessLevel)
}
func (d *sqlUserDriver) Read(id int64) (*UserWithPasswordHash, error) {
return get(d.Db, func(db sqlx.Queryer) (*UserWithPasswordHash, error) {
return d.readImpl(id, db)
})
}
func (*sqlUserDriver) readImpl(id int64, db sqlx.Queryer) (*UserWithPasswordHash, error) {
user := new(UserWithPasswordHash)
if err := sqlx.Get(db, user, "SELECT * FROM app_user WHERE id = $1", id); err != nil {
return nil, err
}
return user, nil
}
func (d *sqlUserDriver) Update(user *models.User) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.updateImpl(user, db)
})
}
func (*sqlUserDriver) updateImpl(user *models.User, db sqlx.Execer) error {
_, err := db.Exec("UPDATE app_user SET username = $1, access_level = $2 WHERE ID = $3",
user.Username, user.AccessLevel, user.Id)
return err
}
func (d *sqlUserDriver) UpdatePassword(id int64, password, newPassword string) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.updatePasswordImpl(id, password, newPassword, db)
})
}
func (d *sqlUserDriver) updatePasswordImpl(id int64, password, newPassword string, db sqlx.Ext) error {
// Make sure the current password is correct
user, err := d.readImpl(id, db)
if err != nil {
return err
}
if err = verifyPassword(user, password); err != nil {
return err
}
newPasswordHash, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
if err != nil {
return errors.New("invalid password specified")
}
_, err = db.Exec("UPDATE app_user SET password_hash = $1 WHERE ID = $2",
newPasswordHash, user.Id)
return err
}
func (d *sqlUserDriver) ReadSettings(id int64) (*models.UserSettings, error) {
return get(d.Db, func(db sqlx.Queryer) (*models.UserSettings, error) {
return d.readSettingsImpl(id, db)
})
}
func (*sqlUserDriver) readSettingsImpl(id int64, db sqlx.Queryer) (*models.UserSettings, error) {
userSettings := new(models.UserSettings)
if err := sqlx.Get(db, userSettings, "SELECT * FROM app_user_settings WHERE user_id = $1", id); err != nil {
return nil, err
}
var tags []string
if err := sqlx.Select(db, &tags, "SELECT tag FROM app_user_favorite_tag WHERE user_id = $1 ORDER BY tag ASC", id); err != nil {
return nil, err
}
userSettings.FavoriteTags = tags
return userSettings, nil
}
func (d *sqlUserDriver) UpdateSettings(settings *models.UserSettings) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.updateSettingsImpl(settings, db)
})
}
func (*sqlUserDriver) updateSettingsImpl(settings *models.UserSettings, db sqlx.Execer) error {
_, err := db.Exec(
"UPDATE app_user_settings "+
"SET home_title = $1, home_image_url = $2 WHERE user_id = $3",
settings.HomeTitle, settings.HomeImageUrl, settings.UserId)
if err != nil {
return err
}
// Deleting and recreating seems inefficient. Maybe make this smarter.
_, err = db.Exec(
"DELETE FROM app_user_favorite_tag WHERE user_id = $1",
settings.UserId)
if err != nil {
return fmt.Errorf("deleting favorite tags before updating on user: %w", err)
}
for _, tag := range settings.FavoriteTags {
_, err = db.Exec(
"INSERT INTO app_user_favorite_tag (user_id, tag) VALUES ($1, $2)",
settings.UserId, tag)
if err != nil {
return fmt.Errorf("updating favorite tags on user: %w", err)
}
}
return nil
}
func (d *sqlUserDriver) Delete(id int64) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.deleteImpl(id, db)
})
}
func (*sqlUserDriver) deleteImpl(id int64, db sqlx.Execer) error {
_, err := db.Exec("DELETE FROM app_user WHERE id = $1", id)
return err
}
func (d *sqlUserDriver) List() (*[]models.User, error) {
return get(d.Db, func(db sqlx.Queryer) (*[]models.User, error) {
return d.listImpl(db)
})
}
func (*sqlUserDriver) listImpl(db sqlx.Queryer) (*[]models.User, error) {
var users []models.User
if err := sqlx.Select(db, &users, "SELECT id, username, access_level, created_at, modified_at FROM app_user ORDER BY username ASC"); err != nil {
return nil, err
}
return &users, nil
}
func (d *sqlUserDriver) CreateSearchFilter(filter *models.SavedSearchFilter) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.createSearchFilterImpl(filter, db)
})
}
func (d *sqlUserDriver) createSearchFilterImpl(filter *models.SavedSearchFilter, db sqlx.Ext) error {
if filter.UserId == nil {
return errors.New("user id is required")
}
stmt := "INSERT INTO search_filter (user_id, name, query, with_pictures, sort_by, sort_dir) " +
"VALUES ($1, $2, $3, $4, $5, $6) RETURNING id"
err := sqlx.Get(db, filter,
stmt, filter.UserId, filter.Name, filter.Query, filter.WithPictures, filter.SortBy, filter.SortDir)
if err != nil {
return err
}
if err = d.setSearchFilterFieldsImpl(*filter.Id, filter.Fields, db); err != nil {
return err
}
if err = d.setSearchFilterStatesImpl(*filter.Id, filter.States, db); err != nil {
return err
}
return d.setSearchFilterTagsImpl(*filter.Id, filter.Tags, db)
}
func (*sqlUserDriver) setSearchFilterFieldsImpl(filterId int64, fields []models.SearchField, db sqlx.Execer) error {
// Deleting and recreating seems inefficient. Maybe make this smarter.
if _, err := db.Exec("DELETE FROM search_filter_field WHERE search_filter_id = $1", filterId); err != nil {
return err
}
for _, field := range fields {
_, err := db.Exec(
"INSERT INTO search_filter_field (search_filter_id, field_name) VALUES ($1, $2)",
filterId, field)
if err != nil {
return err
}
}
return nil
}
func (*sqlUserDriver) setSearchFilterStatesImpl(filterId int64, states []models.RecipeState, db sqlx.Execer) error {
// Deleting and recreating seems inefficient. Maybe make this smarter.
if _, err := db.Exec("DELETE FROM search_filter_state WHERE search_filter_id = $1", filterId); err != nil {
return err
}
for _, state := range states {
_, err := db.Exec(
"INSERT INTO search_filter_state (search_filter_id, state) VALUES ($1, $2)",
filterId, state)
if err != nil {
return err
}
}
return nil
}
func (*sqlUserDriver) setSearchFilterTagsImpl(filterId int64, tags []string, db sqlx.Execer) error {
// Deleting and recreating seems inefficient. Maybe make this smarter.
if _, err := db.Exec("DELETE FROM search_filter_tag WHERE search_filter_id = $1", filterId); err != nil {
return err
}
for _, tag := range tags {
_, err := db.Exec(
"INSERT INTO search_filter_tag (search_filter_id, tag) VALUES ($1, $2)",
filterId, tag)
if err != nil {
return err
}
}
return nil
}
func (d *sqlUserDriver) ReadSearchFilter(userId int64, filterId int64) (*models.SavedSearchFilter, error) {
return get(d.Db, func(db sqlx.Queryer) (*models.SavedSearchFilter, error) {
return d.readSearchFilterImpl(userId, filterId, db)
})
}
func (*sqlUserDriver) readSearchFilterImpl(userId int64, filterId int64, db sqlx.Queryer) (*models.SavedSearchFilter, error) {
filter := new(models.SavedSearchFilter)
if err := sqlx.Get(db, filter, "SELECT * FROM search_filter WHERE id = $1 AND user_id = $2", filterId, userId); err != nil {
return nil, err
}
var fields []models.SearchField
if err := sqlx.Select(
db,
&fields,
"SELECT field_name FROM search_filter_field WHERE search_filter_id = $1",
filterId); err != nil && err != sql.ErrNoRows {
return nil, err
}
filter.Fields = fields
var states []models.RecipeState
if err := sqlx.Select(
db,
&states,
"SELECT state FROM search_filter_state WHERE search_filter_id = $1",
filterId); err != nil && err != sql.ErrNoRows {
return nil, err
}
filter.States = states
var tags []string
if err := sqlx.Select(
db,
&tags,
"SELECT tag FROM search_filter_tag WHERE search_filter_id = $1",
filterId); err != nil && err != sql.ErrNoRows {
return nil, err
}
filter.Tags = tags
return filter, nil
}
func (d *sqlUserDriver) UpdateSearchFilter(filter *models.SavedSearchFilter) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.updateSearchFilterImpl(filter, db)
})
}
func (d *sqlUserDriver) updateSearchFilterImpl(filter *models.SavedSearchFilter, db sqlx.Ext) error {
if filter.Id == nil {
return errors.New("filter id is required")
}
if filter.UserId == nil {
return errors.New("user id is required")
}
// Make sure the filter exists, which is important to confirm the filter is owned by the specified user
if _, err := d.readSearchFilterImpl(*filter.UserId, *filter.Id, db); err != nil {
return err
}
stmt := "UPDATE search_filter SET name = $1, query = $2, with_pictures = $3, sort_by = $4, sort_dir = $5 " +
"WHERE id = $6 AND user_id = $7"
_, err := db.Exec(
stmt, filter.Name, filter.Query, filter.WithPictures, filter.SortBy, filter.SortDir, filter.Id, filter.UserId)
if err != nil {
return err
}
if err = d.setSearchFilterFieldsImpl(*filter.Id, filter.Fields, db); err != nil {
return err
}
if err = d.setSearchFilterStatesImpl(*filter.Id, filter.States, db); err != nil {
return err
}
return d.setSearchFilterTagsImpl(*filter.Id, filter.Tags, db)
}
func (d *sqlUserDriver) DeleteSearchFilter(userId int64, filterId int64) error {
return tx(d.Db, func(db sqlx.Ext) error {
return d.deleteSearchFilterImpl(userId, filterId, db)
})
}
func (*sqlUserDriver) deleteSearchFilterImpl(userId int64, filterId int64, db sqlx.Execer) error {
_, err := db.Exec("DELETE FROM search_filter WHERE id = $1 AND user_id = $2", filterId, userId)
return err
}
// List retrieves all user's saved search filters.
func (d *sqlUserDriver) ListSearchFilters(userId int64) (*[]models.SavedSearchFilterCompact, error) {
return get(d.Db, func(db sqlx.Queryer) (*[]models.SavedSearchFilterCompact, error) {
var filters []models.SavedSearchFilterCompact
err := sqlx.Select(
db,
&filters,
"SELECT id, user_id, name FROM search_filter WHERE user_id = $1 ORDER BY name ASC",
userId)
if err != nil {
return nil, err
}
return &filters, nil
})
}
func verifyPassword(user *UserWithPasswordHash, password string) error {
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)); err != nil {
return errors.New("username or password invalid")
}
return nil
}