accessreq

package
v1.153.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package accessreq is the request for access a signed-in person makes from a link to an asset, collection or prompt they cannot open (#2090), and the owner's answer to it. Approving creates an ordinary person share, so the link the requester followed opens with no further step; declining closes the request. A requester has one pending request per item at a time, asks a few times a day at most, and a request expires after the access_requests setting's period.

Index

Constants

View Source
const (
	StatusPending  = "pending"
	StatusApproved = "approved"
	StatusDeclined = "declined"
	// StatusExpired is reported, never stored: a pending request past its
	// expiry reads as expired.
	StatusExpired = "expired"
)

Statuses.

View Source
const (
	TargetAsset      = "asset"
	TargetCollection = "collection"
	TargetPrompt     = "prompt"
	// TargetResource is a managed resource, a file (#2258).
	TargetResource = "resource"
)

Target types.

View Source
const (
	DefaultExpireDays = 14

	// MaxPerDay is how many requests one person makes for one item in a day.
	MaxPerDay = 3
)

Defaults and bounds.

View Source
const SettingsSection = "access_requests"

SettingsSection is the platform_settings section holding Settings.

Variables

View Source
var (
	ErrNotFound = errors.New("access request not found")
	ErrTooMany  = errors.New("you have asked for access to this item too often today; try again tomorrow")
	ErrDecided  = errors.New("the request was already answered")
)

Errors a caller maps to a status.

Functions

func CleanMessage

func CleanMessage(m string) (string, error)

CleanMessage trims a requester's note and refuses one too long.

func Register

func Register(mux *http.ServeMux, wrap func(http.Handler) http.Handler, d Deps)

Register mounts the routes, each behind wrap, the portal's authentication.

func RegisterSettings

func RegisterSettings(mux *http.ServeMux, wrap func(http.Handler) http.Handler, store SettingsStore, author func(*http.Request) string)

RegisterSettings mounts the admin settings routes behind wrap.

Types

type DecideRequest

type DecideRequest struct {
	// Permission is viewer or editor.
	Permission string `json:"permission" example:"viewer"`
}

DecideRequest is the body of an approval.

type Decision

type Decision struct {
	Status     string
	Permission string
	DecidedBy  string
	ShareID    string
}

Decision is the answer to a request: approved with a permission and the share it created, or declined.

type Deps

type Deps struct {
	Store Requests
	// Resolve reads an item; an error is an item that does not exist.
	Resolve func(ctx context.Context, targetType, id string) (Target, error)
	// CanOpen reports whether the user already opens the item.
	CanOpen func(ctx context.Context, user *access.User, targetType, id string) bool
	// IsAdmin is the platform's administrator rule.
	IsAdmin func(user *access.User) bool
	// CanShare reports whether the user's persona lets them share what they
	// own (persona.SharingAllowed): an approval writes a share, so an owner
	// whose persona withholds sharing cannot approve (#2257). An
	// administrator always can. Nil admits every owner.
	CanShare func(user *access.User) bool
	// Share creates the person share an approval grants and returns its id.
	Share func(ctx context.Context, targetType, id, createdBy, recipientEmail, permission string) (string, error)
	// NotifyOwner tells the owner a request is waiting; NotifyRequester tells
	// the requester how it was answered. Either may be nil.
	NotifyOwner     func(ctx context.Context, req *Request, itemName string)
	NotifyRequester func(ctx context.Context, req *Request, itemName string)
	// Settings reads the stored configuration; nil uses Defaults.
	Settings func(ctx context.Context) Settings
}

Deps is what the routes are built from.

type OpenRequest

type OpenRequest struct {
	Message string `json:"message,omitempty" example:"I need this for the quarterly review"`
}

OpenRequest is the body of a request for access.

type Page

type Page struct {
	// HasAccess is true when the person already opens the item.
	HasAccess bool `json:"has_access"`
	// Name is the item's name, only when the deployment reveals names.
	Name string `json:"name,omitempty"`
	// OwnerEmail is who a request goes to.
	OwnerEmail string `json:"owner_email"`
	// Pending is the person's live request for the item, if any.
	Pending *Request `json:"pending,omitempty"`
}

Page is what the page a person who cannot open an item shows.

type Request

type Request struct {
	ID             string     `json:"id"`
	TargetType     string     `json:"target_type" example:"asset"`
	TargetID       string     `json:"target_id"`
	RequesterID    string     `json:"requester_id"`
	RequesterEmail string     `json:"requester_email"`
	OwnerEmail     string     `json:"owner_email"`
	Message        string     `json:"message,omitempty"`
	Status         string     `json:"status" example:"pending"`
	Permission     string     `json:"permission,omitempty" example:"viewer"`
	DecidedBy      string     `json:"decided_by,omitempty"`
	DecidedAt      *time.Time `json:"decided_at,omitempty"`
	ShareID        string     `json:"share_id,omitempty"`
	CreatedAt      time.Time  `json:"created_at"`
	ExpiresAt      time.Time  `json:"expires_at"`
	// TargetName is the item's name, filled for its owner.
	TargetName string `json:"target_name,omitempty"`
}

Request is one request for access.

type RequestList

type RequestList struct {
	Requests []Request `json:"requests"`
}

RequestList is an owner's waiting requests.

type Requests

type Requests interface {
	Open(ctx context.Context, r Request) (*Request, bool, error)
	Pending(ctx context.Context, targetType, targetID, requesterID string) (*Request, error)
	Get(ctx context.Context, id string) (*Request, error)
	ListForOwner(ctx context.Context, ownerEmail, targetType, targetID string, all bool) ([]Request, error)
	Decide(ctx context.Context, id string, d Decision) error
}

Requests is the store the routes read and write; *Store implements it.

type Settings

type Settings struct {
	// RevealNames shows the item's name on the page a person who cannot
	// open it sees; otherwise the page has a generic title.
	RevealNames bool `json:"reveal_names" example:"false"`
	// ExpireDays is how long a request waits for an answer.
	ExpireDays int `json:"expire_days" example:"14"`
}

Settings is the stored configuration.

func Defaults

func Defaults() Settings

Defaults is the configuration before an administrator writes the section.

func (Settings) Validate

func (s Settings) Validate() error

Validate refuses a period out of range.

type SettingsStore

type SettingsStore interface {
	Read(ctx context.Context) Settings
	Write(ctx context.Context, s Settings, author string) error
}

SettingsStore reads and writes the section.

type Store

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

Store keeps the requests.

func NewStore

func NewStore(db *sql.DB) *Store

NewStore returns the store over db.

func (*Store) Decide

func (s *Store) Decide(ctx context.Context, id string, d Decision) error

Decide records the answer to a pending request, once.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (*Request, error)

Get returns one request.

func (*Store) ListForOwner

func (s *Store) ListForOwner(ctx context.Context, ownerEmail, targetType, targetID string, all bool) ([]Request, error)

ListForOwner returns the live pending requests for items the address owns, or for one item when targetType and targetID are set; with all, every request on the platform (an administrator's list).

func (*Store) Open

func (s *Store) Open(ctx context.Context, r Request) (*Request, bool, error)

Open records a pending request, or returns the one already pending for the requester and item with created false. A requester who has asked MaxPerDay times for the item in the last day is refused.

func (*Store) Pending

func (s *Store) Pending(ctx context.Context, targetType, targetID, requesterID string) (*Request, error)

Pending reports the live pending request for the requester and item, or ErrNotFound.

type Target

type Target struct {
	Name       string
	OwnerID    string
	OwnerEmail string
}

Target is the item a request is about, as its owner holds it.

Jump to

Keyboard shortcuts

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