admin

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithContext

func WithContext(ctx context.Context, adminContext Context) context.Context

func WriteError

func WriteError(w http.ResponseWriter, status int, requestID, code, message string, fields map[string][]string)

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, requestID string, data any, meta map[string]any)

Types

type Action

type Action struct {
	Resource        *Resource `json:"-"`
	Name            string    `json:"name"`
	Label           string    `json:"label"`
	Description     string    `json:"description,omitempty"`
	Icon            string    `json:"icon,omitempty"`
	Dangerous       bool      `json:"danger"`
	RequiresConfirm bool      `json:"requires_confirmation"`
	RequiresReason  bool      `json:"requires_reason"`
	InputSchema     any       `json:"input_schema,omitempty"`
	Permission      string    `json:"permission,omitempty"`
	Handler         any       `json:"-"`
	Timeout         string    `json:"timeout,omitempty"`
}

func (*Action) Action

func (a *Action) Action(name string, handler any) *Action

func (*Action) Audit

func (a *Action) Audit() *Resource

func (*Action) Can

func (a *Action) Can(permission string) *Action

func (*Action) Danger

func (a *Action) Danger() *Action

func (*Action) Describe

func (a *Action) Describe(description string) *Action

func (*Action) Field

func (a *Action) Field(name string) *Field

func (*Action) Form

func (a *Action) Form(schema any) *Action

func (*Action) Policy

func (a *Action) Policy(policy Policy) *Resource

func (*Action) RequireConfirmation

func (a *Action) RequireConfirmation() *Action

func (*Action) RequireReason

func (a *Action) RequireReason() *Action

func (*Action) TenantScoped

func (a *Action) TenantScoped(column string) *Resource

func (*Action) WithIcon

func (a *Action) WithIcon(icon string) *Action

func (*Action) WithTimeout

func (a *Action) WithTimeout(timeout time.Duration) *Action

type ActionHandler

type ActionHandler func(context.Context, ActionRequest[map[string]any]) (ActionResult, error)

ActionHandler is the runtime-compatible action handler signature.

type ActionRequest

type ActionRequest[T any] struct {
	Context    Context
	Actor      Actor
	Resource   *Resource
	ResourceID string
	Input      T
	Selected   []string
	Reason     string
}

ActionRequest is passed to custom action handlers.

type ActionResult

type ActionResult struct {
	Message  string         `json:"message"`
	Redirect string         `json:"redirect,omitempty"`
	Data     map[string]any `json:"data,omitempty"`
}

ActionResult is a structured response from an action.

type Actor

type Actor struct {
	ID          string   `json:"id"`
	Email       string   `json:"email"`
	Name        string   `json:"name"`
	TenantID    string   `json:"tenant_id"`
	Roles       []string `json:"roles"`
	Permissions []string `json:"permissions"`
}

func (Actor) Can

func (a Actor) Can(permission string) bool

func (Actor) HasRole

func (a Actor) HasRole(role string) bool

type AllowAllPolicy

type AllowAllPolicy struct{}

func (AllowAllPolicy) CanCreate

func (AllowAllPolicy) CanCreate(Context, Actor) bool

func (AllowAllPolicy) CanDelete

func (AllowAllPolicy) CanDelete(Context, Actor, any) bool

func (AllowAllPolicy) CanUpdate

func (AllowAllPolicy) CanUpdate(Context, Actor, any) bool

func (AllowAllPolicy) CanView

func (AllowAllPolicy) CanView(Context, Actor, any) bool

type App

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

App holds the resource registry used by generators and runtime handlers.

func New

func New(name string) *App

New creates an admin application registry.

func (*App) GetResource

func (a *App) GetResource(name string) (*Resource, bool)

GetResource returns a resource by name.

func (*App) Name

func (a *App) Name() string

Name returns the display name of the admin application.

func (*App) Register

func (a *App) Register(resource *Resource)

Register inserts a pre-built resource definition.

func (*App) Resource

func (a *App) Resource(model any) *Resource

Resource registers or returns a resource for the supplied model value.

func (*App) Resources

func (a *App) Resources() []*Resource

Resources returns resources in stable name order.

type Context

type Context struct {
	RequestID string
	TenantID  string
	Request   *http.Request
}

Context carries request-scoped admin metadata.

func FromContext

func FromContext(ctx context.Context) (Context, bool)

type DenyAllPolicy

type DenyAllPolicy struct{}

func (DenyAllPolicy) CanCreate

func (DenyAllPolicy) CanCreate(Context, Actor) bool

func (DenyAllPolicy) CanDelete

func (DenyAllPolicy) CanDelete(Context, Actor, any) bool

func (DenyAllPolicy) CanUpdate

func (DenyAllPolicy) CanUpdate(Context, Actor, any) bool

func (DenyAllPolicy) CanView

func (DenyAllPolicy) CanView(Context, Actor, any) bool

type Error

type Error struct {
	Code    string              `json:"code"`
	Message string              `json:"message"`
	Fields  map[string][]string `json:"fields,omitempty"`
}

type Field

type Field struct {
	Resource            *Resource         `json:"-"`
	Name                string            `json:"name"`
	Label               string            `json:"label"`
	Type                FieldType         `json:"type"`
	RequiredValue       bool              `json:"required"`
	UniqueValue         bool              `json:"unique"`
	NullableValue       bool              `json:"nullable"`
	SearchableValue     bool              `json:"searchable"`
	SortableValue       bool              `json:"sortable"`
	FilterableValue     bool              `json:"filterable"`
	ReadonlyValue       bool              `json:"readonly"`
	HiddenValue         bool              `json:"hidden"`
	HiddenInListValue   bool              `json:"hidden_in_list"`
	HiddenInFormValue   bool              `json:"hidden_in_form"`
	HiddenInDetailValue bool              `json:"hidden_in_detail"`
	PrimaryValue        bool              `json:"primary"`
	TenantKeyValue      bool              `json:"tenant_key"`
	DefaultValueValue   any               `json:"default_value,omitempty"`
	ValidationRules     []string          `json:"validation,omitempty"`
	HelpTextValue       string            `json:"help_text,omitempty"`
	PlaceholderValue    string            `json:"placeholder,omitempty"`
	BadgeColors         map[string]string `json:"badge_colors,omitempty"`
	EnumValues          []string          `json:"enum_values,omitempty"`
	Currency            string            `json:"currency,omitempty"`
	Relation            *Relation         `json:"relation,omitempty"`
	RendererName        string            `json:"renderer,omitempty"`
	FormatterName       string            `json:"formatter,omitempty"`
	ParserName          string            `json:"parser,omitempty"`
}

func (*Field) Action

func (f *Field) Action(name string, handler any) *Action

func (*Field) Audit

func (f *Field) Audit() *Resource

func (*Field) Badge

func (f *Field) Badge() *Field

func (*Field) BadgeColor

func (f *Field) BadgeColor(value, color string) *Field

func (*Field) BelongsTo

func (f *Field) BelongsTo(resource any) *Field

func (*Field) Boolean

func (f *Field) Boolean() *Field

func (*Field) Computed

func (f *Field) Computed() *Field

func (*Field) Date

func (f *Field) Date() *Field

func (*Field) DateRangeFilter

func (f *Field) DateRangeFilter() *Field

func (*Field) DateTime

func (f *Field) DateTime() *Field

func (*Field) Decimal

func (f *Field) Decimal() *Field

func (*Field) DefaultValue

func (f *Field) DefaultValue(value any) *Field

func (*Field) Display

func (f *Field) Display(field string) *Field

func (*Field) Email

func (f *Field) Email() *Field

func (*Field) Enum

func (f *Field) Enum(values ...string) *Field

func (*Field) Field

func (f *Field) Field(name string) *Field

func (*Field) FileUpload

func (f *Field) FileUpload() *Field

func (*Field) Filterable

func (f *Field) Filterable() *Field

func (*Field) Float

func (f *Field) Float() *Field

func (*Field) ForeignKey

func (f *Field) ForeignKey(field string) *Field

func (*Field) Formatter

func (f *Field) Formatter(name string) *Field

func (*Field) HasMany

func (f *Field) HasMany(resource any) *Field

func (*Field) Help

func (f *Field) Help(text string) *Field

func (*Field) Hidden

func (f *Field) Hidden() *Field

func (*Field) HiddenInDetail

func (f *Field) HiddenInDetail() *Field

func (*Field) HiddenInForm

func (f *Field) HiddenInForm() *Field

func (*Field) HiddenInList

func (f *Field) HiddenInList() *Field

func (*Field) ImageUpload

func (f *Field) ImageUpload() *Field

func (*Field) Integer

func (f *Field) Integer() *Field

func (*Field) JSON

func (f *Field) JSON() *Field

func (*Field) JSONB

func (f *Field) JSONB() *Field

func (*Field) Markdown

func (f *Field) Markdown() *Field

func (*Field) Money

func (f *Field) Money(currency string) *Field

func (*Field) Nullable

func (f *Field) Nullable() *Field

func (*Field) Number

func (f *Field) Number() *Field

func (*Field) Parser

func (f *Field) Parser(name string) *Field

func (*Field) Password

func (f *Field) Password() *Field

func (*Field) Placeholder

func (f *Field) Placeholder(text string) *Field

func (*Field) Policy

func (f *Field) Policy(policy Policy) *Resource

func (*Field) Primary

func (f *Field) Primary() *Field

func (*Field) Readonly

func (f *Field) Readonly() *Field

func (*Field) RelationTo

func (f *Field) RelationTo(resource any) *Field

func (*Field) Renderer

func (f *Field) Renderer(name string) *Field

func (*Field) Required

func (f *Field) Required() *Field

func (*Field) RichText

func (f *Field) RichText() *Field

func (*Field) Searchable

func (f *Field) Searchable() *Field

func (*Field) Sortable

func (f *Field) Sortable() *Field

func (*Field) String

func (f *Field) String() *Field

func (*Field) TenantKey

func (f *Field) TenantKey() *Field

func (*Field) TenantScoped

func (f *Field) TenantScoped(column string) *Resource

func (*Field) Text

func (f *Field) Text() *Field

func (*Field) Time

func (f *Field) Time() *Field

func (*Field) UUID

func (f *Field) UUID() *Field

func (*Field) Unique

func (f *Field) Unique() *Field

func (*Field) Validation

func (f *Field) Validation(rules ...string) *Field

type FieldType

type FieldType string
const (
	FieldString   FieldType = "string"
	FieldText     FieldType = "text"
	FieldEmail    FieldType = "email"
	FieldPassword FieldType = "password"
	FieldNumber   FieldType = "number"
	FieldInteger  FieldType = "integer"
	FieldFloat    FieldType = "float"
	FieldDecimal  FieldType = "decimal"
	FieldMoney    FieldType = "money"
	FieldBoolean  FieldType = "boolean"
	FieldEnum     FieldType = "enum"
	FieldStatus   FieldType = "status"
	FieldDate     FieldType = "date"
	FieldDateTime FieldType = "datetime"
	FieldTime     FieldType = "time"
	FieldUUID     FieldType = "uuid"
	FieldJSON     FieldType = "json"
	FieldJSONB    FieldType = "jsonb"
	FieldMarkdown FieldType = "markdown"
	FieldRichText FieldType = "rich_text"
	FieldImage    FieldType = "image"
	FieldFile     FieldType = "file"
	FieldRelation FieldType = "relation"
	FieldComputed FieldType = "computed"
)

type Filter

type Filter struct {
	Field    string `json:"field"`
	Operator string `json:"operator"`
	Value    any    `json:"value"`
}

type Policy

type Policy interface {
	CanView(ctx Context, actor Actor, row any) bool
	CanCreate(ctx Context, actor Actor) bool
	CanUpdate(ctx Context, actor Actor, row any) bool
	CanDelete(ctx Context, actor Actor, row any) bool
}

Policy allows resource-level and row-level permission decisions.

type Relation

type Relation struct {
	Resource     string `json:"resource"`
	ForeignKey   string `json:"foreign_key,omitempty"`
	DisplayField string `json:"display_field,omitempty"`
	Kind         string `json:"kind"`
}

type Resource

type Resource struct {
	Name            string    `json:"name"`
	LabelText       string    `json:"label"`
	PluralText      string    `json:"plural_label"`
	IconName        string    `json:"icon"`
	DescriptionText string    `json:"description"`
	Table           string    `json:"table"`
	PrimaryKey      string    `json:"primary_key"`
	DefaultSort     string    `json:"default_sort"`
	DefaultFilters  []Filter  `json:"default_filters,omitempty"`
	DefaultPageSize int       `json:"default_page_size"`
	NavigationGroup string    `json:"navigation_group,omitempty"`
	VisibleWhen     Rule      `json:"-"`
	Fields          []*Field  `json:"fields"`
	Actions         []*Action `json:"actions"`
	PolicyValue     Policy    `json:"-"`
	AuditEnabled    bool      `json:"audit_enabled"`
	TenantColumn    string    `json:"tenant_column,omitempty"`
}

Resource describes an admin-managed database-backed entity.

func (*Resource) Action

func (r *Resource) Action(name string, handler any) *Action

func (*Resource) ActionByName

func (r *Resource) ActionByName(name string) (*Action, bool)

func (*Resource) Audit

func (r *Resource) Audit() *Resource

func (*Resource) Description

func (r *Resource) Description(description string) *Resource

func (*Resource) Field

func (r *Resource) Field(name string) *Field

func (*Resource) FieldByName

func (r *Resource) FieldByName(name string) (*Field, bool)

func (*Resource) Group

func (r *Resource) Group(group string) *Resource

func (*Resource) Icon

func (r *Resource) Icon(icon string) *Resource

func (*Resource) Label

func (r *Resource) Label(label string) *Resource

func (*Resource) PageSize

func (r *Resource) PageSize(size int) *Resource

func (*Resource) PluralLabel

func (r *Resource) PluralLabel(label string) *Resource

func (*Resource) Policy

func (r *Resource) Policy(policy Policy) *Resource

func (*Resource) Primary

func (r *Resource) Primary(primaryKey string) *Resource

func (*Resource) Sort

func (r *Resource) Sort(sort string) *Resource

func (*Resource) TableName

func (r *Resource) TableName(table string) *Resource

func (*Resource) TenantScoped

func (r *Resource) TenantScoped(column string) *Resource

func (*Resource) Visible

func (r *Resource) Visible(rule Rule) *Resource

type Response

type Response struct {
	Data  any            `json:"data"`
	Meta  map[string]any `json:"meta"`
	Error *Error         `json:"error"`
}

type Rule

type Rule func(Context, Actor) bool

Jump to

Keyboard shortcuts

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