user

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

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

func IdToCacheKey

func IdToCacheKey(id string) string

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, ethClient blockchain.BlockchainClient, cacheStorer cachestore.CacheStore) (*Core, error)

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, newUser User) (User, error)

Create inserts a new user into the database.

func (*Core) Delete

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

Delete removes a user from the database.

func (*Core) QueryByEmail

func (c *Core) QueryByEmail(ctx context.Context, email mail.Address) (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) QueryByIDs

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

QueryByIDs gets the specified user from the database.

type MockStorer added in v0.3.1

type MockStorer struct {
	mock.Mock
}

MockStorer mocks the Storer interface for testing.

func (*MockStorer) Count added in v0.3.1

func (m *MockStorer) Count(ctx context.Context, filter QueryFilter) (int, error)

func (*MockStorer) Create added in v0.3.1

func (m *MockStorer) Create(ctx context.Context, usr User) (User, error)

func (*MockStorer) Delete added in v0.3.1

func (m *MockStorer) Delete(ctx context.Context, usr User) error

func (*MockStorer) Query added in v0.3.1

func (m *MockStorer) Query(ctx context.Context, filter QueryFilter, orderBy string, pageNumber int, rowsPerPage int) ([]User, error)

func (*MockStorer) QueryByEmail added in v0.3.1

func (m *MockStorer) QueryByEmail(ctx context.Context, email mail.Address) (User, error)

func (*MockStorer) QueryByID added in v0.3.1

func (m *MockStorer) QueryByID(ctx context.Context, userID string) (User, error)

func (*MockStorer) QueryByIDs added in v0.3.1

func (m *MockStorer) QueryByIDs(ctx context.Context, userIDs []string) ([]User, error)

type NewUser

type NewUser struct {
	Name     string
	Email    string
	Password string `json:"password"`
}

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"`
}

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) WithEndCreatedDate

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

WithEndCreatedDate 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 {
	Name string
}

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) 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) (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 string) (User, error)
	QueryByIDs(ctx context.Context, userID []string) ([]User, error)
	QueryByEmail(ctx context.Context, email mail.Address) (User, error)
}

type User

type User struct {
	ID           primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
	Name         string             `json:"name"`
	Email        string             `json:"email"`
	Roles        []Role             `json:"roles"`
	PasswordHash []byte             `json:"password_hash"`
	AddressHex   string             `json:"address_hex"`
	PublicKey    []byte             `json:"public_key"`
	PrivateKey   []byte             `json:"private_key"`
	SecretKey    []byte             `json:"secret_key"`
	DateCreated  time.Time          `json:"date_created"`
	DateUpdated  time.Time          `json:"date_updated"`
}

func MapToUser added in v0.3.0

func MapToUser(nu NewUser) (User, error)

func (User) CacheExpiration

func (u User) CacheExpiration() time.Duration

func (User) CacheKey

func (u User) CacheKey() string

type UserCredentials

type UserCredentials struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

type UserDTO

type UserDTO struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Email       string    `json:"email"`
	Roles       []Role    `json:"roles"`
	AddressHex  string    `json:"address_hex"`
	DateCreated time.Time `json:"date_created"`
	DateUpdated time.Time `json:"date_updated"`
}

func MapToDto added in v0.3.0

func MapToDto(user User) UserDTO

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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