2020-10-28 15:35:41 +02:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2020-11-06 17:46:35 +02:00
|
|
|
"encoding/json"
|
2020-10-28 15:35:41 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-octo-tasks/server/model"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *SQLStore) getUserByCondition(condition sq.Eq) (*model.User, error) {
|
|
|
|
query := s.getQueryBuilder().
|
|
|
|
Select("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "props", "create_at", "update_at", "delete_at").
|
|
|
|
From("users").
|
|
|
|
Where(condition)
|
|
|
|
row := query.QueryRow()
|
|
|
|
user := model.User{}
|
|
|
|
|
2020-11-06 17:46:35 +02:00
|
|
|
var propsBytes []byte
|
|
|
|
err := row.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)
|
2020-10-28 15:35:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLStore) GetUserById(userID string) (*model.User, error) {
|
|
|
|
return s.getUserByCondition(sq.Eq{"id": userID})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLStore) GetUserByEmail(email string) (*model.User, error) {
|
|
|
|
return s.getUserByCondition(sq.Eq{"email": email})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLStore) GetUserByUsername(username string) (*model.User, error) {
|
|
|
|
return s.getUserByCondition(sq.Eq{"username": username})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLStore) CreateUser(user *model.User) error {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
|
2020-11-06 17:46:35 +02:00
|
|
|
propsBytes, err := json.Marshal(user.Props)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:35:41 +02:00
|
|
|
query := s.getQueryBuilder().Insert("users").
|
|
|
|
Columns("id", "username", "email", "password", "mfa_secret", "auth_service", "auth_data", "props", "create_at", "update_at", "delete_at").
|
2020-11-06 17:46:35 +02: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 15:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLStore) UpdateUser(user *model.User) error {
|
2020-11-06 17:46:35 +02:00
|
|
|
now := time.Now().Unix()
|
|
|
|
|
|
|
|
propsBytes, err := json.Marshal(user.Props)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := s.getQueryBuilder().Update("users").
|
|
|
|
Set("username", user.Username).
|
|
|
|
Set("email", user.Email).
|
|
|
|
Set("props", propsBytes).
|
|
|
|
Set("update_at", now)
|
|
|
|
|
|
|
|
_, err = query.Exec()
|
|
|
|
return err
|
2020-10-28 15:35:41 +02:00
|
|
|
}
|