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"
|
|
|
|
|
2021-11-01 15:27:32 +00:00
|
|
|
"storj.io/common/memory"
|
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)
|
2021-11-18 18:54:39 +00:00
|
|
|
// GetByEmailWithUnverified is a method for querying users by email from the database.
|
|
|
|
GetByEmailWithUnverified(ctx context.Context, email string) (*User, []User, error)
|
|
|
|
// GetByEmail is a method for querying user by verified email from the database.
|
2018-12-10 13:47:48 +00:00
|
|
|
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
|
2021-07-01 00:13:45 +01:00
|
|
|
// UpdatePaidTier sets whether the user is in the paid tier.
|
2021-11-01 15:27:32 +00:00
|
|
|
UpdatePaidTier(ctx context.Context, id uuid.UUID, paidTier bool, projectBandwidthLimit, projectStorageLimit memory.Size) 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)
|
2021-11-01 15:27:32 +00:00
|
|
|
// GetUserProjectLimits is a method to get the users storage and bandwidth limits for new projects.
|
|
|
|
GetUserProjectLimits(ctx context.Context, id uuid.UUID) (limit *ProjectLimits, 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 {
|
2021-06-25 12:17:55 +01:00
|
|
|
FullName string `json:"fullName"`
|
|
|
|
ShortName string `json:"shortName"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
PartnerID string `json:"partnerId"`
|
2021-09-23 00:38:18 +01:00
|
|
|
UserAgent []byte `json:"userAgent"`
|
2021-06-25 12:17:55 +01:00
|
|
|
Password string `json:"password"`
|
|
|
|
IsProfessional bool `json:"isProfessional"`
|
|
|
|
Position string `json:"position"`
|
|
|
|
CompanyName string `json:"companyName"`
|
|
|
|
WorkingOn string `json:"workingOn"`
|
|
|
|
EmployeeCount string `json:"employeeCount"`
|
|
|
|
HaveSalesContact bool `json:"haveSalesContact"`
|
|
|
|
RecaptchaResponse string `json:"recaptchaResponse"`
|
|
|
|
IP string `json:"ip"`
|
2021-10-12 14:54:05 +01:00
|
|
|
SignupPromoCode string `json:"signupPromoCode"`
|
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()
|
|
|
|
}
|
|
|
|
|
2021-11-01 15:27:32 +00:00
|
|
|
// ProjectLimits holds info for a users bandwidth and storage limits for new projects.
|
|
|
|
type ProjectLimits struct {
|
|
|
|
ProjectBandwidthLimit memory.Size `json:"projectBandwidthLimit"`
|
|
|
|
ProjectStorageLimit memory.Size `json:"projectStorageLimit"`
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:21:16 +01:00
|
|
|
// AuthUser holds info for user authentication token requests.
|
|
|
|
type AuthUser struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
MFAPasscode string `json:"mfaPasscode"`
|
|
|
|
MFARecoveryCode string `json:"mfaRecoveryCode"`
|
|
|
|
}
|
|
|
|
|
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"`
|
2021-09-23 00:38:18 +01:00
|
|
|
UserAgent []byte `json:"userAgent"`
|
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
|
|
|
|
2021-11-01 15:27:32 +00:00
|
|
|
ProjectLimit int `json:"projectLimit"`
|
|
|
|
ProjectStorageLimit int64 `json:"projectStorageLimit"`
|
|
|
|
ProjectBandwidthLimit int64 `json:"projectBandwidthLimit"`
|
|
|
|
PaidTier bool `json:"paidTier"`
|
2021-02-04 15:00:15 +00:00
|
|
|
|
|
|
|
IsProfessional bool `json:"isProfessional"`
|
|
|
|
Position string `json:"position"`
|
|
|
|
CompanyName string `json:"companyName"`
|
|
|
|
CompanySize int `json:"companySize"`
|
|
|
|
WorkingOn string `json:"workingOn"`
|
2021-02-10 15:55:38 +00:00
|
|
|
EmployeeCount string `json:"employeeCount"`
|
2021-04-27 19:40:03 +01:00
|
|
|
|
|
|
|
HaveSalesContact bool `json:"haveSalesContact"`
|
2021-07-13 18:21:16 +01:00
|
|
|
|
|
|
|
MFAEnabled bool `json:"mfaEnabled"`
|
|
|
|
MFASecretKey string `json:"mfaSecretKey"`
|
|
|
|
MFARecoveryCodes []string `json:"mfaRecoveryCodes"`
|
2021-10-12 14:54:05 +01:00
|
|
|
|
|
|
|
SignupPromoCode string `json:"signupPromoCode"`
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|