data

package
v0.0.0-...-4d6aaa6 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeActivation     = "activation"
	ScopeAuthentication = "authentication"
)

Variables

View Source
var (
	ErrEditConflict   = errors.New("edit conflict")
	ErrRecordNotFound = errors.New("record not found")
)
View Source
var (
	AnonymousUser     = &User{}
	ErrDuplicateEmail = errors.New("duplicate email")
)

Functions

func ValidatePasswordPlaintext

func ValidatePasswordPlaintext(v *validator.Validator, password string)

ValidatePasswordPlaintext ensures that a provided password satisfies the desired password requirements. Any violations will be added to the given validator.Validator under the "password" key.

func ValidateTokenPlaintext

func ValidateTokenPlaintext(v *validator.Validator, tokenPlaintext string)

func ValidateUser

func ValidateUser(v *validator.Validator, user *User)

ValidateUser checks if a user is considered valid and stores any errors in the provided validator.Validator struct.

Types

type Models

type Models struct {
	Permissions PermissionModel
	Tokens      TokenModel
	Users       UserModel
}

func NewModels

func NewModels(db *sql.DB) Models

type PermissionModel

type PermissionModel struct {
	DB *sql.DB
}

func (PermissionModel) AddForUser

func (m PermissionModel) AddForUser(userID int64, codes ...string) error

func (PermissionModel) GetAllForUser

func (m PermissionModel) GetAllForUser(userID int64) (Permissions, error)

type Permissions

type Permissions []string

func (Permissions) Include

func (p Permissions) Include(code string) bool

type Token

type Token struct {
	Plaintext string    `json:"token"`
	Hash      []byte    `json:"-"`
	UserID    int64     `json:"-"`
	Expiry    time.Time `json:"expiry"`
	Scope     string    `json:"-"`
}

type TokenModel

type TokenModel struct {
	DB *sql.DB
}

func (TokenModel) DeleteAllForUser

func (m TokenModel) DeleteAllForUser(scope string, userID int64) error

func (TokenModel) Insert

func (m TokenModel) Insert(token *Token) error

func (TokenModel) New

func (m TokenModel) New(userID int64, ttl time.Duration, scope string) (*Token, error)

type User

type User struct {
	ID           int64           `json:"-"`
	Version      int             `json:"-"`
	CreatedAt    time.Time       `json:"-"`
	UpdatedAt    time.Time       `json:"-"`
	UserID       string          `json:"user_id"`
	Email        string          `json:"email"`
	Password     password        `json:"-"`
	Name         string          `json:"name"`
	FriendlyName *string         `json:"friendly_name,omitempty"`
	BirthDate    *jsonz.DateOnly `json:"birth_date,omitempty"`
	Gender       *string         `json:"gender,omitempty"`
	CountryCode  *string         `json:"country_code,omitempty"`
	TimeZone     *string         `json:"time_zone,omitempty"`
	Activated    bool            `json:"-"`
	Suspended    bool            `json:"-"`
}

User represents a human user of a system.

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

IsAnonymous compares the User receiver to the AnonymousUser struct.

type UserModel

type UserModel struct {
	DB *sql.DB
}

func (UserModel) DeleteByEmail

func (m UserModel) DeleteByEmail(email string) error

DeleteByEmail soft deletes the user with the given email address. If no matching record exists, ErrRecordNotFound is returned.

func (UserModel) GetByIdentifier

func (m UserModel) GetByIdentifier(field, value string) (*User, error)

GetByIdentifier queries the database for a user based on the given field for the given value. If no matching record exists, ErrRecordNotFound is returned. Valid field names are "email" and "user_id".

func (UserModel) GetForToken

func (m UserModel) GetForToken(tokenScope, tokenPlaintext string) (*User, error)

GetForToken retrieves a User from the database for a given Token. If the token is expired, or the user has been suspended or deleted, then an ErrRecordNotFound error is returned.

func (UserModel) Insert

func (m UserModel) Insert(user *User) error

Insert adds the given User into the database. If the email address (case insensitive) already exists in the database, then an ErrDuplicateEmail response will be returned.

func (UserModel) Update

func (m UserModel) Update(user *User) error

Update updates the database record for the given User. If there is an edit conflict and the version number is not the expected one, then ErrEditConflict will be returned.

Jump to

Keyboard shortcuts

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