2018-11-08 14:19:42 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// 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 {
|
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)
|
2018-11-08 14:19:42 +00:00
|
|
|
// Get is a method for querying user from the database by id
|
|
|
|
Get(ctx context.Context, id uuid.UUID) (*User, error)
|
|
|
|
// 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
|
|
|
|
// Update is a method for updating user entity
|
|
|
|
Update(ctx context.Context, user *User) error
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
// UserInfo holds User updatable data
|
2018-11-21 15:51:43 +00:00
|
|
|
type UserInfo struct {
|
|
|
|
FirstName string `json:"firstName"`
|
|
|
|
LastName string `json:"lastName"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
|
2018-11-29 16:23:44 +00:00
|
|
|
// IsValid checks UserInfo validity and returns error describing whats wrong
|
|
|
|
func (user *UserInfo) IsValid() error {
|
|
|
|
var errs validationErrors
|
|
|
|
|
|
|
|
// validate email
|
|
|
|
_, err := mail.ParseAddress(user.Email)
|
|
|
|
errs.AddWrap(err)
|
|
|
|
|
|
|
|
// validate firstName
|
|
|
|
if user.FirstName == "" {
|
|
|
|
errs.Add("firstName can't be empty")
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
return errs.Combine()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser struct holds info for User creation
|
|
|
|
type CreateUser struct {
|
|
|
|
UserInfo
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
2018-11-29 16:23:44 +00:00
|
|
|
|
2018-12-10 15:57:06 +00:00
|
|
|
// IsValid checks CreateUser validity and returns error describing whats wrong
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:19:42 +00:00
|
|
|
// User is a database object that describes User entity
|
|
|
|
type User struct {
|
2018-11-14 10:50:15 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
2018-11-08 14:19:42 +00:00
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
FirstName string `json:"firstName"`
|
|
|
|
LastName string `json:"lastName"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
PasswordHash []byte `json:"passwordHash"`
|
2018-11-08 14:19:42 +00:00
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|