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.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package console
|
2018-11-08 14:19:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-29 16:23:44 +00:00
|
|
|
"net/mail"
|
2018-11-08 14:19:42 +00:00
|
|
|
"time"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2018-11-08 14:19:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Users exposes methods to manage User table in database.
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Database
|
2018-11-08 14:19:42 +00:00
|
|
|
type Users interface {
|
2019-01-30 15:04:40 +00:00
|
|
|
// Get is a method for querying user from the database by id.
|
|
|
|
Get(ctx context.Context, id uuid.UUID) (*User, error)
|
2018-12-10 13:47:48 +00:00
|
|
|
// GetByEmail is a method for querying user by email from the database.
|
|
|
|
GetByEmail(ctx context.Context, email string) (*User, error)
|
2019-01-30 15:04:40 +00:00
|
|
|
// Insert is a method for inserting user into the database.
|
2018-11-09 12:05:24 +00:00
|
|
|
Insert(ctx context.Context, user *User) (*User, error)
|
2018-11-08 14:19:42 +00:00
|
|
|
// Delete is a method for deleting user by Id from the database.
|
|
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
2019-01-30 15:04:40 +00:00
|
|
|
// Update is a method for updating user entity.
|
2018-11-08 14:19:42 +00:00
|
|
|
Update(ctx context.Context, user *User) error
|
2020-07-15 17:49:37 +01:00
|
|
|
// GetProjectLimit is a method to get the users project limit
|
|
|
|
GetProjectLimit(ctx context.Context, id uuid.UUID) (limit int, err error)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
// UserInfo holds User updatable data.
|
2018-11-21 15:51:43 +00:00
|
|
|
type UserInfo struct {
|
2019-03-27 12:33:32 +00:00
|
|
|
FullName string `json:"fullName"`
|
|
|
|
ShortName string `json:"shortName"`
|
2018-11-21 15:51:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
// IsValid checks UserInfo validity and returns error describing whats wrong.
|
2018-11-29 16:23:44 +00:00
|
|
|
func (user *UserInfo) IsValid() error {
|
|
|
|
var errs validationErrors
|
|
|
|
|
2019-03-27 12:33:32 +00:00
|
|
|
// validate fullName
|
2019-11-25 21:36:36 +00:00
|
|
|
if err := ValidateFullName(user.FullName); err != nil {
|
2019-10-29 14:24:16 +00:00
|
|
|
errs.AddWrap(err)
|
2018-11-29 16:23:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
return errs.Combine()
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
// CreateUser struct holds info for User creation.
|
2018-12-10 15:57:06 +00:00
|
|
|
type CreateUser struct {
|
2019-10-29 14:24:16 +00:00
|
|
|
FullName string `json:"fullName"`
|
|
|
|
ShortName string `json:"shortName"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
PartnerID string `json:"partnerId"`
|
|
|
|
Password string `json:"password"`
|
2018-12-10 15:57:06 +00:00
|
|
|
}
|
2018-11-29 16:23:44 +00:00
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
// IsValid checks CreateUser validity and returns error describing whats wrong.
|
2018-12-10 15:57:06 +00:00
|
|
|
func (user *CreateUser) IsValid() error {
|
|
|
|
var errs validationErrors
|
2018-11-29 16:23:44 +00:00
|
|
|
|
2019-11-25 21:36:36 +00:00
|
|
|
errs.AddWrap(ValidateFullName(user.FullName))
|
|
|
|
errs.AddWrap(ValidatePassword(user.Password))
|
2018-11-29 16:23:44 +00:00
|
|
|
|
2019-10-29 14:24:16 +00:00
|
|
|
// validate email
|
|
|
|
_, err := mail.ParseAddress(user.Email)
|
|
|
|
errs.AddWrap(err)
|
|
|
|
|
2019-07-17 21:53:14 +01:00
|
|
|
if user.PartnerID != "" {
|
2020-04-02 13:30:43 +01:00
|
|
|
_, err := uuid.FromString(user.PartnerID)
|
2019-07-17 21:53:14 +01:00
|
|
|
if err != nil {
|
|
|
|
errs.AddWrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:23:44 +00:00
|
|
|
return errs.Combine()
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// UserStatus - is used to indicate status of the users account.
|
2019-02-11 10:33:56 +00:00
|
|
|
type UserStatus int
|
|
|
|
|
|
|
|
const (
|
2020-08-11 15:50:01 +01:00
|
|
|
// Inactive is a user status that he receives after registration.
|
2019-02-11 10:33:56 +00:00
|
|
|
Inactive UserStatus = 0
|
2020-08-11 15:50:01 +01:00
|
|
|
// Active is a user status that he receives after account activation.
|
2019-02-11 10:33:56 +00:00
|
|
|
Active UserStatus = 1
|
2020-08-11 15:50:01 +01:00
|
|
|
// Deleted is a user status that he receives after deleting account.
|
2019-02-11 10:33:56 +00:00
|
|
|
Deleted UserStatus = 2
|
|
|
|
)
|
|
|
|
|
2019-01-30 15:04:40 +00:00
|
|
|
// User is a database object that describes User entity.
|
2018-11-08 14:19:42 +00:00
|
|
|
type User struct {
|
2018-11-14 10:50:15 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
2018-11-08 14:19:42 +00:00
|
|
|
|
2019-03-27 12:33:32 +00:00
|
|
|
FullName string `json:"fullName"`
|
|
|
|
ShortName string `json:"shortName"`
|
2019-01-30 15:04:40 +00:00
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
Email string `json:"email"`
|
|
|
|
PasswordHash []byte `json:"passwordHash"`
|
2018-11-08 14:19:42 +00:00
|
|
|
|
2019-07-12 18:59:19 +01:00
|
|
|
Status UserStatus `json:"status"`
|
|
|
|
PartnerID uuid.UUID `json:"partnerId"`
|
2019-02-11 10:33:56 +00:00
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
2020-07-15 16:14:09 +01:00
|
|
|
|
|
|
|
ProjectLimit int `json:"project_limit"`
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|