2017-11-20 00:10:04 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package storage // import "miniflux.app/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2020-12-23 00:02:10 -05:00
|
|
|
"runtime"
|
2017-11-20 00:10:04 -05:00
|
|
|
"strings"
|
|
|
|
|
2020-07-28 23:28:02 -04:00
|
|
|
"miniflux.app/logger"
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/model"
|
2017-11-23 01:22:33 -05:00
|
|
|
|
2020-12-22 00:14:10 -05:00
|
|
|
"github.com/lib/pq"
|
2017-11-20 00:10:04 -05:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2020-09-27 19:01:06 -04:00
|
|
|
// CountUsers returns the total number of users.
|
|
|
|
func (s *Storage) CountUsers() int {
|
|
|
|
var result int
|
|
|
|
err := s.db.QueryRow(`SELECT count(*) FROM users`).Scan(&result)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// SetLastLogin updates the last login date of a user.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) SetLastLogin(userID int64) error {
|
2019-10-30 01:48:07 -04:00
|
|
|
query := `UPDATE users SET last_login_at=now() WHERE id=$1`
|
2017-11-20 00:10:04 -05:00
|
|
|
_, err := s.db.Exec(query, userID)
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to update last login date: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// UserExists checks if a user exists by using the given username.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) UserExists(username string) bool {
|
2019-10-30 01:48:07 -04:00
|
|
|
var result bool
|
|
|
|
s.db.QueryRow(`SELECT true FROM users WHERE username=LOWER($1)`, username).Scan(&result)
|
|
|
|
return result
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// AnotherUserExists checks if another user exists with the given username.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) AnotherUserExists(userID int64, username string) bool {
|
2019-10-30 01:48:07 -04:00
|
|
|
var result bool
|
|
|
|
s.db.QueryRow(`SELECT true FROM users WHERE id != $1 AND username=LOWER($2)`, userID, username).Scan(&result)
|
|
|
|
return result
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// CreateUser creates a new user.
|
2021-01-04 00:20:21 -05:00
|
|
|
func (s *Storage) CreateUser(userCreationRequest *model.UserCreationRequest) (*model.User, error) {
|
|
|
|
var hashedPassword string
|
|
|
|
if userCreationRequest.Password != "" {
|
|
|
|
var err error
|
|
|
|
hashedPassword, err = hashPassword(userCreationRequest.Password)
|
2017-11-23 01:22:33 -05:00
|
|
|
if err != nil {
|
2021-01-04 00:20:21 -05:00
|
|
|
return nil, err
|
2017-11-23 01:22:33 -05:00
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2019-04-28 21:20:46 -04:00
|
|
|
query := `
|
|
|
|
INSERT INTO users
|
2020-12-22 00:14:10 -05:00
|
|
|
(username, password, is_admin, google_id, openid_connect_id)
|
2017-11-25 13:40:23 -05:00
|
|
|
VALUES
|
2020-12-22 00:14:10 -05:00
|
|
|
(LOWER($1), $2, $3, $4, $5)
|
2019-04-28 21:20:46 -04:00
|
|
|
RETURNING
|
2020-12-22 00:14:10 -05:00
|
|
|
id,
|
|
|
|
username,
|
|
|
|
is_admin,
|
|
|
|
language,
|
|
|
|
theme,
|
|
|
|
timezone,
|
|
|
|
entry_direction,
|
|
|
|
entries_per_page,
|
|
|
|
keyboard_shortcuts,
|
|
|
|
show_reading_time,
|
|
|
|
entry_swipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
double_tap,
|
2020-12-22 00:14:10 -05:00
|
|
|
stylesheet,
|
|
|
|
google_id,
|
2021-02-28 16:29:51 -05:00
|
|
|
openid_connect_id,
|
2021-05-14 07:51:51 -04:00
|
|
|
display_mode,
|
2021-08-30 10:53:05 -04:00
|
|
|
entry_order,
|
|
|
|
default_reading_speed,
|
2022-07-20 16:07:55 -04:00
|
|
|
cjk_reading_speed,
|
2022-10-16 01:36:59 -04:00
|
|
|
default_home_page,
|
2022-10-25 23:00:56 -04:00
|
|
|
categories_sorting_order
|
2019-04-28 21:20:46 -04:00
|
|
|
`
|
2017-11-25 13:40:23 -05:00
|
|
|
|
2020-12-22 18:33:15 -05:00
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
2021-01-04 00:20:21 -05:00
|
|
|
return nil, fmt.Errorf(`store: unable to start transaction: %v`, err)
|
2020-12-22 18:33:15 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:20:21 -05:00
|
|
|
var user model.User
|
|
|
|
err = tx.QueryRow(
|
|
|
|
query,
|
|
|
|
userCreationRequest.Username,
|
|
|
|
hashedPassword,
|
|
|
|
userCreationRequest.IsAdmin,
|
|
|
|
userCreationRequest.GoogleID,
|
|
|
|
userCreationRequest.OpenIDConnectID,
|
|
|
|
).Scan(
|
2017-11-25 13:40:23 -05:00
|
|
|
&user.ID,
|
2017-12-29 17:55:16 -05:00
|
|
|
&user.Username,
|
|
|
|
&user.IsAdmin,
|
2017-11-25 13:40:23 -05:00
|
|
|
&user.Language,
|
|
|
|
&user.Theme,
|
|
|
|
&user.Timezone,
|
2017-12-24 21:04:34 -05:00
|
|
|
&user.EntryDirection,
|
2020-07-08 19:24:54 -04:00
|
|
|
&user.EntriesPerPage,
|
2019-04-28 21:20:46 -04:00
|
|
|
&user.KeyboardShortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
&user.ShowReadingTime,
|
2020-09-27 10:41:04 -04:00
|
|
|
&user.EntrySwipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
&user.DoubleTap,
|
2020-12-22 00:14:10 -05:00
|
|
|
&user.Stylesheet,
|
|
|
|
&user.GoogleID,
|
|
|
|
&user.OpenIDConnectID,
|
2021-02-28 16:29:51 -05:00
|
|
|
&user.DisplayMode,
|
2021-05-14 07:51:51 -04:00
|
|
|
&user.EntryOrder,
|
2021-08-30 10:53:05 -04:00
|
|
|
&user.DefaultReadingSpeed,
|
|
|
|
&user.CJKReadingSpeed,
|
2022-07-20 16:07:55 -04:00
|
|
|
&user.DefaultHomePage,
|
2022-10-25 23:00:56 -04:00
|
|
|
&user.CategoriesSortingOrder,
|
2017-11-25 13:40:23 -05:00
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2020-12-22 18:33:15 -05:00
|
|
|
tx.Rollback()
|
2021-01-04 00:20:21 -05:00
|
|
|
return nil, fmt.Errorf(`store: unable to create user: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:33:15 -05:00
|
|
|
_, err = tx.Exec(`INSERT INTO categories (user_id, title) VALUES ($1, $2)`, user.ID, "All")
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2021-01-04 00:20:21 -05:00
|
|
|
return nil, fmt.Errorf(`store: unable to create user default category: %v`, err)
|
2020-12-22 18:33:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(`INSERT INTO integrations (user_id) VALUES ($1)`, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2021-01-04 00:20:21 -05:00
|
|
|
return nil, fmt.Errorf(`store: unable to create integration row: %v`, err)
|
2020-12-22 18:33:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2021-01-04 00:20:21 -05:00
|
|
|
return nil, fmt.Errorf(`store: unable to commit transaction: %v`, err)
|
2020-12-22 18:33:15 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:20:21 -05:00
|
|
|
return &user, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// UpdateUser updates a user.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) UpdateUser(user *model.User) error {
|
|
|
|
if user.Password != "" {
|
|
|
|
hashedPassword, err := hashPassword(user.Password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-28 21:20:46 -04:00
|
|
|
query := `
|
|
|
|
UPDATE users SET
|
|
|
|
username=LOWER($1),
|
|
|
|
password=$2,
|
|
|
|
is_admin=$3,
|
|
|
|
theme=$4,
|
|
|
|
language=$5,
|
|
|
|
timezone=$6,
|
|
|
|
entry_direction=$7,
|
2020-07-08 19:24:54 -04:00
|
|
|
entries_per_page=$8,
|
2020-07-16 22:46:24 -04:00
|
|
|
keyboard_shortcuts=$9,
|
2020-09-27 10:41:04 -04:00
|
|
|
show_reading_time=$10,
|
2020-12-22 00:14:10 -05:00
|
|
|
entry_swipe=$11,
|
2023-01-14 19:49:21 -05:00
|
|
|
double_tap=$12,
|
|
|
|
stylesheet=$13,
|
|
|
|
google_id=$14,
|
|
|
|
openid_connect_id=$15,
|
|
|
|
display_mode=$16,
|
|
|
|
entry_order=$17,
|
|
|
|
default_reading_speed=$18,
|
|
|
|
cjk_reading_speed=$19,
|
|
|
|
default_home_page=$20,
|
|
|
|
categories_sorting_order=$21
|
2019-04-28 21:20:46 -04:00
|
|
|
WHERE
|
2023-01-14 19:49:21 -05:00
|
|
|
id=$22
|
2019-04-28 21:20:46 -04:00
|
|
|
`
|
2017-12-02 20:04:01 -05:00
|
|
|
|
|
|
|
_, err = s.db.Exec(
|
|
|
|
query,
|
|
|
|
user.Username,
|
|
|
|
hashedPassword,
|
|
|
|
user.IsAdmin,
|
|
|
|
user.Theme,
|
|
|
|
user.Language,
|
|
|
|
user.Timezone,
|
|
|
|
user.EntryDirection,
|
2020-07-08 19:24:54 -04:00
|
|
|
user.EntriesPerPage,
|
2019-04-28 21:20:46 -04:00
|
|
|
user.KeyboardShortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
user.ShowReadingTime,
|
2020-09-27 10:41:04 -04:00
|
|
|
user.EntrySwipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
user.DoubleTap,
|
2020-12-22 00:14:10 -05:00
|
|
|
user.Stylesheet,
|
|
|
|
user.GoogleID,
|
|
|
|
user.OpenIDConnectID,
|
2021-02-28 16:29:51 -05:00
|
|
|
user.DisplayMode,
|
2021-05-14 07:51:51 -04:00
|
|
|
user.EntryOrder,
|
2021-08-30 10:53:05 -04:00
|
|
|
user.DefaultReadingSpeed,
|
|
|
|
user.CJKReadingSpeed,
|
2022-07-20 16:07:55 -04:00
|
|
|
user.DefaultHomePage,
|
2022-10-25 23:00:56 -04:00
|
|
|
user.CategoriesSortingOrder,
|
2017-12-02 20:04:01 -05:00
|
|
|
user.ID,
|
|
|
|
)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to update user: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-28 21:20:46 -04:00
|
|
|
query := `
|
|
|
|
UPDATE users SET
|
|
|
|
username=LOWER($1),
|
|
|
|
is_admin=$2,
|
|
|
|
theme=$3,
|
|
|
|
language=$4,
|
|
|
|
timezone=$5,
|
|
|
|
entry_direction=$6,
|
2020-07-08 19:24:54 -04:00
|
|
|
entries_per_page=$7,
|
2020-07-16 22:46:24 -04:00
|
|
|
keyboard_shortcuts=$8,
|
2020-09-27 10:41:04 -04:00
|
|
|
show_reading_time=$9,
|
2020-12-22 00:14:10 -05:00
|
|
|
entry_swipe=$10,
|
2023-01-14 19:49:21 -05:00
|
|
|
double_tap=$11,
|
|
|
|
stylesheet=$12,
|
|
|
|
google_id=$13,
|
|
|
|
openid_connect_id=$14,
|
|
|
|
display_mode=$15,
|
|
|
|
entry_order=$16,
|
|
|
|
default_reading_speed=$17,
|
|
|
|
cjk_reading_speed=$18,
|
|
|
|
default_home_page=$19,
|
|
|
|
categories_sorting_order=$20
|
2019-04-28 21:20:46 -04:00
|
|
|
WHERE
|
2023-01-14 19:49:21 -05:00
|
|
|
id=$21
|
2019-04-28 21:20:46 -04:00
|
|
|
`
|
2017-12-02 20:04:01 -05:00
|
|
|
|
|
|
|
_, err := s.db.Exec(
|
|
|
|
query,
|
|
|
|
user.Username,
|
|
|
|
user.IsAdmin,
|
|
|
|
user.Theme,
|
|
|
|
user.Language,
|
|
|
|
user.Timezone,
|
|
|
|
user.EntryDirection,
|
2020-07-08 19:24:54 -04:00
|
|
|
user.EntriesPerPage,
|
2019-04-28 21:20:46 -04:00
|
|
|
user.KeyboardShortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
user.ShowReadingTime,
|
2020-09-27 10:41:04 -04:00
|
|
|
user.EntrySwipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
user.DoubleTap,
|
2020-12-22 00:14:10 -05:00
|
|
|
user.Stylesheet,
|
|
|
|
user.GoogleID,
|
|
|
|
user.OpenIDConnectID,
|
2021-02-28 16:29:51 -05:00
|
|
|
user.DisplayMode,
|
2021-05-14 07:51:51 -04:00
|
|
|
user.EntryOrder,
|
2021-08-30 10:53:05 -04:00
|
|
|
user.DefaultReadingSpeed,
|
|
|
|
user.CJKReadingSpeed,
|
2022-07-20 16:07:55 -04:00
|
|
|
user.DefaultHomePage,
|
2022-10-25 23:00:56 -04:00
|
|
|
user.CategoriesSortingOrder,
|
2017-12-02 20:04:01 -05:00
|
|
|
user.ID,
|
|
|
|
)
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to update user: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2020-04-25 23:18:10 -04:00
|
|
|
}
|
2020-03-30 19:54:02 -04:00
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:08:32 -05:00
|
|
|
// UserLanguage returns the language of the given user.
|
2018-10-15 00:43:48 -04:00
|
|
|
func (s *Storage) UserLanguage(userID int64) (language string) {
|
|
|
|
err := s.db.QueryRow(`SELECT language FROM users WHERE id = $1`, userID).Scan(&language)
|
|
|
|
if err != nil {
|
|
|
|
return "en_US"
|
2018-02-28 00:08:32 -05:00
|
|
|
}
|
2018-10-15 00:43:48 -04:00
|
|
|
|
|
|
|
return language
|
2018-02-28 00:08:32 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// UserByID finds a user by the ID.
|
|
|
|
func (s *Storage) UserByID(userID int64) (*model.User, error) {
|
2019-04-28 21:20:46 -04:00
|
|
|
query := `
|
|
|
|
SELECT
|
2019-10-30 01:48:07 -04:00
|
|
|
id,
|
|
|
|
username,
|
|
|
|
is_admin,
|
|
|
|
theme,
|
|
|
|
language,
|
|
|
|
timezone,
|
|
|
|
entry_direction,
|
2020-07-08 19:24:54 -04:00
|
|
|
entries_per_page,
|
2019-10-30 01:48:07 -04:00
|
|
|
keyboard_shortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
show_reading_time,
|
2020-09-27 10:41:04 -04:00
|
|
|
entry_swipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
double_tap,
|
2019-10-30 01:48:07 -04:00
|
|
|
last_login_at,
|
2020-12-22 00:14:10 -05:00
|
|
|
stylesheet,
|
|
|
|
google_id,
|
2021-02-28 16:29:51 -05:00
|
|
|
openid_connect_id,
|
2021-05-14 07:51:51 -04:00
|
|
|
display_mode,
|
2021-08-30 10:53:05 -04:00
|
|
|
entry_order,
|
|
|
|
default_reading_speed,
|
2022-07-20 16:07:55 -04:00
|
|
|
cjk_reading_speed,
|
2022-10-16 01:36:59 -04:00
|
|
|
default_home_page,
|
2022-10-25 23:00:56 -04:00
|
|
|
categories_sorting_order
|
2019-04-28 21:20:46 -04:00
|
|
|
FROM
|
|
|
|
users
|
|
|
|
WHERE
|
|
|
|
id = $1
|
|
|
|
`
|
2017-12-29 16:53:02 -05:00
|
|
|
return s.fetchUser(query, userID)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// UserByUsername finds a user by the username.
|
|
|
|
func (s *Storage) UserByUsername(username string) (*model.User, error) {
|
2019-04-28 21:20:46 -04:00
|
|
|
query := `
|
|
|
|
SELECT
|
2019-10-30 01:48:07 -04:00
|
|
|
id,
|
|
|
|
username,
|
|
|
|
is_admin,
|
|
|
|
theme,
|
|
|
|
language,
|
|
|
|
timezone,
|
|
|
|
entry_direction,
|
2020-07-08 19:24:54 -04:00
|
|
|
entries_per_page,
|
2019-10-30 01:48:07 -04:00
|
|
|
keyboard_shortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
show_reading_time,
|
2020-09-27 10:41:04 -04:00
|
|
|
entry_swipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
double_tap,
|
2019-10-30 01:48:07 -04:00
|
|
|
last_login_at,
|
2020-12-22 00:14:10 -05:00
|
|
|
stylesheet,
|
|
|
|
google_id,
|
2021-02-28 16:29:51 -05:00
|
|
|
openid_connect_id,
|
2021-05-14 07:51:51 -04:00
|
|
|
display_mode,
|
2021-08-30 10:53:05 -04:00
|
|
|
entry_order,
|
|
|
|
default_reading_speed,
|
2022-07-20 16:07:55 -04:00
|
|
|
cjk_reading_speed,
|
2022-10-16 01:36:59 -04:00
|
|
|
default_home_page,
|
2022-10-25 23:00:56 -04:00
|
|
|
categories_sorting_order
|
2019-04-28 21:20:46 -04:00
|
|
|
FROM
|
|
|
|
users
|
|
|
|
WHERE
|
|
|
|
username=LOWER($1)
|
|
|
|
`
|
2017-12-29 16:53:02 -05:00
|
|
|
return s.fetchUser(query, username)
|
2017-11-23 01:22:33 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:14:10 -05:00
|
|
|
// UserByField finds a user by a field value.
|
|
|
|
func (s *Storage) UserByField(field, value string) (*model.User, error) {
|
2019-04-28 21:20:46 -04:00
|
|
|
query := `
|
|
|
|
SELECT
|
2019-10-30 01:48:07 -04:00
|
|
|
id,
|
|
|
|
username,
|
|
|
|
is_admin,
|
|
|
|
theme,
|
|
|
|
language,
|
|
|
|
timezone,
|
|
|
|
entry_direction,
|
2020-07-08 19:24:54 -04:00
|
|
|
entries_per_page,
|
2019-10-30 01:48:07 -04:00
|
|
|
keyboard_shortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
show_reading_time,
|
2020-09-27 10:41:04 -04:00
|
|
|
entry_swipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
double_tap,
|
2019-10-30 01:48:07 -04:00
|
|
|
last_login_at,
|
2020-12-22 00:14:10 -05:00
|
|
|
stylesheet,
|
|
|
|
google_id,
|
2021-02-28 16:29:51 -05:00
|
|
|
openid_connect_id,
|
2021-05-14 07:51:51 -04:00
|
|
|
display_mode,
|
2021-08-30 10:53:05 -04:00
|
|
|
entry_order,
|
|
|
|
default_reading_speed,
|
2022-07-20 16:07:55 -04:00
|
|
|
cjk_reading_speed,
|
2022-10-16 01:36:59 -04:00
|
|
|
default_home_page,
|
2022-10-25 23:00:56 -04:00
|
|
|
categories_sorting_order
|
2019-04-28 21:20:46 -04:00
|
|
|
FROM
|
|
|
|
users
|
|
|
|
WHERE
|
2020-12-22 00:14:10 -05:00
|
|
|
%s=$1
|
2019-04-28 21:20:46 -04:00
|
|
|
`
|
2020-12-22 00:14:10 -05:00
|
|
|
return s.fetchUser(fmt.Sprintf(query, pq.QuoteIdentifier(field)), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnotherUserWithFieldExists returns true if a user has the value set for the given field.
|
|
|
|
func (s *Storage) AnotherUserWithFieldExists(userID int64, field, value string) bool {
|
|
|
|
var result bool
|
|
|
|
s.db.QueryRow(fmt.Sprintf(`SELECT true FROM users WHERE id <> $1 AND %s=$2`, pq.QuoteIdentifier(field)), userID, value).Scan(&result)
|
|
|
|
return result
|
2017-12-29 16:53:02 -05:00
|
|
|
}
|
|
|
|
|
2020-03-01 20:38:29 -05:00
|
|
|
// UserByAPIKey returns a User from an API Key.
|
|
|
|
func (s *Storage) UserByAPIKey(token string) (*model.User, error) {
|
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
u.id,
|
|
|
|
u.username,
|
|
|
|
u.is_admin,
|
|
|
|
u.theme,
|
|
|
|
u.language,
|
|
|
|
u.timezone,
|
|
|
|
u.entry_direction,
|
2020-07-08 19:24:54 -04:00
|
|
|
u.entries_per_page,
|
2020-03-01 20:38:29 -05:00
|
|
|
u.keyboard_shortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
u.show_reading_time,
|
2020-09-27 10:41:04 -04:00
|
|
|
u.entry_swipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
u.double_tap,
|
2020-03-01 20:38:29 -05:00
|
|
|
u.last_login_at,
|
2020-12-22 00:14:10 -05:00
|
|
|
u.stylesheet,
|
|
|
|
u.google_id,
|
2021-02-28 16:29:51 -05:00
|
|
|
u.openid_connect_id,
|
2021-05-14 07:51:51 -04:00
|
|
|
u.display_mode,
|
2021-08-30 10:53:05 -04:00
|
|
|
u.entry_order,
|
|
|
|
u.default_reading_speed,
|
2022-07-20 16:07:55 -04:00
|
|
|
u.cjk_reading_speed,
|
2022-10-16 01:36:59 -04:00
|
|
|
u.default_home_page,
|
2022-10-25 23:00:56 -04:00
|
|
|
u.categories_sorting_order
|
2020-03-01 20:38:29 -05:00
|
|
|
FROM
|
|
|
|
users u
|
|
|
|
LEFT JOIN
|
|
|
|
api_keys ON api_keys.user_id=u.id
|
|
|
|
WHERE
|
|
|
|
api_keys.token = $1
|
|
|
|
`
|
|
|
|
return s.fetchUser(query, token)
|
|
|
|
}
|
|
|
|
|
2017-12-29 16:53:02 -05:00
|
|
|
func (s *Storage) fetchUser(query string, args ...interface{}) (*model.User, error) {
|
2021-01-04 00:20:21 -05:00
|
|
|
var user model.User
|
2017-12-29 16:53:02 -05:00
|
|
|
err := s.db.QueryRow(query, args...).Scan(
|
2017-12-02 20:04:01 -05:00
|
|
|
&user.ID,
|
|
|
|
&user.Username,
|
|
|
|
&user.IsAdmin,
|
|
|
|
&user.Theme,
|
|
|
|
&user.Language,
|
|
|
|
&user.Timezone,
|
|
|
|
&user.EntryDirection,
|
2020-07-08 19:24:54 -04:00
|
|
|
&user.EntriesPerPage,
|
2019-04-28 21:20:46 -04:00
|
|
|
&user.KeyboardShortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
&user.ShowReadingTime,
|
2020-09-27 10:41:04 -04:00
|
|
|
&user.EntrySwipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
&user.DoubleTap,
|
2017-12-29 16:53:02 -05:00
|
|
|
&user.LastLoginAt,
|
2020-12-22 00:14:10 -05:00
|
|
|
&user.Stylesheet,
|
|
|
|
&user.GoogleID,
|
|
|
|
&user.OpenIDConnectID,
|
2021-02-28 16:29:51 -05:00
|
|
|
&user.DisplayMode,
|
2021-05-14 07:51:51 -04:00
|
|
|
&user.EntryOrder,
|
2021-08-30 10:53:05 -04:00
|
|
|
&user.DefaultReadingSpeed,
|
|
|
|
&user.CJKReadingSpeed,
|
2022-07-20 16:07:55 -04:00
|
|
|
&user.DefaultHomePage,
|
2022-10-25 23:00:56 -04:00
|
|
|
&user.CategoriesSortingOrder,
|
2017-12-02 20:04:01 -05:00
|
|
|
)
|
2017-12-29 16:53:02 -05:00
|
|
|
|
2017-11-23 01:22:33 -05:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
} else if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch user: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:20:21 -05:00
|
|
|
return &user, nil
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// RemoveUser deletes a user.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) RemoveUser(userID int64) error {
|
2020-12-22 18:33:15 -05:00
|
|
|
tx, err := s.db.Begin()
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2019-11-18 01:13:16 -05:00
|
|
|
return fmt.Errorf(`store: unable to start transaction: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:33:15 -05:00
|
|
|
if _, err := tx.Exec(`DELETE FROM users WHERE id=$1`, userID); err != nil {
|
|
|
|
tx.Rollback()
|
2019-11-18 01:13:16 -05:00
|
|
|
return fmt.Errorf(`store: unable to remove user #%d: %v`, userID, err)
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:33:15 -05:00
|
|
|
if _, err := tx.Exec(`DELETE FROM integrations WHERE user_id=$1`, userID); err != nil {
|
|
|
|
tx.Rollback()
|
2019-11-18 01:13:16 -05:00
|
|
|
return fmt.Errorf(`store: unable to remove integration settings for user #%d: %v`, userID, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:33:15 -05:00
|
|
|
if err := tx.Commit(); err != nil {
|
2019-11-18 01:13:16 -05:00
|
|
|
return fmt.Errorf(`store: unable to commit transaction: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:28:02 -04:00
|
|
|
// RemoveUserAsync deletes user data without locking the database.
|
|
|
|
func (s *Storage) RemoveUserAsync(userID int64) {
|
|
|
|
go func() {
|
2021-03-17 01:15:40 -04:00
|
|
|
if err := s.deleteUserFeeds(userID); err != nil {
|
|
|
|
logger.Error(`%v`, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-28 23:28:02 -04:00
|
|
|
s.db.Exec(`DELETE FROM users WHERE id=$1`, userID)
|
|
|
|
s.db.Exec(`DELETE FROM integrations WHERE user_id=$1`, userID)
|
2021-03-17 01:15:40 -04:00
|
|
|
|
2020-12-23 00:02:10 -05:00
|
|
|
logger.Debug(`[MASS DELETE] User #%d has been deleted (%d GoRoutines)`, userID, runtime.NumGoroutine())
|
2020-07-28 23:28:02 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-03-17 01:15:40 -04:00
|
|
|
func (s *Storage) deleteUserFeeds(userID int64) error {
|
|
|
|
rows, err := s.db.Query(`SELECT id FROM feeds WHERE user_id=$1`, userID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`store: unable to get user feeds: %v`, err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var feedID int64
|
|
|
|
rows.Scan(&feedID)
|
|
|
|
|
|
|
|
logger.Debug(`[USER DELETION] Deleting feed #%d for user #%d (%d GoRoutines)`, feedID, userID, runtime.NumGoroutine())
|
|
|
|
|
|
|
|
if err := s.RemoveFeed(userID, feedID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// Users returns all users.
|
|
|
|
func (s *Storage) Users() (model.Users, error) {
|
2017-12-29 16:53:02 -05:00
|
|
|
query := `
|
|
|
|
SELECT
|
2019-10-30 01:48:07 -04:00
|
|
|
id,
|
|
|
|
username,
|
|
|
|
is_admin,
|
|
|
|
theme,
|
|
|
|
language,
|
|
|
|
timezone,
|
|
|
|
entry_direction,
|
2020-07-08 19:24:54 -04:00
|
|
|
entries_per_page,
|
2019-10-30 01:48:07 -04:00
|
|
|
keyboard_shortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
show_reading_time,
|
2020-09-27 10:41:04 -04:00
|
|
|
entry_swipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
double_tap,
|
2019-10-30 01:48:07 -04:00
|
|
|
last_login_at,
|
2020-12-22 00:14:10 -05:00
|
|
|
stylesheet,
|
|
|
|
google_id,
|
2021-02-28 16:29:51 -05:00
|
|
|
openid_connect_id,
|
2021-05-14 07:51:51 -04:00
|
|
|
display_mode,
|
2021-08-30 10:53:05 -04:00
|
|
|
entry_order,
|
|
|
|
default_reading_speed,
|
2022-07-20 16:07:55 -04:00
|
|
|
cjk_reading_speed,
|
2022-10-16 01:36:59 -04:00
|
|
|
default_home_page,
|
2022-10-25 23:00:56 -04:00
|
|
|
categories_sorting_order
|
2019-04-28 21:20:46 -04:00
|
|
|
FROM
|
|
|
|
users
|
|
|
|
ORDER BY username ASC
|
|
|
|
`
|
2017-12-02 20:04:01 -05:00
|
|
|
rows, err := s.db.Query(query)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch users: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2017-12-02 20:04:01 -05:00
|
|
|
var users model.Users
|
2017-11-20 00:10:04 -05:00
|
|
|
for rows.Next() {
|
2021-01-04 00:20:21 -05:00
|
|
|
var user model.User
|
2017-11-20 00:10:04 -05:00
|
|
|
err := rows.Scan(
|
|
|
|
&user.ID,
|
|
|
|
&user.Username,
|
|
|
|
&user.IsAdmin,
|
|
|
|
&user.Theme,
|
|
|
|
&user.Language,
|
|
|
|
&user.Timezone,
|
2017-12-29 16:53:02 -05:00
|
|
|
&user.EntryDirection,
|
2020-07-08 19:24:54 -04:00
|
|
|
&user.EntriesPerPage,
|
2019-04-28 21:20:46 -04:00
|
|
|
&user.KeyboardShortcuts,
|
2020-07-16 22:46:24 -04:00
|
|
|
&user.ShowReadingTime,
|
2020-09-27 10:41:04 -04:00
|
|
|
&user.EntrySwipe,
|
2023-01-14 19:49:21 -05:00
|
|
|
&user.DoubleTap,
|
2017-11-20 00:10:04 -05:00
|
|
|
&user.LastLoginAt,
|
2020-12-22 00:14:10 -05:00
|
|
|
&user.Stylesheet,
|
|
|
|
&user.GoogleID,
|
|
|
|
&user.OpenIDConnectID,
|
2021-02-28 16:29:51 -05:00
|
|
|
&user.DisplayMode,
|
2021-05-14 07:51:51 -04:00
|
|
|
&user.EntryOrder,
|
2021-08-30 10:53:05 -04:00
|
|
|
&user.DefaultReadingSpeed,
|
|
|
|
&user.CJKReadingSpeed,
|
2022-07-20 16:07:55 -04:00
|
|
|
&user.DefaultHomePage,
|
2022-10-25 23:00:56 -04:00
|
|
|
&user.CategoriesSortingOrder,
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return nil, fmt.Errorf(`store: unable to fetch users row: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:20:21 -05:00
|
|
|
users = append(users, &user)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 00:30:04 -05:00
|
|
|
// CheckPassword validate the hashed password.
|
2017-11-20 00:10:04 -05:00
|
|
|
func (s *Storage) CheckPassword(username, password string) error {
|
|
|
|
var hash string
|
|
|
|
username = strings.ToLower(username)
|
|
|
|
|
|
|
|
err := s.db.QueryRow("SELECT password FROM users WHERE username=$1", username).Scan(&hash)
|
|
|
|
if err == sql.ErrNoRows {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to find this user: %s`, username)
|
2017-11-20 00:10:04 -05:00
|
|
|
} else if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: unable to fetch user: %v`, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return fmt.Errorf(`store: invalid password for "%s" (%v)`, username, err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-29 20:04:23 -04:00
|
|
|
// HasPassword returns true if the given user has a password defined.
|
|
|
|
func (s *Storage) HasPassword(userID int64) (bool, error) {
|
|
|
|
var result bool
|
|
|
|
query := `SELECT true FROM users WHERE id=$1 AND password <> ''`
|
|
|
|
|
|
|
|
err := s.db.QueryRow(query, userID).Scan(&result)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
2019-10-30 01:48:07 -04:00
|
|
|
return false, fmt.Errorf(`store: unable to execute query: %v`, err)
|
2018-04-29 20:04:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if result {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
func hashPassword(password string) (string, error) {
|
|
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
return string(bytes), err
|
|
|
|
}
|