users

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package users manages accounts: creating, changing and deleting users, their grants, invites and the first-run setup that creates the admin.

Index

Constants

View Source
const DefaultInviteTTL = 72 * time.Hour

DefaultInviteTTL is how long an invite stays valid unless set.

Variables

View Source
var (
	ErrNotFound         = errors.New("users: not found")
	ErrUsernameTaken    = errors.New("users: username taken")
	ErrLastAdmin        = errors.New("users: the last enabled admin can't be disabled, demoted or deleted")
	ErrCannotChangeSelf = errors.New("users: admins can't disable, demote or delete themselves")
	ErrAlreadySetUp     = errors.New("users: the first account already exists")
	ErrInviteInvalid    = errors.New("users: invite unknown, used or expired")
)

Errors callers map to responses.

View Source
var ContentRatings = []string{"safe", "suggestive", "erotic", "pornographic"}

Content ratings, mildest first.

Functions

This section is empty.

Types

type Grants

type Grants struct {
	MayAdd               bool     `json:"may_add"`
	MayDownload          bool     `json:"may_download"`
	MayManageMatches     bool     `json:"may_manage_matches"`
	ContentRatingCeiling string   `json:"content_rating_ceiling"`
	AllSources           bool     `json:"all_sources"`
	AllowedSources       []string `json:"allowed_sources"`
}

Grants say what a user may do. Admins may do everything regardless.

func AdminGrants

func AdminGrants() Grants

AdminGrants are what an admin may do: everything.

func DefaultGrants

func DefaultGrants() Grants

DefaultGrants are a new user's grants unless the admin sets others: browse every source, add to their own library and read, up to suggestive content; no downloads and no match fixing.

func Effective

func Effective(u db.User) Grants

Effective returns what u may actually do: admins get everything.

func GrantsOf

func GrantsOf(u db.User) Grants

GrantsOf returns u's stored grants.

func InviteGrants

func InviteGrants(inv db.Invite) (Grants, error)

InviteGrants decodes an invite's grants.

func (Grants) Validate

func (g Grants) Validate() error

Validate checks the grants' values.

type GrantsPatch

type GrantsPatch struct {
	MayAdd               *bool
	MayDownload          *bool
	MayManageMatches     *bool
	ContentRatingCeiling *string
	AllSources           *bool
	AllowedSources       *[]string
}

GrantsPatch changes some grants; nil fields stay.

func (*GrantsPatch) Apply

func (p *GrantsPatch) Apply(g Grants) Grants

Apply returns g with p applied.

type NewInvite

type NewInvite struct {
	Role   string
	Grants Grants
	Note   string
	TTL    time.Duration
}

NewInvite is an invite to create.

type NewUser

type NewUser struct {
	Username    string
	Password    string
	DisplayName string
	Role        string
	Grants      Grants
}

NewUser is a user to create.

type Patch

type Patch struct {
	DisplayName *string
	Role        *string
	Disabled    *bool
	Password    *string
	Grants      *GrantsPatch
}

Patch changes some of a user's fields; nil fields stay.

type Service

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

Service manages accounts.

func NewService

func NewService(st *store.Store, a *auth.Service, log *slog.Logger) *Service

NewService returns the users service.

func (*Service) AcceptInvite

func (s *Service) AcceptInvite(ctx context.Context, token string, n NewUser, ci auth.ClientInfo) (db.User, auth.NewSession, error)

AcceptInvite creates the account an invite allows, uses the invite up and signs the new user in. n's role and grants are ignored: they come from the invite.

func (*Service) CheckInvite

func (s *Service) CheckInvite(ctx context.Context, token string) (db.Invite, error)

CheckInvite returns the open invite for token.

func (*Service) CompleteSetup

func (s *Service) CompleteSetup(ctx context.Context, actor uuid.UUID) (SetupStatus, error)

CompleteSetup marks the wizard finished. Finishing it again keeps the first time.

func (*Service) CreateFirstAdmin

func (s *Service) CreateFirstAdmin(ctx context.Context, n NewUser, ci auth.ClientInfo) (db.User, auth.NewSession, error)

CreateFirstAdmin creates the first account as an admin and signs it in. Once any account exists it fails with ErrAlreadySetUp, also when two requests race.

func (*Service) CreateInvite

func (s *Service) CreateInvite(ctx context.Context, actor uuid.UUID, n NewInvite) (db.Invite, string, error)

CreateInvite creates an invite. Its token is returned only here.

func (*Service) CreateUser

func (s *Service) CreateUser(ctx context.Context, actor uuid.UUID, n NewUser) (db.User, error)

CreateUser creates a user (an admin's action).

func (*Service) DeleteInvite

func (s *Service) DeleteInvite(ctx context.Context, actor, id uuid.UUID) error

DeleteInvite revokes an invite.

func (*Service) DeleteUser

func (s *Service) DeleteUser(ctx context.Context, actor, id uuid.UUID) error

DeleteUser deletes a user with their sessions and keys.

func (*Service) GetUser

func (s *Service) GetUser(ctx context.Context, id uuid.UUID) (db.User, error)

GetUser returns a user.

func (*Service) HasUsers

func (s *Service) HasUsers(ctx context.Context) (bool, error)

HasUsers reports whether any account exists (the first-run gate).

func (*Service) ListInvites

func (s *Service) ListInvites(ctx context.Context) ([]db.Invite, error)

ListInvites lists open invites.

func (*Service) ListUsers

func (s *Service) ListUsers(ctx context.Context, after string, limit int) ([]db.User, string, error)

ListUsers returns up to limit users after the cursor (a lowercased username), and the cursor of the next page ("" on the last).

func (*Service) SetupStatus

func (s *Service) SetupStatus(ctx context.Context) (SetupStatus, error)

SetupStatus reports the wizard's state.

func (*Service) UpdateUser

func (s *Service) UpdateUser(ctx context.Context, actor, id uuid.UUID, p Patch) (db.User, error)

UpdateUser changes a user (an admin's action, or the user's own display name). Disabling or resetting the password ends the user's sessions.

type SetupStatus

type SetupStatus struct {
	AdminCreated bool
	CompletedAt  *time.Time
}

SetupStatus is where the first-run wizard stands.

Jump to

Keyboard shortcuts

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