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"
|
|
|
|
|
2022-02-23 16:02:45 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2021-11-01 15:27:32 +00:00
|
|
|
"storj.io/common/memory"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2022-07-19 10:26:18 +01:00
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
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)
|
2022-01-06 19:46:53 +00:00
|
|
|
// GetUnverifiedNeedingReminder gets unverified users needing a reminder to verify their email.
|
2022-06-08 19:30:29 +01:00
|
|
|
GetUnverifiedNeedingReminder(ctx context.Context, firstReminder, secondReminder, cutoff time.Time) ([]*User, error)
|
2022-01-06 19:46:53 +00:00
|
|
|
// UpdateVerificationReminders increments verification_reminders.
|
|
|
|
UpdateVerificationReminders(ctx context.Context, id uuid.UUID) error
|
2022-09-23 23:23:32 +01:00
|
|
|
// UpdateFailedLoginCountAndExpiration increments failed_login_count and sets login_lockout_expiration appropriately.
|
|
|
|
UpdateFailedLoginCountAndExpiration(ctx context.Context, failedLoginPenalty *float64, id uuid.UUID) 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.
|
2022-06-01 22:15:37 +01:00
|
|
|
Update(ctx context.Context, userID uuid.UUID, request UpdateUserRequest) error
|
2021-07-01 00:13:45 +01:00
|
|
|
// UpdatePaidTier sets whether the user is in the paid tier.
|
2022-02-28 21:37:46 +00:00
|
|
|
UpdatePaidTier(ctx context.Context, id uuid.UUID, paidTier bool, projectBandwidthLimit, projectStorageLimit memory.Size, projectSegmentLimit int64, projectLimit int) error
|
2022-12-15 07:11:03 +00:00
|
|
|
// UpdateUserProjectLimits is a method to update the user's usage limits for new projects.
|
|
|
|
UpdateUserProjectLimits(ctx context.Context, id uuid.UUID, limits UsageLimits) 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)
|
2022-02-04 17:31:24 +00:00
|
|
|
// GetUserPaidTier is a method to gather whether the specified user is on the Paid Tier or not.
|
|
|
|
GetUserPaidTier(ctx context.Context, id uuid.UUID) (isPaid bool, 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.
|
2022-02-23 16:02:45 +00:00
|
|
|
// The returned error has the class ErrValiation.
|
2018-11-29 16:23:44 +00:00
|
|
|
func (user *UserInfo) IsValid() error {
|
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 {
|
2022-02-23 16:02:45 +00:00
|
|
|
return ErrValidation.Wrap(err)
|
2018-11-29 16:23:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 16:02:45 +00:00
|
|
|
return nil
|
2018-12-10 15:57:06 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-05-06 21:56:18 +01:00
|
|
|
FullName string `json:"fullName"`
|
|
|
|
ShortName string `json:"shortName"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
UserAgent []byte `json:"userAgent"`
|
|
|
|
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"`
|
|
|
|
CaptchaResponse string `json:"captchaResponse"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
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.
|
2022-02-23 16:02:45 +00:00
|
|
|
// The returned error has the class ErrValiation.
|
2018-12-10 15:57:06 +00:00
|
|
|
func (user *CreateUser) IsValid() error {
|
2022-02-23 16:02:45 +00:00
|
|
|
errgrp := errs.Group{}
|
2018-11-29 16:23:44 +00:00
|
|
|
|
2022-02-23 16:02:45 +00:00
|
|
|
errgrp.Add(
|
|
|
|
ValidateFullName(user.FullName),
|
|
|
|
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)
|
2022-02-23 16:02:45 +00:00
|
|
|
errgrp.Add(err)
|
2019-10-29 14:24:16 +00:00
|
|
|
|
2022-02-23 16:02:45 +00:00
|
|
|
return ErrValidation.Wrap(errgrp.Err())
|
2018-11-29 16:23:44 +00:00
|
|
|
}
|
|
|
|
|
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-12-06 19:06:50 +00:00
|
|
|
ProjectSegmentLimit int64 `json:"projectSegmentLimit"`
|
2021-11-01 15:27:32 +00:00
|
|
|
}
|
|
|
|
|
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"`
|
2022-07-21 14:31:38 +01:00
|
|
|
CaptchaResponse string `json:"captchaResponse"`
|
2022-06-05 23:41:38 +01:00
|
|
|
IP string `json:"-"`
|
|
|
|
UserAgent string `json:"-"`
|
2021-07-13 18:21:16 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 10:26:18 +01:00
|
|
|
// TokenInfo holds info for user authentication token responses.
|
|
|
|
type TokenInfo struct {
|
|
|
|
consoleauth.Token `json:"token"`
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
}
|
|
|
|
|
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"`
|
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"`
|
2021-12-06 19:06:50 +00:00
|
|
|
ProjectSegmentLimit int64 `json:"projectSegmentLimit"`
|
2021-11-01 15:27:32 +00:00
|
|
|
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"`
|
2021-12-22 15:43:37 +00:00
|
|
|
|
2023-01-03 20:55:57 +00:00
|
|
|
VerificationReminders int `json:"verificationReminders"`
|
2022-02-18 14:53:53 +00:00
|
|
|
|
|
|
|
FailedLoginCount int `json:"failedLoginCount"`
|
|
|
|
LoginLockoutExpiration time.Time `json:"loginLockoutExpiration"`
|
2022-08-17 11:30:07 +01:00
|
|
|
SignupCaptcha *float64 `json:"-"`
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
2022-05-27 15:31:28 +01:00
|
|
|
|
|
|
|
// ResponseUser is an entity which describes db User and can be sent in response.
|
|
|
|
type ResponseUser struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
FullName string `json:"fullName"`
|
|
|
|
ShortName string `json:"shortName"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
UserAgent []byte `json:"userAgent"`
|
|
|
|
ProjectLimit int `json:"projectLimit"`
|
|
|
|
IsProfessional bool `json:"isProfessional"`
|
|
|
|
Position string `json:"position"`
|
|
|
|
CompanyName string `json:"companyName"`
|
|
|
|
EmployeeCount string `json:"employeeCount"`
|
|
|
|
HaveSalesContact bool `json:"haveSalesContact"`
|
|
|
|
PaidTier bool `json:"paidTier"`
|
|
|
|
MFAEnabled bool `json:"isMFAEnabled"`
|
|
|
|
MFARecoveryCodeCount int `json:"mfaRecoveryCodeCount"`
|
|
|
|
}
|
2022-06-05 23:41:38 +01:00
|
|
|
|
|
|
|
// key is a context value key type.
|
|
|
|
type key int
|
|
|
|
|
|
|
|
// userKey is context key for User.
|
|
|
|
const userKey key = 0
|
|
|
|
|
|
|
|
// WithUser creates new context with User.
|
|
|
|
func WithUser(ctx context.Context, user *User) context.Context {
|
|
|
|
return context.WithValue(ctx, userKey, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUser gets User from context.
|
|
|
|
func GetUser(ctx context.Context) (*User, error) {
|
2022-07-30 04:15:30 +01:00
|
|
|
if user, ok := ctx.Value(userKey).(*User); ok {
|
2022-06-05 23:41:38 +01:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, Error.New("user is not in context")
|
|
|
|
}
|
2022-06-01 22:15:37 +01:00
|
|
|
|
|
|
|
// UpdateUserRequest contains all columns which are optionally updatable by users.Update.
|
|
|
|
type UpdateUserRequest struct {
|
|
|
|
FullName *string
|
|
|
|
ShortName **string
|
|
|
|
|
|
|
|
Email *string
|
|
|
|
PasswordHash []byte
|
|
|
|
|
|
|
|
Status *UserStatus
|
|
|
|
|
|
|
|
ProjectLimit *int
|
|
|
|
ProjectStorageLimit *int64
|
|
|
|
ProjectBandwidthLimit *int64
|
|
|
|
ProjectSegmentLimit *int64
|
|
|
|
PaidTier *bool
|
|
|
|
|
|
|
|
MFAEnabled *bool
|
|
|
|
MFASecretKey **string
|
|
|
|
MFARecoveryCodes *[]string
|
|
|
|
|
|
|
|
// failed_login_count is nullable, but we don't really have a reason
|
|
|
|
// to set it to NULL, so it doesn't need to be a double pointer here.
|
|
|
|
FailedLoginCount *int
|
|
|
|
|
|
|
|
LoginLockoutExpiration **time.Time
|
|
|
|
}
|