user

package
v0.0.0-...-40cf777 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package user provides an example of a core business API. Right now these calls are just wrapping the data/data layer. But at some point you will want auditing or something that isn't specific to the data/store layer.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound              = errors.New("user not found")
	ErrInvalidID             = errors.New("ID is not in its proper form")
	ErrAuthenticationFailure = errors.New("authentication failed")
)

Set of error variables for CRUD operations.

Functions

This section is empty.

Types

type Core

type Core struct {
	// contains filtered or unexported fields
}

Core manages the set of API's for user access.

func NewCore

func NewCore(log *zap.SugaredLogger, sqlxDB *sqlx.DB, client email.EmailClient) Core

NewCore constructs a core for user api access.

func (Core) Authenticate

func (c Core) Authenticate(ctx context.Context, now time.Time, email, password string) (auth.Claims, error)

Authenticate finds a user by their email and verifies their password. On success it returns a Claims User representing this user. The claims can be used to generate a token for future authentication.

func (Core) Create

func (c Core) Create(ctx context.Context, nu NewUser, now time.Time) (User, error)

Create inserts a new user into the database.

func (Core) Delete

func (c Core) Delete(ctx context.Context, userID string) error

Delete removes a user from the database.

func (Core) Query

func (c Core) Query(ctx context.Context, pageNumber int, rowsPerPage int) ([]User, error)

Query retrieves a list of existing users from the database.

func (Core) QueryByEmail

func (c Core) QueryByEmail(ctx context.Context, email string) (User, error)

QueryByEmail gets the specified user from the database by email.

func (Core) QueryByID

func (c Core) QueryByID(ctx context.Context, userID string) (User, error)

QueryByID gets the specified user from the database.

func (Core) Update

func (c Core) Update(ctx context.Context, userID string, uu UpdateUser, now time.Time) error

Update replaces a user document in the database.

type NewUser

type NewUser struct {
	Name            string   `json:"name" validate:"required"`
	Email           string   `json:"email" validate:"required,email"`
	Roles           []string `json:"roles" validate:"required"`
	Password        string   `json:"password" validate:"required"`
	PasswordConfirm string   `json:"password_confirm" validate:"eqfield=Password"`
}

NewUser contains information needed to create a new User.

type UpdateUser

type UpdateUser struct {
	Name            *string  `json:"name"`
	Email           *string  `json:"email" validate:"omitempty,email"`
	Roles           []string `json:"roles"`
	Password        *string  `json:"password"`
	PasswordConfirm *string  `json:"password_confirm" validate:"omitempty,eqfield=Password"`
}

UpdateUser defines what information may be provided to modify an existing 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.

type User

type User struct {
	ID           string    `json:"id"`
	Name         string    `json:"name"`
	Email        string    `json:"email"`
	Roles        []string  `json:"roles"`
	PasswordHash []byte    `json:"-"`
	DateCreated  time.Time `json:"date_created"`
	DateUpdated  time.Time `json:"date_updated"`
}

User represents an individual user.

Directories

Path Synopsis
Package db contains user related CRUD functionality.
Package db contains user related CRUD functionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL