~netlandish/gobwebs

44ee572f3d84010dcfa67506baa93d91fb9327d7 — Peter Sanchez 1 year, 9 months ago 1e14c8a
More cleanup for new DBI changes
2 files changed, 8 insertions(+), 11 deletions(-)

M accounts/confirm.go
M interfaces.go
M accounts/confirm.go => accounts/confirm.go +5 -8
@@ 47,9 47,9 @@ func NewConfirmation(ctype int, user gobwebs.User, expires time.Time) *Confirmat
}

// GetConfirmation will fetch from the db using the given key
func GetConfirmation(db *sql.DB, key string) (*Confirmation, error) {
func GetConfirmation(ctx context.Context, key string) (*Confirmation, error) {
	c := &Confirmation{Key: key}
	if err := c.Load(db); err != nil {
	if err := c.Load(ctx); err != nil {
		return nil, err
	}
	return c, nil


@@ 64,11 64,10 @@ func (c *Confirmation) IsValid(ctype int) bool {
}

// Load will load a confirmation using the Key value
func (c *Confirmation) Load(db *sql.DB) error {
func (c *Confirmation) Load(ctx context.Context) error {
	if c.Key == "" {
		return fmt.Errorf("Can not load without a Key value")
	}
	ctx := database.Context(context.Background(), db)
	if err := database.WithTx(ctx, database.TxOptionsRO, func(tx *sql.Tx) error {
		row := tx.QueryRowContext(ctx, `
			SELECT 


@@ 96,14 95,13 @@ func (c *Confirmation) Load(db *sql.DB) error {
}

// Store saves a new confirmation to the database.
func (c *Confirmation) Store(db *sql.DB) error {
func (c *Confirmation) Store(ctx context.Context) error {
	if c.Type == 0 || c.UserID == 0 {
		return fmt.Errorf("Can not store confirmation with either Type or UserID missing")
	}
	if c.Key == "" {
		c.Key = ksuid.New().String()
	}
	ctx := database.Context(context.Background(), db)
	if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		var tid uint
		for {


@@ 157,7 155,7 @@ func (c *Confirmation) Store(db *sql.DB) error {
}

// Confirm sets `confirm_time`, essentially confirming the confimration request
func (c *Confirmation) Confirm(db *sql.DB) error {
func (c *Confirmation) Confirm(ctx context.Context) error {
	// The requirement for ID is because we want the confirmation to be
	// loaded and verify it's valid before confirming. No shortcuts allowed
	// as that will just lead to trouble


@@ 165,7 163,6 @@ func (c *Confirmation) Confirm(db *sql.DB) error {
		return fmt.Errorf("Can not confirm an empty confirmation")
	}

	ctx := database.Context(context.Background(), db)
	if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		row := tx.QueryRowContext(ctx, `
			UPDATE "confirmations" 

M interfaces.go => interfaces.go +3 -3
@@ 1,7 1,7 @@
package gobwebs

import (
	"database/sql"
	"context"
	"time"

	"github.com/labstack/echo/v4"


@@ 39,10 39,10 @@ type User interface {
// Generally used by accounts and middlewares
type UserFetch interface {
	GetUser() User
	FromDB(db *sql.DB, uid any, markAuth bool) (User, error)
	FromDB(ctx context.Context, uid any, markAuth bool) (User, error)
	FromContext(c echo.Context) User

	WritePassword(db *sql.DB, user User) error
	WritePassword(ctx context.Context, user User) error

	// Helpers to customize the behavior of accounts handlers
	LoginType() int