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 ¶
- Variables
- type Attrs
- type Filter
- type Listed
- type Page
- type Scenario
- func (s Scenario) Ban(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- func (s Scenario) Create(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- func (s Scenario) Delete(ctx context.Context, account domain.Account, principal access.Principal, ...) error
- func (s Scenario) Group(ctx context.Context, account domain.Account, principal access.Principal, ...) (*domain.Group, error)
- func (s Scenario) List(ctx context.Context, account domain.Account, principal access.Principal, ...) (Page, error)
- func (s Scenario) Me(ctx context.Context, accountID, id uuid.UUID) (domain.User, string, error)
- func (s Scenario) Related(ctx context.Context, account domain.Account, principal access.Principal, ...) ([]uuid.UUID, error)
- func (s Scenario) RelatedShow(ctx context.Context, account domain.Account, principal access.Principal, ...) (uuid.UUID, error)
- func (s Scenario) RelatedUser(ctx context.Context, account domain.Account, principal access.Principal, ...) (domain.User, error)
- func (s Scenario) ResetPassword(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- func (s Scenario) SeedAdmin(ctx context.Context, accountID uuid.UUID, email string) error
- func (s Scenario) SetGroup(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- func (s Scenario) Show(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- func (s Scenario) Unban(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- func (s Scenario) Update(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- func (s Scenario) UpdatePassword(ctx context.Context, account domain.Account, principal access.Principal, ...) (Listed, error)
- type Store
Constants ¶
This section is empty.
Variables ¶
var ErrEmailTaken = errors.New("users: email taken")
ErrEmailTaken is the unique-index violation of (account, email).
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 Scenario ¶
type Scenario struct {
// contains filtered or unexported fields
}
Scenario runs the user scenarios.
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) 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 ¶
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.