user

package
v0.0.0-...-51ed351 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2023 License: Apache-2.0 Imports: 8 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

View Source
const (
	OrderByID      = "userid"
	OrderByName    = "name"
	OrderByEmail   = "email"
	OrderByRoles   = "roles"
	OrderByEnabled = "enabled"
)

Set of fields that the results can be ordered by. These are the names that should be used by the application layer.

Variables

View Source
var (
	RoleAdmin = Role{"ADMIN"}
	RoleUser  = Role{"USER"}
)

Set of possible roles for a user.

View Source
var (
	ErrNotFound              = errors.New("user not found")
	ErrUniqueEmail           = errors.New("email is not unique")
	ErrAuthenticationFailure = errors.New("authentication failed")
)

Set of error variables for CRUD operations.

View Source
var DefaultOrderBy = order.NewBy(OrderByID, order.ASC)

DefaultOrderBy represents the default way we sort.

Functions

This section is empty.

Types

type Core

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

Core manages the set of APIs for user access.

func NewCore

func NewCore(storer Storer) *Core

NewCore constructs a core for user api access.

func (*Core) Authenticate

func (c *Core) Authenticate(ctx context.Context, email mail.Address, password string) (User, 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) Count

func (c *Core) Count(ctx context.Context, filter QueryFilter) (int, error)

Count returns the total number of users in the store.

func (*Core) Create

func (c *Core) Create(ctx context.Context, nu NewUser) (User, error)

Create a new user in the database.

func (*Core) Delete

func (c *Core) Delete(ctx context.Context, usr User) error

Delete removes a user from the database.

func (*Core) Query

func (c *Core) Query(ctx context.Context, filter QueryFilter, orderBy order.By, pageNumber int, rowsPerPage int) ([]User, error)

Query retrieves a list of existing users form the database.

func (*Core) QueryByEmail

func (c *Core) QueryByEmail(ctx context.Context, email mail.Address) (User, error)

QueryByEmail gets the specified use from the database by email.

func (*Core) QueryByID

func (c *Core) QueryByID(ctx context.Context, userID uuid.UUID) (User, error)

QueryByID gets the specified user from the database.

func (*Core) QueryByIDs

func (c *Core) QueryByIDs(ctx context.Context, userIDs []uuid.UUID) ([]User, error)

QueryByIDs gets the specified users from the database.

func (*Core) Update

func (c *Core) Update(ctx context.Context, usr User, uu UpdateUser) (User, error)

Update replaces a user record in the database.

type NewUser

type NewUser struct {
	Name            string
	Email           mail.Address
	Roles           []Role
	Department      string
	Password        string
	PasswordConfirm string
}

NewUser contains information needed to create a new user.

type QueryFilter

type QueryFilter struct {
	ID               *uuid.UUID    `validate:"omitempty"`
	Name             *string       `validate:"omitempty,min=3"`
	Email            *mail.Address `validate:"omitempty"`
	StartCreatedDate *time.Time    `validate:"omitempty"`
	EndCreatedDate   *time.Time    `validate:"omitempty"`
}

QueryFilter holds the available fields a query can be filtered on.

func (*QueryFilter) Validate

func (qf *QueryFilter) Validate() error

Validate checks the data in the model is considered clean.

func (*QueryFilter) WithEmail

func (qf *QueryFilter) WithEmail(email mail.Address)

WithEmail sets the Email field of the QueryFilter value.

func (*QueryFilter) WithEndDateCreated

func (qf *QueryFilter) WithEndDateCreated(endDate time.Time)

WithEndDateCreated sets the DateCreated field of the QueryFilter value.

func (*QueryFilter) WithName

func (qf *QueryFilter) WithName(name string)

WithName sets the Name field of the QueryFilter value.

func (*QueryFilter) WithStartDateCreated

func (qf *QueryFilter) WithStartDateCreated(startDate time.Time)

WithStartDateCreated sets the DateCreated field of the QueryFilter value.

func (*QueryFilter) WithUserID

func (qf *QueryFilter) WithUserID(userID uuid.UUID)

WithUserID sets the ID field of the QueryFilter value.

type Role

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

Role represents a role in the system.

func MustParseRole

func MustParseRole(value string) Role

MustParseRole parses the string value and returns a role if one exists. If an error occurs the function panics.

func ParseRole

func ParseRole(value string) (Role, error)

ParseRole parses the string value and returns a role if one exists.

func (Role) Equal

func (r Role) Equal(r2 Role) bool

Equal provides support for the go-cmp package and testing.

func (Role) MarshalText

func (r Role) MarshalText() ([]byte, error)

MarshalText implement the marshal interface for JSON conversions.

func (Role) Name

func (r Role) Name() string

Name returns the name of the role.

func (*Role) UnmarshalText

func (r *Role) UnmarshalText(data []byte) error

UnmarshalText implement the unmarshal interface for JSON conversions.

type Storer

type Storer interface {
	Create(ctx context.Context, usr User) error
	Update(ctx context.Context, usr User) error
	Delete(ctx context.Context, usr User) error
	Query(ctx context.Context, filter QueryFilter, orderBy order.By, pageNumber int, rowsPerPage int) ([]User, error)
	Count(ctx context.Context, filter QueryFilter) (int, error)
	QueryByID(ctx context.Context, userID uuid.UUID) (User, error)
	QueryByIDs(ctx context.Context, userIDs []uuid.UUID) ([]User, error)
	QueryByEmail(ctx context.Context, email mail.Address) (User, error)
}

Storer interface declares the behavior this package needs to persists and retrieve data.

type UpdateUser

type UpdateUser struct {
	Name            *string
	Email           *mail.Address
	Roles           []Role
	Department      *string
	Password        *string
	PasswordConfirm *string
	Enabled         *bool
}

UpdateUser contains information needed to update a user.

type User

type User struct {
	ID           uuid.UUID
	Name         string
	Email        mail.Address
	Roles        []Role
	PasswordHash []byte
	Department   string
	Enabled      bool
	DateCreated  time.Time
	DateUpdated  time.Time
}

User represents information about an individual user.

Directories

Path Synopsis
stores

Jump to

Keyboard shortcuts

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