2019-05-16 10:39:25 -04:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2019-05-27 02:44:40 -05:00
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
2019-05-16 10:39:25 -04:00
|
|
|
"time"
|
|
|
|
|
2019-05-28 04:44:01 -05:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
|
2019-05-27 02:44:40 -05:00
|
|
|
"github.com/lib/pq"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/go-playground/validator.v9"
|
2019-05-16 10:39:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// User represents someone with access to our system.
|
|
|
|
type User struct {
|
2019-05-29 18:52:28 -08:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
2019-05-16 10:39:25 -04:00
|
|
|
|
2019-05-29 18:52:28 -08:00
|
|
|
PasswordSalt string `json:"-"`
|
|
|
|
PasswordHash []byte `json:"-"`
|
|
|
|
PasswordReset sql.NullString `json:"-"`
|
2019-05-16 10:39:25 -04:00
|
|
|
|
2019-05-29 18:52:28 -08:00
|
|
|
Timezone string `json:"timezone"`
|
2019-05-27 02:44:40 -05:00
|
|
|
|
2019-05-29 18:52:28 -08:00
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
ArchivedAt pq.NullTime `json:"archived_at"`
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 02:44:40 -05:00
|
|
|
// CreateUserRequest contains information needed to create a new User.
|
|
|
|
type CreateUserRequest struct {
|
2019-05-29 15:05:17 -05:00
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Email string `json:"email" validate:"required,email,unique"`
|
|
|
|
Password string `json:"password" validate:"required"`
|
|
|
|
PasswordConfirm string `json:"password_confirm" validate:"eqfield=Password"`
|
|
|
|
Timezone *string `json:"timezone" validate:"omitempty"`
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2019-05-27 02:44:40 -05:00
|
|
|
// UpdateUserRequest defines what information may be provided to modify an existing
|
2019-05-16 10:39:25 -04:00
|
|
|
// User. All fields are optional so clients can send just the fields they want
|
|
|
|
// changed. It uses pointer fields so we can differentiate between a field that
|
|
|
|
// was not provided and a field that was provided as explicitly blank. Normally
|
|
|
|
// we do not want to use pointers to basic types but we make exceptions around
|
|
|
|
// marshalling/unmarshalling.
|
2019-05-27 02:44:40 -05:00
|
|
|
type UpdateUserRequest struct {
|
2019-05-29 15:05:17 -05:00
|
|
|
ID string `validate:"required,uuid"`
|
|
|
|
Name *string `json:"name" validate:"omitempty"`
|
|
|
|
Email *string `json:"email" validate:"omitempty,email,unique"`
|
|
|
|
Timezone *string `json:"timezone" validate:"omitempty"`
|
2019-05-27 02:44:40 -05:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UpdatePassword defines what information is required to update a user password.
|
2019-05-27 02:44:40 -05:00
|
|
|
type UpdatePasswordRequest struct {
|
|
|
|
ID string `validate:"required,uuid"`
|
|
|
|
Password string `json:"password" validate:"required"`
|
|
|
|
PasswordConfirm string `json:"password_confirm" validate:"omitempty,eqfield=Password"`
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserFindRequest defines the possible options to search for users. By default
|
|
|
|
// archived users will be excluded from response.
|
2019-05-27 02:44:40 -05:00
|
|
|
type UserFindRequest struct {
|
|
|
|
Where *string
|
|
|
|
Args []interface{}
|
|
|
|
Order []string
|
|
|
|
Limit *uint
|
|
|
|
Offset *uint
|
|
|
|
IncludedArchived bool
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccount defines the one to many relationship of an user to an account. This
|
|
|
|
// will enable a single user access to multiple accounts without having duplicate
|
|
|
|
// users. Each association of a user to an account has a set of roles and a status
|
|
|
|
// defined for the user. The roles will be applied to enforce ACLs across the
|
|
|
|
// application. The status will allow users to be managed on by account with users
|
|
|
|
// being global to the application.
|
2019-05-27 02:44:40 -05:00
|
|
|
type UserAccount struct {
|
2019-05-29 18:52:28 -08:00
|
|
|
ID string `json:"id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
AccountID string `json:"account_id"`
|
|
|
|
Roles UserAccountRoles `json:"roles"`
|
|
|
|
Status UserAccountStatus `json:"status"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
ArchivedAt pq.NullTime `json:"archived_at"`
|
2019-05-29 15:05:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddAccountRequest defines the information is needed to associate a user to an
|
|
|
|
// account. Users are global to the application and each users access can be managed
|
|
|
|
// on an account level. If a current entry exists in the database but is archived,
|
|
|
|
// it will be un-archived.
|
2019-05-27 02:44:40 -05:00
|
|
|
type AddAccountRequest struct {
|
2019-05-29 15:05:17 -05:00
|
|
|
UserID string `validate:"required,uuid"`
|
|
|
|
AccountID string `validate:"required,uuid"`
|
|
|
|
Roles UserAccountRoles `json:"roles" validate:"required,dive,oneof=admin user"`
|
|
|
|
Status *UserAccountStatus `json:"status" validate:"omitempty,oneof=active invited disabled"`
|
2019-05-27 02:44:40 -05:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UpdateAccountRequest defines the information needed to update the roles or the
|
|
|
|
// status for an existing user account.
|
2019-05-27 02:44:40 -05:00
|
|
|
type UpdateAccountRequest struct {
|
2019-05-29 15:05:17 -05:00
|
|
|
UserID string `validate:"required,uuid"`
|
|
|
|
AccountID string `validate:"required,uuid"`
|
|
|
|
Roles *UserAccountRoles `json:"roles" validate:"required,dive,oneof=admin user"`
|
|
|
|
Status *UserAccountStatus `json:"status" validate:"omitempty,oneof=active invited disabled"`
|
|
|
|
unArchive bool `json:"-"` // Internal use only.
|
2019-05-27 02:44:40 -05:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// RemoveAccountRequest defines the information needed to remove an existing account
|
|
|
|
// for a user. This will archive (soft-delete) the existing database entry.
|
2019-05-27 02:44:40 -05:00
|
|
|
type RemoveAccountRequest struct {
|
|
|
|
UserID string `validate:"required,uuid"`
|
|
|
|
AccountID string `validate:"required,uuid"`
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// DeleteAccountRequest defines the information needed to delete an existing account
|
|
|
|
// for a user. This will hard delete the existing database entry.
|
2019-05-27 02:44:40 -05:00
|
|
|
type DeleteAccountRequest struct {
|
|
|
|
UserID string `validate:"required,uuid"`
|
|
|
|
AccountID string `validate:"required,uuid"`
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountFindRequest defines the possible options to search for users accounts.
|
|
|
|
// By default archived user accounts will be excluded from response.
|
2019-05-27 02:44:40 -05:00
|
|
|
type UserAccountFindRequest struct {
|
|
|
|
Where *string
|
|
|
|
Args []interface{}
|
|
|
|
Order []string
|
|
|
|
Limit *uint
|
|
|
|
Offset *uint
|
|
|
|
IncludedArchived bool
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountStatus represents the status of a user for an account.
|
2019-05-29 03:35:08 -05:00
|
|
|
type UserAccountStatus string
|
2019-05-28 04:44:01 -05:00
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountStatus values define the status field of a user account.
|
2019-05-27 02:44:40 -05:00
|
|
|
const (
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountStatus_Active defines the state when a user can access an account.
|
|
|
|
UserAccountStatus_Active UserAccountStatus = "active"
|
|
|
|
// UserAccountStatus_Invited defined the state when a user has been invited to an
|
|
|
|
// account.
|
|
|
|
UserAccountStatus_Invited UserAccountStatus = "invited"
|
|
|
|
// UserAccountStatus_Disabled defines the state when a user has been disabled from
|
|
|
|
// accessing an account.
|
2019-05-29 03:35:08 -05:00
|
|
|
UserAccountStatus_Disabled UserAccountStatus = "disabled"
|
2019-05-27 02:44:40 -05:00
|
|
|
)
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountStatus_Values provides list of valid UserAccountStatus values.
|
2019-05-29 03:35:08 -05:00
|
|
|
var UserAccountStatus_Values = []UserAccountStatus{
|
|
|
|
UserAccountStatus_Active,
|
2019-05-29 15:05:17 -05:00
|
|
|
UserAccountStatus_Invited,
|
2019-05-29 03:35:08 -05:00
|
|
|
UserAccountStatus_Disabled,
|
2019-05-27 02:44:40 -05:00
|
|
|
}
|
|
|
|
|
2019-05-29 03:35:08 -05:00
|
|
|
// Scan supports reading the UserAccountStatus value from the database.
|
|
|
|
func (s *UserAccountStatus) Scan(value interface{}) error {
|
2019-05-27 02:44:40 -05:00
|
|
|
asBytes, ok := value.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Scan source is not []byte")
|
|
|
|
}
|
2019-05-29 03:35:08 -05:00
|
|
|
*s = UserAccountStatus(string(asBytes))
|
2019-05-27 02:44:40 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-29 03:35:08 -05:00
|
|
|
// Value converts the UserAccountStatus value to be stored in the database.
|
|
|
|
func (s UserAccountStatus) Value() (driver.Value, error) {
|
2019-05-27 02:44:40 -05:00
|
|
|
v := validator.New()
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
errs := v.Var(s, "required,oneof=active invited disabled")
|
2019-05-27 02:44:40 -05:00
|
|
|
if errs != nil {
|
|
|
|
return nil, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(s), nil
|
|
|
|
}
|
|
|
|
|
2019-05-29 03:35:08 -05:00
|
|
|
// String converts the UserAccountStatus value to a string.
|
|
|
|
func (s UserAccountStatus) String() string {
|
2019-05-27 02:44:40 -05:00
|
|
|
return string(s)
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 04:44:01 -05:00
|
|
|
// UserAccountRole represents the role of a user for an account.
|
|
|
|
type UserAccountRole string
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountRole values define the role field of a user account.
|
2019-05-28 04:44:01 -05:00
|
|
|
const (
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountRole_Admin defines the state of a user when they have admin
|
|
|
|
// privileges for accessing an account. This role provides a user with full
|
|
|
|
// access to an account.
|
2019-05-28 04:44:01 -05:00
|
|
|
UserAccountRole_Admin UserAccountRole = auth.RoleAdmin
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountRole_User defines the state of a user when they have basic
|
|
|
|
// privileges for accessing an account. This role provies a user with the most
|
|
|
|
// limited access to an account.
|
|
|
|
UserAccountRole_User UserAccountRole = auth.RoleUser
|
2019-05-28 04:44:01 -05:00
|
|
|
)
|
|
|
|
|
2019-05-29 15:05:17 -05:00
|
|
|
// UserAccountRole_Values provides list of valid UserAccountRole values.
|
2019-05-28 04:44:01 -05:00
|
|
|
var UserAccountRole_Values = []UserAccountRole{
|
|
|
|
UserAccountRole_Admin,
|
|
|
|
UserAccountRole_User,
|
|
|
|
}
|
|
|
|
|
|
|
|
// String converts the UserAccountRole value to a string.
|
|
|
|
func (s UserAccountRole) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserAccountRoles represents a set of roles for a user for an account.
|
|
|
|
type UserAccountRoles []UserAccountRole
|
|
|
|
|
|
|
|
// Scan supports reading the UserAccountRole value from the database.
|
|
|
|
func (s *UserAccountRoles) Scan(value interface{}) error {
|
|
|
|
arr := &pq.StringArray{}
|
|
|
|
if err := arr.Scan(value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range *arr {
|
|
|
|
*s = append(*s, UserAccountRole(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value converts the UserAccountRole value to be stored in the database.
|
|
|
|
func (s UserAccountRoles) Value() (driver.Value, error) {
|
|
|
|
v := validator.New()
|
|
|
|
|
|
|
|
var arr pq.StringArray
|
|
|
|
for _, r := range s {
|
2019-05-29 03:35:08 -05:00
|
|
|
errs := v.Var(r, "required,oneof=admin user")
|
2019-05-28 04:44:01 -05:00
|
|
|
if errs != nil {
|
|
|
|
return nil, errs
|
|
|
|
}
|
|
|
|
arr = append(arr, r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr.Value()
|
|
|
|
}
|
|
|
|
|
2019-05-16 10:39:25 -04:00
|
|
|
// Token is the payload we deliver to users when they authenticate.
|
|
|
|
type Token struct {
|
2019-05-28 04:44:01 -05:00
|
|
|
Token string `json:"token"`
|
|
|
|
claims auth.Claims `json:"-"`
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|