focalboard/server/services/store/sqlstore/user.go

237 lines
4.8 KiB
Go
Raw Normal View History

2020-10-28 14:35:41 +01:00
package sqlstore
import (
"database/sql"
2020-11-06 16:46:35 +01:00
"encoding/json"
"fmt"
"log"
2020-10-28 14:35:41 +01:00
2021-01-26 23:13:46 +01:00
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/utils"
2020-10-28 14:35:41 +01:00
sq "github.com/Masterminds/squirrel"
)
type UserNotFoundError struct {
id string
}
func (unf UserNotFoundError) Error() string {
return fmt.Sprintf("user not found (%s)", unf.id)
}
func (s *SQLStore) getRegisteredUserCount(db sq.BaseRunner) (int, error) {
query := s.getQueryBuilder(db).
2021-01-14 01:56:01 +01:00
Select("count(*)").
2021-04-17 09:06:57 +02:00
From(s.tablePrefix + "users").
2021-01-14 01:56:01 +01:00
Where(sq.Eq{"delete_at": 0})
row := query.QueryRow()
var count int
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func (s *SQLStore) getUserByCondition(db sq.BaseRunner, condition sq.Eq) (*model.User, error) {
users, err := s.getUsersByCondition(db, condition)
if err != nil {
return nil, err
}
if len(users) == 0 {
return nil, nil
}
return users[0], nil
}
func (s *SQLStore) getUsersByCondition(db sq.BaseRunner, condition sq.Eq) ([]*model.User, error) {
query := s.getQueryBuilder(db).
Select(
"id",
"username",
"email",
"password",
"mfa_secret",
"auth_service",
"auth_data",
"props",
"create_at",
"update_at",
"delete_at",
).
2021-04-17 09:06:57 +02:00
From(s.tablePrefix + "users").
2020-12-07 20:40:16 +01:00
Where(sq.Eq{"delete_at": 0}).
2020-10-28 14:35:41 +01:00
Where(condition)
rows, err := query.Query()
2020-11-06 16:46:35 +01:00
if err != nil {
log.Printf("getUsersByCondition ERROR: %v", err)
2020-11-06 16:46:35 +01:00
return nil, err
}
defer s.CloseRows(rows)
2020-11-06 16:46:35 +01:00
users, err := s.usersFromRows(rows)
2020-10-28 14:35:41 +01:00
if err != nil {
return nil, err
}
if len(users) == 0 {
return nil, sql.ErrNoRows
}
return users, nil
2020-10-28 14:35:41 +01:00
}
func (s *SQLStore) getUserByID(db sq.BaseRunner, userID string) (*model.User, error) {
return s.getUserByCondition(db, sq.Eq{"id": userID})
2020-10-28 14:35:41 +01:00
}
func (s *SQLStore) getUserByEmail(db sq.BaseRunner, email string) (*model.User, error) {
return s.getUserByCondition(db, sq.Eq{"email": email})
2020-10-28 14:35:41 +01:00
}
func (s *SQLStore) getUserByUsername(db sq.BaseRunner, username string) (*model.User, error) {
return s.getUserByCondition(db, sq.Eq{"username": username})
2020-10-28 14:35:41 +01:00
}
func (s *SQLStore) createUser(db sq.BaseRunner, user *model.User) error {
now := utils.GetMillis()
2020-10-28 14:35:41 +01:00
2020-11-06 16:46:35 +01:00
propsBytes, err := json.Marshal(user.Props)
if err != nil {
return err
}
query := s.getQueryBuilder(db).Insert(s.tablePrefix+"users").
2020-10-28 14:35:41 +01:00
Columns("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "props", "create_at", "update_at", "delete_at").
2020-11-06 16:46:35 +01:00
Values(user.ID, user.Username, user.Email, user.Password, user.MfaSecret, user.AuthService, user.AuthData, propsBytes, now, now, 0)
_, err = query.Exec()
return err
2020-10-28 14:35:41 +01:00
}
func (s *SQLStore) updateUser(db sq.BaseRunner, user *model.User) error {
now := utils.GetMillis()
2020-11-06 16:46:35 +01:00
propsBytes, err := json.Marshal(user.Props)
if err != nil {
return err
}
query := s.getQueryBuilder(db).Update(s.tablePrefix+"users").
2020-11-06 16:46:35 +01:00
Set("username", user.Username).
Set("email", user.Email).
Set("props", propsBytes).
Set("update_at", now).
Where(sq.Eq{"id": user.ID})
2020-11-06 16:46:35 +01:00
2021-02-01 19:49:57 +01:00
result, err := query.Exec()
if err != nil {
return err
}
rowCount, err := result.RowsAffected()
if err != nil {
return err
}
if rowCount < 1 {
return UserNotFoundError{user.ID}
2021-02-01 19:49:57 +01:00
}
return nil
2020-10-28 14:35:41 +01:00
}
func (s *SQLStore) updateUserPassword(db sq.BaseRunner, username, password string) error {
now := utils.GetMillis()
query := s.getQueryBuilder(db).Update(s.tablePrefix+"users").
Set("password", password).
Set("update_at", now).
Where(sq.Eq{"username": username})
2021-02-01 19:49:57 +01:00
result, err := query.Exec()
if err != nil {
return err
}
rowCount, err := result.RowsAffected()
if err != nil {
return err
}
if rowCount < 1 {
return UserNotFoundError{username}
2021-02-01 19:49:57 +01:00
}
return nil
}
2021-01-22 20:28:45 +01:00
func (s *SQLStore) updateUserPasswordByID(db sq.BaseRunner, userID, password string) error {
now := utils.GetMillis()
2021-01-21 19:16:40 +01:00
query := s.getQueryBuilder(db).Update(s.tablePrefix+"users").
2021-01-21 19:16:40 +01:00
Set("password", password).
Set("update_at", now).
Where(sq.Eq{"id": userID})
2021-02-01 19:49:57 +01:00
result, err := query.Exec()
if err != nil {
return err
}
rowCount, err := result.RowsAffected()
if err != nil {
return err
}
if rowCount < 1 {
return UserNotFoundError{userID}
2021-02-01 19:49:57 +01:00
}
return nil
2021-01-21 19:16:40 +01:00
}
func (s *SQLStore) getUsersByWorkspace(db sq.BaseRunner, _ string) ([]*model.User, error) {
return s.getUsersByCondition(db, nil)
}
func (s *SQLStore) usersFromRows(rows *sql.Rows) ([]*model.User, error) {
users := []*model.User{}
for rows.Next() {
var user model.User
var propsBytes []byte
err := rows.Scan(
&user.ID,
&user.Username,
&user.Email,
&user.Password,
&user.MfaSecret,
&user.AuthService,
&user.AuthData,
&propsBytes,
&user.CreateAt,
&user.UpdateAt,
&user.DeleteAt,
)
if err != nil {
return nil, err
}
err = json.Unmarshal(propsBytes, &user.Props)
if err != nil {
return nil, err
}
users = append(users, &user)
}
return users, nil
}