users

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package users is the user scenario: the bootstrap admin seed, the status computation, CRUD, the password/ban actions and the relationship reads (me/profile included).

Index

Constants

This section is empty.

Variables

View Source
var ErrEmailTaken = errors.New("users: email taken")

ErrEmailTaken is the unique-index violation of (account, email).

View Source
var ErrNotFound = domain.ErrNotFound // one sentinel across the scenario ports

ErrNotFound is the storage port's not-found sentinel.

Functions

This section is empty.

Types

type Attrs

type Attrs struct {
	FirstName *string
	LastName  *string
	Email     *string
	// Password is nil when absent or explicitly null; PasswordSet distinguishes them.
	Password    *string
	PasswordSet bool
	Metadata    map[string]any
	// Role is the requested role (wire form); empty keeps the current
	// one (or "user" on create).
	Role string
	// GroupSet reports whether the group relationship was addressed;
	// Group carries its value (zero = remove).
	GroupSet bool
	Group    uuid.NullUUID
}

Attrs is the decoded write payload of POST/PATCH /users (the handler has already enforced the typed-params shape, including who may send role/password/metadata/group at all).

type Filter

type Filter struct {
	// IDs narrows the scope to a search's matches; nil keeps it whole.
	IDs []uuid.UUID
	// Roles defaults to ["user"] on the wire (the handler applies the
	// default before calling).
	Roles []string
	// GroupID/ProductID/LicenseID carry the FK filters.
	GroupID   uuid.NullUUID
	ProductID uuid.NullUUID
	LicenseID uuid.NullUUID
	// Status filters by the computed status (ACTIVE/INACTIVE/BANNED).
	Status string
	// Assigned filters on owning at least one license.
	Assigned *bool
	// Metadata is a jsonb containment filter (keys underscored).
	Metadata map[string]any
}

Filter is the users index filter set (every field optional).

type Listed

type Listed struct {
	User   domain.User
	Status string
}

Listed is a page row: the user plus her computed status.

type Page

type Page struct {
	Rows  []Listed
	Total int
}

Page is one result page with its total (offset pagination meta).

type Scenario

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

Scenario runs the user scenarios.

func New

func New(store Store, publisher events.Publisher, clock func() time.Time) Scenario

New builds the scenario (events publishes the user webhook events).

func (Scenario) Ban

func (s Scenario) Ban(
	ctx context.Context, account domain.Account, principal access.Principal, alias string,
) (Listed, error)

func (Scenario) Create

func (s Scenario) Create(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	attrs Attrs,
) (Listed, error)

Create validates and writes a new user, answering the row with her computed status.

func (Scenario) Delete

func (s Scenario) Delete(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias string,
) error

Delete removes the user (the last admin is undeletable).

func (Scenario) Group

func (s Scenario) Group(
	ctx context.Context, account domain.Account, principal access.Principal, alias string,
) (*domain.Group, error)

Group answers the user's group (nil when she has none).

func (Scenario) List

func (s Scenario) List(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	f Filter,
	p paging.Params,
) (Page, error)

List answers the filtered, paginated page of the principal's scope.

func (Scenario) Me

func (s Scenario) Me(ctx context.Context, accountID, id uuid.UUID) (domain.User, string, error)

Me loads the bearer's user with her computed status.

func (Scenario) Related

func (s Scenario) Related(
	ctx context.Context, account domain.Account, principal access.Principal, alias, kind string,
) ([]uuid.UUID, error)

Related answers the ids of the user's related records of one kind, under that relationship's policy.

func (Scenario) RelatedShow

func (s Scenario) RelatedShow(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias, kind, recordAlias string,
) (uuid.UUID, error)

RelatedShow answers one related record's id (the record must belong to the user's relation).

func (Scenario) RelatedUser

func (s Scenario) RelatedUser(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias, kind string,
) (domain.User, error)

RelatedUser resolves and authorizes the user of a relationship route (the nested license routes of the licenses group reuse it).

func (Scenario) ResetPassword

func (s Scenario) ResetPassword(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias, resetToken, newPassword string,
) (Listed, error)

ResetPassword resets the password with the emailed reset token (anti-enumeration: a token mismatch is the user 404), revoking every token of the user.

func (Scenario) SeedAdmin

func (s Scenario) SeedAdmin(ctx context.Context, accountID uuid.UUID, email string) error

SeedAdmin creates the passwordless test admin when her email is not taken. Testkit issues its bearer directly without password authentication.

func (Scenario) SetGroup

func (s Scenario) SetGroup(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias string,
	groupID uuid.NullUUID,
) (Listed, error)

SetGroup attaches the user to a group (zero UUID removes), answering the updated user.

func (Scenario) Show

func (s Scenario) Show(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias string,
) (Listed, error)

Show loads the user by id-or-email alias, enforcing the reference's scoped lookup (an invisible user is a 404).

func (Scenario) Unban

func (s Scenario) Unban(
	ctx context.Context, account domain.Account, principal access.Principal, alias string,
) (Listed, error)

Unban clears bannedAt.

func (Scenario) Update

func (s Scenario) Update(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias string,
	attrs Attrs,
) (Listed, error)

Update applies the write payload to the user.

func (Scenario) UpdatePassword

func (s Scenario) UpdatePassword(
	ctx context.Context,
	account domain.Account,
	principal access.Principal,
	alias, oldPassword, newPassword string,
) (Listed, error)

UpdatePassword changes the bearer's own password (oldPassword verified), revoking every other token of the user.

type Store

type Store interface {
	// Do runs fn in one transaction; the writes and the webhook events
	// of one action commit together.
	Do(ctx context.Context, fn func(ctx context.Context) error) error
	// UserByID loads a user; ErrNotFound when none.
	UserByID(ctx context.Context, accountID, id uuid.UUID) (domain.User, error)
	// UserByEmail loads a user by downcased email; ErrNotFound.
	UserByEmail(ctx context.Context, accountID uuid.UUID, email string) (domain.User, error)
	// InsertUser writes a new user row.
	InsertUser(ctx context.Context, user domain.User) error
	// UpdateUser writes only the addressed attributes, preserving concurrent auth state.
	UpdateUser(ctx context.Context, user domain.User, attrs Attrs) error
	// DeleteUser removes the row.
	DeleteUser(ctx context.Context, accountID, id uuid.UUID) error
	// UserHasActiveLicense answers the user's status input.
	UserHasActiveLicense(ctx context.Context, accountID, id uuid.UUID) (bool, error)
	// ListUsers answers the filtered page (with computed statuses).
	ListUsers(ctx context.Context, accountID uuid.UUID, f Filter, scopeKind string,
		scopeID uuid.UUID, p paging.Params,
	) ([]Listed, error)
	// CountUsers answers the filter's total.
	CountUsers(ctx context.Context, accountID uuid.UUID, f Filter, scopeKind string,
		scopeID uuid.UUID,
	) (int, error)
	// UserIsTeammate answers whether other shares a license with user.
	UserIsTeammate(ctx context.Context, accountID, userID, otherID uuid.UUID) (bool, error)
	// UserOfLicense answers whether the user owns or is attached to the
	// license.
	UserOfLicense(ctx context.Context, accountID, licenseID, userID uuid.UUID) (bool, error)
	// CountAdmins answers the account's admin count.
	CountAdmins(ctx context.Context, accountID uuid.UUID) (int, error)
	// UpdateUserPassword writes a new password digest and clears the
	// reset fields.
	UpdateUserPassword(ctx context.Context, accountID, id uuid.UUID, digest string) error
	// SetUserPasswordReset stores the reset token digest and timestamp.
	SetUserPasswordReset(ctx context.Context, accountID, id uuid.UUID, token string, sentAt time.Time) error
	// SetUserBannedAt writes (or clears) the ban timestamp.
	SetUserBannedAt(ctx context.Context, accountID, id uuid.UUID, bannedAt *time.Time) error
	// DeleteUserTokens revokes every token of the user.
	DeleteUserTokens(ctx context.Context, accountID, userID uuid.UUID) error
	// DeleteUserTokensExcept revokes every token of the user but the
	// acting one.
	DeleteUserTokensExcept(ctx context.Context, accountID, userID, exceptID uuid.UUID) error
	// CountGroupUsers answers the group's member count.
	CountGroupUsers(ctx context.Context, accountID, groupID uuid.UUID) (int, error)
	// GroupByID loads a group; ErrNotFound.
	GroupByID(ctx context.Context, accountID, id uuid.UUID) (domain.Group, error)
	// UserLicenseIDs answers the user's license ids (owned or attached).
	UserLicenseIDs(ctx context.Context, accountID, userID uuid.UUID) ([]uuid.UUID, error)
	// UserMachineIDs answers the user's machine ids.
	UserMachineIDs(ctx context.Context, accountID, userID uuid.UUID) ([]uuid.UUID, error)
	// UserProductIDs answers the product ids behind the user's licenses.
	UserProductIDs(ctx context.Context, accountID, userID uuid.UUID) ([]uuid.UUID, error)
	// GroupIDsOfUser answers the group ids the user belongs to or owns.
	GroupIDsOfUser(ctx context.Context, accountID, userID uuid.UUID) ([]uuid.UUID, error)
}

Store is the scenario's consumer-side storage port.

Jump to

Keyboard shortcuts

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