user

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package user provides the database implementation for the User entity.

Index

Constants

This section is empty.

Variables

View Source
var (
	MaxAge = 5 * 365 * 86400

	ErrAuth         = errors.New("invalid credentials")
	ErrExists       = errors.New("user exists")
	ErrTokenExpired = errors.New("token expired")
)

Functions

func Model

func Model(uu []*User) func(int) database.Model

Model is called along with database.ModelSlice to convert the given slice of User models to a slice of database.Model interfaces.

func Select

func Select(col string, opts ...query.Option) query.Query

Select returns a query that selects the given column from the users table, with each given query.Option applied to the returned query.

func WhereHandle

func WhereHandle(handle string) query.Option

WhereHandle returns a query.Option that when applied to a query will add two WHERE clauses that will check the given handle against the email column or the username column.

Types

type DeleteForm

type DeleteForm struct {
	Password string `schema:"delete_password"`
}

func (DeleteForm) Fields

func (f DeleteForm) Fields() map[string]string

func (DeleteForm) Validate

func (f DeleteForm) Validate() error

type EmailForm

type EmailForm struct {
	User  *User  `schema:"-"`
	Users *Store `schema:"-"`

	Email          string `schema:"email"`
	VerifyPassword string `schema:"verify_password"`
}

func (EmailForm) Fields

func (f EmailForm) Fields() map[string]string

Fields returns a map containing the Email field of the current EmailForm.

func (EmailForm) Validate

func (f EmailForm) Validate() error

Validate checks to see if the Email field is present, valid, and unique. The uniqueness checks will be skipped if the email matches the existing one. The Password field is checked for presence, then compared against the one in the database for authentication.

type LoginForm

type LoginForm struct {
	Handle      string `schema:"handle"`
	Password    string `schema:"password"`
	RedirectURI string `schema:"redirect_uri"`
}

func (LoginForm) Fields

func (f LoginForm) Fields() map[string]string

Fields returns a map containing the Handle field of the current LoginForm.

func (LoginForm) Validate

func (f LoginForm) Validate() error

Validate checks to see if the current LoginForm has a present Handle, and Password field.

type NewPasswordForm

type NewPasswordForm struct {
	Token          string `schema:"token"`
	Password       string `schema:"password"`
	VerifyPassword string `schema:"verify_password"`
}

func (NewPasswordForm) Fields

func (f NewPasswordForm) Fields() map[string]string

func (NewPasswordForm) Validate

func (f NewPasswordForm) Validate() error

type PasswordForm

type PasswordForm struct {
	User  *User  `schema:"-"`
	Users *Store `schema:"-"`

	OldPassword    string `schema:"old_password"`
	NewPassword    string `schema:"new_password"`
	VerifyPassword string `schema:"verify_password"`
}

func (PasswordForm) Fields

func (PasswordForm) Fields() map[string]string

Fields will return an empty map of strings.

func (PasswordForm) Validate

func (f PasswordForm) Validate() error

Validate the current PasswordForm. This checks for the presence of the old, new, and current password fields, as well as if the current password is valid based on what is in the database.

type PasswordResetForm

type PasswordResetForm struct {
	Email string `schema:"email"`
}

func (PasswordResetForm) Fields

func (f PasswordResetForm) Fields() map[string]string

func (PasswordResetForm) Validate

func (f PasswordResetForm) Validate() error

type RegisterForm

type RegisterForm struct {
	Users *Store

	Email          string `schema:"email"`
	Username       string `schema:"username"`
	Password       string `schema:"password"`
	VerifyPassword string `schema:"verify_password"`
}

func (RegisterForm) Fields

func (f RegisterForm) Fields() map[string]string

Fields returns a map of the Email and Username fields.

func (RegisterForm) Validate

func (f RegisterForm) Validate() error

Validate checks to see if the Email field is present and valid. An email is considered valid if it is less than 254 characters in length, and contains an @ character. The Username field is checked for presence, uniqueness, and validitity. A username must be between 3 and 64 characters, and only contain letters, numbers, dashes, and dots. The Password field is checked for presence, and length. It should be between 6 and 60 characters.

type Store

type Store struct {
	database.Store
}

Store is the type for creating and modifying User models in the database.

func NewStore

func NewStore(db *sqlx.DB, mm ...database.Model) *Store

NewStore returns a new Store for querying the users table. Each database passed to this function will be bound to the returned Store.

func (*Store) All

func (s *Store) All(opts ...query.Option) ([]*User, error)

All returns a slice of User models, applying each query.Option that is given.

func (*Store) Auth

func (s *Store) Auth(handle, password string) (*User, error)

Auth looks up the user by the given handle, and checks that the given password matches the hash in the database.

func (*Store) Bind

func (s *Store) Bind(_ ...database.Model)

Bind implements the database.Model interface. This does nothing.

func (*Store) Create

func (s *Store) Create(email, username string, password []byte) (*User, string, error)

Create creates a new user with the given email, username and password. The given password is hashed via bcrypt using the default cost.

func (*Store) Delete

func (s *Store) Delete(id int64, password []byte) error

Delete the user with the given id. This will set the deleted_at field in the table to the time at which this method was called.

func (*Store) Get

func (s *Store) Get(opts ...query.Option) (*User, error)

Get returns a single User database, applying each query.Option that is given.

func (*Store) Load

func (s *Store) Load(key string, vals []interface{}, load database.LoaderFunc) error

Load loads in a slice of User models where the given key is in the list of given vals. Each database is loaded individually via a call to the given load callback.

func (*Store) New

func (*Store) New() *User

New returns a new zero-value User model.

func (*Store) RequestVerify

func (s *Store) RequestVerify(id int64) (string, error)

func (*Store) ResetPassword

func (s *Store) ResetPassword(id int64) (string, error)

func (*Store) Update

func (s *Store) Update(id int64, email string, cleanup bool, password []byte) error

Update sets the email, cleanup, and password fields for the given user to the given values. If the given password is nil, then this will not be updated, otherwise a new hash is generated for it.

func (*Store) UpdatePassword

func (s *Store) UpdatePassword(tok string, password []byte) error

func (*Store) Verify

func (s *Store) Verify(tok string) error

type User

type User struct {
	ID        int64        `db:"id"`
	Email     string       `db:"email"`
	Username  string       `db:"username"`
	Password  []byte       `db:"password"`
	Verified  bool         `db:"verified"`
	Cleanup   bool         `db:"cleanup"`
	CreatedAt time.Time    `db:"created_at"`
	UpdatedAt time.Time    `db:"updated_at"`
	DeletedAt sql.NullTime `db:"deleted_at"`

	Permissions map[string]struct{} `db:"-"`
}

User represents a user account in the database. This will either be created through registration, or sign-on via an OAuth provider.

func FromContext

func FromContext(ctx context.Context) (*User, bool)

FromContext returns the *User database from the given context value, if any.

func (*User) Bind

func (*User) Bind(_ ...database.Model)

Bind implements the database.Model interface. This does nothing.

func (*User) Endpoint

func (*User) Endpoint(_ ...string) string

Endpoint implements the database.Model interface. This returns an empty string.

func (*User) IsZero

func (u *User) IsZero() bool

IsZero implements the database.Model interface.

func (*User) JSON

func (u *User) JSON(_ string) map[string]interface{}

JSON implements the database.Model interface. This will return a map with the values of the current user under each key. This will not include the password field.

func (*User) Primary

func (u *User) Primary() (string, int64)

Primary implements the database.Model interface.

func (*User) SetPermission

func (u *User) SetPermission(perm string)

SetPermission set's the given permission in the underlying Permissions map of the current User. If the map is nil then it will be initialized.

func (*User) SetPrimary

func (u *User) SetPrimary(id int64)

SetPrimary implements the database.Model interface.

func (*User) Values

func (u *User) Values() map[string]interface{}

Values implements the databae.Model interface. This will return a map with the following values, email, username, password, updated_at, and deleted_at.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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