db

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoUser = errors.New("user not found")

ErrNoUser is returned by if the user or admin account is not found.

Functions

func ApplyMigrations

func ApplyMigrations(dbURI string) error

ApplyMigrations ensures all migrations are applied to the database at dbURI.

Types

type AdminTable

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

AdminTable represents the table of admins permitted to use the service. It handles password hashing, so all `pass` method arguments are expected to be plaintext.

func NewAdminTable

func NewAdminTable(l log.Logger, pool PgxIface) *AdminTable

NewAdminTable creates a new AdminTable backed by a Postgres connection pool.

func (*AdminTable) AddAdmin

func (a *AdminTable) AddAdmin(ctx context.Context, user, pass string) error

AddAdmin creates a new admin account.

func (*AdminTable) ChangePassword

func (a *AdminTable) ChangePassword(ctx context.Context, user, pass string) error

ChangePassword updates the hashed password for an admin. ErrNoUser is returned if the admin is not in the database.

func (*AdminTable) CheckPassword

func (a *AdminTable) CheckPassword(ctx context.Context, user, pass string) (bool, error)

CheckPassword returns true if the admin exists and the password matches the hash on file. Otherwise it returns false.

func (*AdminTable) DeleteAdmin

func (a *AdminTable) DeleteAdmin(ctx context.Context, user string) error

DeleteAdmin removes an admin account. It does not affect any changes they may have made to the data.

type FilterOption

type FilterOption = func(f *filters)

FilterOption is a way to filter by particular values with GetUsers.

func WithKeyHash

func WithKeyHash(keyHash string) FilterOption

WithKeyHash returns a FilterOption that filters by the provided MD5 key hash.

type PgxIface

type PgxIface interface {
	Begin(context.Context) (pgx.Tx, error)
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	Ping(context.Context) error
	Close()
}

PgxIface allows the Table objects to use anything that looks like a *pgxpool.Pool.

type User

type User struct {
	ID                int    `json:"id"`
	Name              string `json:"name"`
	NameKeyHash       string `json:"name_key_hash"`
	FinishYear        int    `json:"finish_year"`
	Professor         bool   `json:"professor"`
	TA                bool   `json:"ta"`
	StudentLeadership bool   `json:"student_leadership"`
	AlumniBoard       bool   `json:"alumni_board"`
}

User contains the information about a user necessary to admit them to the Discord server and assign appropriate roles upon entry to the server.

type UserTable

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

UserTable represents the table of users that the bouncer will accept into the Discord server.

func NewUserTable

func NewUserTable(l log.Logger, pool PgxIface) *UserTable

NewUserTable creates a new UserTable backed by a Postgres connection pool.

func (*UserTable) CreateUser

func (t *UserTable) CreateUser(ctx context.Context, u *User) (int, error)

CreateUser creates a new user (ignoring the ID field) and returns the new ID.

func (*UserTable) DeleteUser

func (t *UserTable) DeleteUser(ctx context.Context, id int) error

DeleteUser removes the user by ID. If the user did not exist, returns ErrNoUser.

func (*UserTable) GetUser

func (t *UserTable) GetUser(ctx context.Context, id int) (*User, error)

GetUser returns the user by ID, if present. If not present, ErrNoUser is returned.

func (*UserTable) GetUsers

func (t *UserTable) GetUsers(ctx context.Context, opts ...FilterOption) ([]*User, error)

GetUsers returns all users in the database.

func (*UserTable) UpdateUser

func (t *UserTable) UpdateUser(ctx context.Context, u *User) error

UpdateUser inserts the information in u into the row identified by u.ID. If that row does not exist, ErrNoUser is returned.

Jump to

Keyboard shortcuts

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