2021-12-08 18:43:14 +03:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2022-05-21 19:59:22 +03:00
|
|
|
"database/sql"
|
2021-12-08 18:43:14 +03:00
|
|
|
"fmt"
|
2021-12-09 17:02:57 +03:00
|
|
|
"strings"
|
2022-06-27 17:09:06 +03:00
|
|
|
|
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2021-12-08 18:43:14 +03:00
|
|
|
)
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
// userRaw is the store model for an User.
|
|
|
|
// Fields have exactly the same meanings as User.
|
|
|
|
type userRaw struct {
|
|
|
|
ID int
|
|
|
|
|
|
|
|
// Standard fields
|
|
|
|
RowStatus api.RowStatus
|
|
|
|
CreatedTs int64
|
|
|
|
UpdatedTs int64
|
|
|
|
|
|
|
|
// Domain specific fields
|
|
|
|
Email string
|
|
|
|
Role api.Role
|
|
|
|
Name string
|
|
|
|
PasswordHash string
|
|
|
|
OpenID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (raw *userRaw) toUser() *api.User {
|
|
|
|
return &api.User{
|
|
|
|
ID: raw.ID,
|
|
|
|
|
|
|
|
RowStatus: raw.RowStatus,
|
|
|
|
CreatedTs: raw.CreatedTs,
|
|
|
|
UpdatedTs: raw.UpdatedTs,
|
|
|
|
|
|
|
|
Email: raw.Email,
|
|
|
|
Role: raw.Role,
|
|
|
|
Name: raw.Name,
|
|
|
|
PasswordHash: raw.PasswordHash,
|
|
|
|
OpenID: raw.OpenID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 02:37:23 +03:00
|
|
|
func (s *Store) CreateUser(create *api.UserCreate) (*api.User, error) {
|
2022-05-19 13:32:04 +03:00
|
|
|
userRaw, err := createUser(s.db, create)
|
2021-12-15 05:55:17 +03:00
|
|
|
if err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return nil, err
|
2021-12-15 05:55:17 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
user := userRaw.toUser()
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return user, nil
|
|
|
|
}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-05-16 02:37:23 +03:00
|
|
|
func (s *Store) PatchUser(patch *api.UserPatch) (*api.User, error) {
|
2022-05-19 13:32:04 +03:00
|
|
|
userRaw, err := patchUser(s.db, patch)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
user := userRaw.toUser()
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
return user, nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-05-16 17:19:39 +03:00
|
|
|
func (s *Store) FindUserList(find *api.UserFind) ([]*api.User, error) {
|
2022-05-19 13:32:04 +03:00
|
|
|
userRawList, err := findUserList(s.db, find)
|
2022-05-16 17:19:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
list := []*api.User{}
|
|
|
|
for _, raw := range userRawList {
|
|
|
|
list = append(list, raw.toUser())
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:19:39 +03:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2022-05-16 02:37:23 +03:00
|
|
|
func (s *Store) FindUser(find *api.UserFind) (*api.User, error) {
|
2022-02-03 10:32:03 +03:00
|
|
|
list, err := findUserList(s.db, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-14 15:40:24 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
} else if len(list) > 1 {
|
|
|
|
return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d users with filter %+v, expect 1. ", len(list), find)}
|
|
|
|
}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
user := list[0].toUser()
|
|
|
|
|
|
|
|
return user, nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-07-26 16:41:20 +03:00
|
|
|
func (s *Store) DeleteUser(delete *api.UserDelete) error {
|
|
|
|
err := deleteUser(s.db, delete)
|
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-21 19:59:22 +03:00
|
|
|
func createUser(db *sql.DB, create *api.UserCreate) (*userRaw, error) {
|
|
|
|
row, err := db.Query(`
|
2022-02-03 10:32:03 +03:00
|
|
|
INSERT INTO user (
|
2022-05-15 05:57:54 +03:00
|
|
|
email,
|
|
|
|
role,
|
2022-02-03 10:32:03 +03:00
|
|
|
name,
|
2022-02-06 11:19:20 +03:00
|
|
|
password_hash,
|
2022-02-03 10:32:03 +03:00
|
|
|
open_id
|
|
|
|
)
|
2022-05-15 05:57:54 +03:00
|
|
|
VALUES (?, ?, ?, ?, ?)
|
2022-07-08 17:16:18 +03:00
|
|
|
RETURNING id, email, role, name, password_hash, open_id, created_ts, updated_ts, row_status
|
2022-02-04 16:24:21 +03:00
|
|
|
`,
|
2022-05-15 05:57:54 +03:00
|
|
|
create.Email,
|
|
|
|
create.Role,
|
2022-02-04 16:24:21 +03:00
|
|
|
create.Name,
|
2022-02-06 11:19:20 +03:00
|
|
|
create.PasswordHash,
|
2022-05-02 21:05:43 +03:00
|
|
|
create.OpenID,
|
2022-02-04 16:24:21 +03:00
|
|
|
)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer row.Close()
|
|
|
|
|
|
|
|
row.Next()
|
2022-05-19 13:32:04 +03:00
|
|
|
var userRaw userRaw
|
2022-02-03 10:32:03 +03:00
|
|
|
if err := row.Scan(
|
2022-05-19 13:32:04 +03:00
|
|
|
&userRaw.ID,
|
|
|
|
&userRaw.Email,
|
|
|
|
&userRaw.Role,
|
|
|
|
&userRaw.Name,
|
|
|
|
&userRaw.PasswordHash,
|
|
|
|
&userRaw.OpenID,
|
|
|
|
&userRaw.CreatedTs,
|
|
|
|
&userRaw.UpdatedTs,
|
2022-07-08 17:16:18 +03:00
|
|
|
&userRaw.RowStatus,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
return &userRaw, nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-05-21 19:59:22 +03:00
|
|
|
func patchUser(db *sql.DB, patch *api.UserPatch) (*userRaw, error) {
|
2022-02-03 10:32:03 +03:00
|
|
|
set, args := []string{}, []interface{}{}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
if v := patch.RowStatus; v != nil {
|
|
|
|
set, args = append(set, "row_status = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-15 05:57:54 +03:00
|
|
|
if v := patch.Email; v != nil {
|
2022-05-19 13:32:04 +03:00
|
|
|
set, args = append(set, "email = ?"), append(args, *v)
|
2022-05-15 05:57:54 +03:00
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
if v := patch.Name; v != nil {
|
2022-05-19 13:32:04 +03:00
|
|
|
set, args = append(set, "name = ?"), append(args, *v)
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
2022-02-06 11:19:20 +03:00
|
|
|
if v := patch.PasswordHash; v != nil {
|
2022-05-19 13:32:04 +03:00
|
|
|
set, args = append(set, "password_hash = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-05-02 21:05:43 +03:00
|
|
|
if v := patch.OpenID; v != nil {
|
2022-05-19 13:32:04 +03:00
|
|
|
set, args = append(set, "open_id = ?"), append(args, *v)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-02-04 16:24:21 +03:00
|
|
|
|
2022-05-02 21:05:43 +03:00
|
|
|
args = append(args, patch.ID)
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-05-21 19:59:22 +03:00
|
|
|
row, err := db.Query(`
|
2022-02-03 10:32:03 +03:00
|
|
|
UPDATE user
|
|
|
|
SET `+strings.Join(set, ", ")+`
|
|
|
|
WHERE id = ?
|
2022-07-08 17:16:18 +03:00
|
|
|
RETURNING id, email, role, name, password_hash, open_id, created_ts, updated_ts, row_status
|
2022-02-03 10:32:03 +03:00
|
|
|
`, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer row.Close()
|
|
|
|
|
|
|
|
if row.Next() {
|
2022-05-19 13:32:04 +03:00
|
|
|
var userRaw userRaw
|
2022-02-03 10:32:03 +03:00
|
|
|
if err := row.Scan(
|
2022-05-19 13:32:04 +03:00
|
|
|
&userRaw.ID,
|
|
|
|
&userRaw.Email,
|
|
|
|
&userRaw.Role,
|
|
|
|
&userRaw.Name,
|
|
|
|
&userRaw.PasswordHash,
|
|
|
|
&userRaw.OpenID,
|
|
|
|
&userRaw.CreatedTs,
|
|
|
|
&userRaw.UpdatedTs,
|
2022-07-08 17:16:18 +03:00
|
|
|
&userRaw.RowStatus,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
return &userRaw, nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
2021-12-15 05:55:17 +03:00
|
|
|
|
2022-05-02 21:05:43 +03:00
|
|
|
return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("user ID not found: %d", patch.ID)}
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-05-21 19:59:22 +03:00
|
|
|
func findUserList(db *sql.DB, find *api.UserFind) ([]*userRaw, error) {
|
2022-02-03 19:56:44 +03:00
|
|
|
where, args := []string{"1 = 1"}, []interface{}{}
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-05-02 21:05:43 +03:00
|
|
|
if v := find.ID; v != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
where, args = append(where, "id = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-15 05:57:54 +03:00
|
|
|
if v := find.Role; v != nil {
|
|
|
|
where, args = append(where, "role = ?"), append(args, *v)
|
|
|
|
}
|
|
|
|
if v := find.Email; v != nil {
|
|
|
|
where, args = append(where, "email = ?"), append(args, *v)
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
if v := find.Name; v != nil {
|
|
|
|
where, args = append(where, "name = ?"), append(args, *v)
|
|
|
|
}
|
2022-05-02 21:05:43 +03:00
|
|
|
if v := find.OpenID; v != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
where, args = append(where, "open_id = ?"), append(args, *v)
|
|
|
|
}
|
2021-12-08 18:43:14 +03:00
|
|
|
|
2022-05-21 19:59:22 +03:00
|
|
|
rows, err := db.Query(`
|
2022-02-03 10:32:03 +03:00
|
|
|
SELECT
|
|
|
|
id,
|
2022-05-15 05:57:54 +03:00
|
|
|
email,
|
|
|
|
role,
|
2022-02-03 10:32:03 +03:00
|
|
|
name,
|
2022-02-06 11:19:20 +03:00
|
|
|
password_hash,
|
2022-02-03 10:32:03 +03:00
|
|
|
open_id,
|
2022-02-03 19:56:44 +03:00
|
|
|
created_ts,
|
2022-07-08 17:16:18 +03:00
|
|
|
updated_ts,
|
|
|
|
row_status
|
2022-02-03 10:32:03 +03:00
|
|
|
FROM user
|
2022-05-22 07:35:57 +03:00
|
|
|
WHERE `+strings.Join(where, " AND ")+`
|
2022-07-26 16:41:20 +03:00
|
|
|
ORDER BY created_ts DESC, row_status DESC`,
|
2022-02-03 10:32:03 +03:00
|
|
|
args...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
userRawList := make([]*userRaw, 0)
|
2022-02-03 10:32:03 +03:00
|
|
|
for rows.Next() {
|
2022-05-19 13:32:04 +03:00
|
|
|
var userRaw userRaw
|
2022-02-03 10:32:03 +03:00
|
|
|
if err := rows.Scan(
|
2022-05-19 13:32:04 +03:00
|
|
|
&userRaw.ID,
|
|
|
|
&userRaw.Email,
|
|
|
|
&userRaw.Role,
|
|
|
|
&userRaw.Name,
|
|
|
|
&userRaw.PasswordHash,
|
|
|
|
&userRaw.OpenID,
|
|
|
|
&userRaw.CreatedTs,
|
|
|
|
&userRaw.UpdatedTs,
|
2022-07-08 17:16:18 +03:00
|
|
|
&userRaw.RowStatus,
|
2022-02-03 10:32:03 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, FormatError(err)
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
userRawList = append(userRawList, &userRaw)
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, FormatError(err)
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
return userRawList, nil
|
2021-12-08 18:43:14 +03:00
|
|
|
}
|
2022-07-26 16:41:20 +03:00
|
|
|
|
|
|
|
func deleteUser(db *sql.DB, delete *api.UserDelete) error {
|
2022-07-26 17:32:26 +03:00
|
|
|
result, err := db.Exec(`
|
|
|
|
PRAGMA foreign_keys = ON;
|
|
|
|
DELETE FROM user WHERE id = ?
|
|
|
|
`, delete.ID)
|
2022-07-26 16:41:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
if rows == 0 {
|
|
|
|
return &common.Error{Code: common.NotFound, Err: fmt.Errorf("user ID not found: %d", delete.ID)}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|