2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-08 14:19:42 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-10 15:00:33 +01:00
|
|
|
"strings"
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2018-11-08 14:19:42 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 14:37:39 +00:00
|
|
|
// ensures that users implements console.Users.
|
|
|
|
var _ console.Users = (*users)(nil)
|
|
|
|
|
2018-11-09 12:05:24 +00:00
|
|
|
// implementation of Users interface repository using spacemonkeygo/dbx orm
|
2018-11-08 14:19:42 +00:00
|
|
|
type users struct {
|
2018-12-10 12:29:01 +00:00
|
|
|
db dbx.Methods
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get is a method for querying user from the database by id
|
2019-06-04 12:55:38 +01:00
|
|
|
func (users *users) Get(ctx context.Context, id uuid.UUID) (_ *console.User, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-12 09:14:16 +00:00
|
|
|
user, err := users.db.Get_User_By_Id(ctx, dbx.User_Id(id[:]))
|
2018-11-08 14:19:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return userFromDBX(ctx, user)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 13:47:48 +00:00
|
|
|
// GetByEmail is a method for querying user by email from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (users *users) GetByEmail(ctx context.Context, email string) (_ *console.User, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-09-10 15:00:33 +01:00
|
|
|
user, err := users.db.Get_User_By_NormalizedEmail_And_Status_Not_Number(ctx, dbx.User_NormalizedEmail(normalizeEmail(email)))
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return userFromDBX(ctx, user)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert is a method for inserting user into the database
|
2019-06-04 12:55:38 +01:00
|
|
|
func (users *users) Insert(ctx context.Context, user *console.User) (_ *console.User, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-11-20 19:16:27 +00:00
|
|
|
|
|
|
|
if user.ID.IsZero() {
|
|
|
|
return nil, errs.New("user id is not set")
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 21:53:14 +01:00
|
|
|
optional := dbx.User_Create_Fields{
|
|
|
|
ShortName: dbx.User_ShortName(user.ShortName),
|
|
|
|
}
|
|
|
|
if !user.PartnerID.IsZero() {
|
|
|
|
optional.PartnerId = dbx.User_PartnerId(user.PartnerID[:])
|
|
|
|
}
|
2020-07-15 17:49:37 +01:00
|
|
|
if user.ProjectLimit != 0 {
|
|
|
|
optional.ProjectLimit = dbx.User_ProjectLimit(user.ProjectLimit)
|
|
|
|
}
|
2019-07-17 21:53:14 +01:00
|
|
|
|
2018-11-09 12:05:24 +00:00
|
|
|
createdUser, err := users.db.Create_User(ctx,
|
2019-11-20 19:16:27 +00:00
|
|
|
dbx.User_Id(user.ID[:]),
|
2019-02-11 10:33:56 +00:00
|
|
|
dbx.User_Email(user.Email),
|
2019-09-10 15:00:33 +01:00
|
|
|
dbx.User_NormalizedEmail(normalizeEmail(user.Email)),
|
2019-06-06 17:07:14 +01:00
|
|
|
dbx.User_FullName(user.FullName),
|
2019-01-30 15:04:40 +00:00
|
|
|
dbx.User_PasswordHash(user.PasswordHash),
|
2019-07-17 21:53:14 +01:00
|
|
|
optional,
|
2019-01-30 15:04:40 +00:00
|
|
|
)
|
2018-11-08 14:19:42 +00:00
|
|
|
|
2018-11-09 12:05:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return userFromDBX(ctx, createdUser)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is a method for deleting user by Id from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (users *users) Delete(ctx context.Context, id uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = users.db.Delete_User_By_Id(ctx, dbx.User_Id(id[:]))
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is a method for updating user entity
|
2019-06-04 12:55:38 +01:00
|
|
|
func (users *users) Update(ctx context.Context, user *console.User) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-07-17 21:53:14 +01:00
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
_, err = users.db.Update_User_By_Id(
|
2018-11-28 10:31:15 +00:00
|
|
|
ctx,
|
2018-11-12 09:14:16 +00:00
|
|
|
dbx.User_Id(user.ID[:]),
|
2018-11-28 10:31:15 +00:00
|
|
|
toUpdateUser(user),
|
|
|
|
)
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:14:09 +01:00
|
|
|
// GetProjectLimit is a method to get the users project limit
|
|
|
|
func (users *users) GetProjectLimit(ctx context.Context, id uuid.UUID) (limit int, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
row, err := users.db.Get_User_ProjectLimit_By_Id(ctx, dbx.User_Id(id[:]))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return row.ProjectLimit, nil
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:31:15 +00:00
|
|
|
// toUpdateUser creates dbx.User_Update_Fields with only non-empty fields as updatable
|
2019-01-15 13:03:24 +00:00
|
|
|
func toUpdateUser(user *console.User) dbx.User_Update_Fields {
|
2018-11-28 10:31:15 +00:00
|
|
|
update := dbx.User_Update_Fields{
|
2019-09-10 15:00:33 +01:00
|
|
|
FullName: dbx.User_FullName(user.FullName),
|
|
|
|
ShortName: dbx.User_ShortName(user.ShortName),
|
|
|
|
Email: dbx.User_Email(user.Email),
|
|
|
|
NormalizedEmail: dbx.User_NormalizedEmail(normalizeEmail(user.Email)),
|
|
|
|
Status: dbx.User_Status(int(user.Status)),
|
2020-07-15 16:14:09 +01:00
|
|
|
ProjectLimit: dbx.User_ProjectLimit(user.ProjectLimit),
|
2018-11-28 10:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extra password check to update only calculated hash from service
|
2018-12-10 15:57:06 +00:00
|
|
|
if len(user.PasswordHash) != 0 {
|
2018-11-28 10:31:15 +00:00
|
|
|
update.PasswordHash = dbx.User_PasswordHash(user.PasswordHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:19:42 +00:00
|
|
|
// userFromDBX is used for creating User entity from autogenerated dbx.User struct
|
2019-06-04 12:55:38 +01:00
|
|
|
func userFromDBX(ctx context.Context, user *dbx.User) (_ *console.User, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-08 14:19:42 +00:00
|
|
|
if user == nil {
|
|
|
|
return nil, errs.New("user parameter is nil")
|
|
|
|
}
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
id, err := uuid.FromBytes(user.Id)
|
2018-11-08 14:19:42 +00:00
|
|
|
if err != nil {
|
2018-11-12 09:14:16 +00:00
|
|
|
return nil, err
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
result := console.User{
|
2018-11-12 09:14:16 +00:00
|
|
|
ID: id,
|
2019-03-27 12:33:32 +00:00
|
|
|
FullName: user.FullName,
|
2019-02-11 10:33:56 +00:00
|
|
|
Email: user.Email,
|
2018-11-09 12:05:24 +00:00
|
|
|
PasswordHash: user.PasswordHash,
|
2019-02-11 10:33:56 +00:00
|
|
|
Status: console.UserStatus(user.Status),
|
2018-11-09 12:05:24 +00:00
|
|
|
CreatedAt: user.CreatedAt,
|
2020-07-15 16:14:09 +01:00
|
|
|
ProjectLimit: user.ProjectLimit,
|
2019-01-30 15:04:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 21:53:14 +01:00
|
|
|
if user.PartnerId != nil {
|
2020-03-31 17:49:16 +01:00
|
|
|
result.PartnerID, err = uuid.FromBytes(user.PartnerId)
|
2019-07-17 21:53:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 12:33:32 +00:00
|
|
|
if user.ShortName != nil {
|
|
|
|
result.ShortName = *user.ShortName
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
return &result, nil
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
2019-09-10 15:00:33 +01:00
|
|
|
|
|
|
|
func normalizeEmail(email string) string {
|
|
|
|
return strings.ToUpper(email)
|
|
|
|
}
|