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"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Users exposes methods to manage User table in database.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// validate email
|
|
|
|
_, err := mail.ParseAddress(user.Email)
|
|
|
|
errs.AddWrap(err)
|
|
|
|
|
2019-03-27 12:33:32 +00:00
|
|
|
// validate fullName
|
|
|
|
if user.FullName == "" {
|
|
|
|
errs.Add("fullName can't be empty")
|
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 {
|
|
|
|
UserInfo
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
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
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
errs.AddWrap(user.UserInfo.IsValid())
|
|
|
|
errs.AddWrap(validatePassword(user.Password))
|
2018-11-29 16:23:44 +00:00
|
|
|
|
|
|
|
return errs.Combine()
|
|
|
|
}
|
|
|
|
|
2019-02-11 10:33:56 +00:00
|
|
|
// UserStatus - is used to indicate status of the users account
|
|
|
|
type UserStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Inactive is a user status that he receives after registration
|
|
|
|
Inactive UserStatus = 0
|
|
|
|
// Active is a user status that he receives after account activation
|
|
|
|
Active UserStatus = 1
|
|
|
|
// Deleted is a user status that he receives after deleting account
|
|
|
|
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"`
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|