users

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultRoleID int64 = 2

Variables

View Source
var (
	ErrNoUserID      = errors.New("this operation requires a user id")
	ErrNotDerivedKey = errors.New("passwords must be stored as a derived key")
)

Standard errors for database operations and checking.

Functions

This section is empty.

Types

type Permission

type Permission struct {
	ID          int64
	Title       string
	Description sql.NullString
	Created     time.Time
	Modified    time.Time
}

Database model for a permission object.

type Role

type Role struct {
	ID          int64
	Title       string
	Description sql.NullString
	Created     time.Time
	Modified    time.Time
	// contains filtered or unexported fields
}

Database model for a role object.

func GetRole

func GetRole(ctx context.Context, id int64) (role *Role, err error)

func (*Role) Permissions

func (r *Role) Permissions(ctx context.Context, reset bool) (_ []*Permission, err error)

type User

type User struct {
	ID              int64
	FullName        sql.NullString
	Email           string
	Username        string
	Password        string
	RoleID          int64
	LastSeen        sql.NullTime
	PasswordChanged sql.NullTime
	Created         time.Time
	Modified        time.Time
	// contains filtered or unexported fields
}

Database model for a user object.

func UserFromEmail

func UserFromEmail(ctx context.Context, email string, claims bool) (user *User, err error)

UserFromEmail gets a user and populates the role and permissions if claims is true. This function returns the password for login purposes unless claims is false.

func UserFromID

func UserFromID(ctx context.Context, id int64) (user *User, err error)

UserFromID gets a user without password or claims and should be used for non-login purposes.

func UserFromUsername

func UserFromUsername(ctx context.Context, username string, claims bool) (user *User, err error)

UserFromUsername gets a user and populates the role and permissions if claims is true. This function returns the password for login purposes unless claims is false.

func (*User) Create

func (u *User) Create(ctx context.Context) (err error)

Create a user from the model. The ID, Created, and Modified timestamps will be populated on the model after creation. Note that the LastSeen timestamp is ignored and the PasswordChange timestamp is set to now (no matter what it was set to before). TODO: handle specific database errors like uniqueness constraints or other schema errors.

func (*User) Permissions

func (u *User) Permissions(ctx context.Context, reset bool) (_ []string, err error)

Permissions returns the claims associated with the user. If the permissions are not cached on the struct, then a database query to the user_permissions view is executed. Use the reset bool to force a database query even if the permissions are cached.

func (*User) Role

func (u *User) Role(ctx context.Context, reset bool) (_ *Role, err error)

Role returns the role associated with the user. If the role is not cached on the struct then a database query is performed and an error may be returned. Use the reset bool to force a database query even if the role is cached on the struct.

func (*User) Update

func (u *User) Update(ctx context.Context) (err error)

Update a user's full name, email, username, role id, and/or last seen timestamp. The user must have an ID field populated or ErrNoUserID will be returned.

func (*User) UpdateLastSeen

func (u *User) UpdateLastSeen(ctx context.Context) (err error)

Update a user's last seen timestamp to now. The user must have an ID field populated or ErrNotDerivedKey will be returned.

func (*User) UpdatePassword

func (u *User) UpdatePassword(ctx context.Context) (err error)

Update a user's password. The user must have an ID field populated and the Password must be a derived key or ErrNoUserID or ErrNotDerivedKey will be returned.

Jump to

Keyboard shortcuts

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