Documentation
¶
Overview ¶
Package useradmin provides a standalone user admin interface following the folder-per-controller pattern. Each controller is in its own subfolder and handles its own views and AJAX data.
This module is modeled on github.com/dracory/blogadmin.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUserStoreRequired is returned when UserStore is not provided ErrUserStoreRequired = errors.New("user store is required") // ErrLoggerRequired is returned when Logger is not provided ErrLoggerRequired = errors.New("logger is required") // ErrSessionStoreRequired is returned when SessionStore is not // provided. The impersonate controller needs it to create a new // session for the impersonated user. ErrSessionStoreRequired = errors.New("session store is required for the impersonate controller") // ErrGeoStoreRequired is returned when GeoStore is not provided. // The user update controller needs it to list countries and // timezones. ErrGeoStoreRequired = errors.New("geo store is required for the user update controller") )
Common errors
Functions ¶
This section is empty.
Types ¶
type AdminInterface ¶
type AdminInterface interface {
Handle(w http.ResponseWriter, r *http.Request)
}
AdminInterface defines the interface for the user admin
func New ¶
func New(opts AdminOptions) (AdminInterface, error)
New creates a new user admin instance. Returns ErrUserStoreRequired if UserStore is nil, ErrLoggerRequired if Logger is nil, ErrGeoStoreRequired if GeoStore is nil, and ErrSessionStoreRequired if SessionStore is nil.
This makes misconfiguration fail fast at construction instead of surfacing as runtime errors inside individual controllers.
type AdminOptions ¶
type AdminOptions struct {
// UserStore is required
UserStore userstore.StoreInterface
// GeoStore is required for the user update controller (country and
// timezone lists).
GeoStore geostore.StoreInterface
// Logger is required
Logger *slog.Logger
// SessionStore is required for the impersonate controller.
SessionStore sessionstore.StoreInterface
// BlindIndexFirstName/LastName/Email enable filtered search by
// the corresponding field. Optional.
BlindIndexFirstName blindindexstore.StoreInterface
BlindIndexLastName blindindexstore.StoreInterface
BlindIndexEmail blindindexstore.StoreInterface
// TaskStore is used to enqueue a blind index rebuild when a user's
// email changes and vault tokenization is enabled. Optional.
TaskStore taskstore.StoreInterface
// BlindIndexRebuildTaskAlias is the task alias enqueued on email
// change. If empty, the enqueue is skipped.
BlindIndexRebuildTaskAlias string
// VaultTokenizer abstracts vault tokenization. Optional — when
// nil, user fields are treated as plain text.
VaultTokenizer shared.VaultTokenizer
// AuthUser returns the authenticated user from the request, or
// nil if unauthenticated. Used by the create/delete/impersonate
// controllers for authorization checks.
AuthUser func(r *http.Request) userstore.UserInterface
// AuthUserID returns the authenticated user ID from the request.
// If it returns "", the user is treated as unauthenticated and
// redirected to AdminHomeURL. If nil, the auth check is skipped
// (the host project is expected to gate the route).
AuthUserID func(r *http.Request) string
// FlashRedirect redirects with a flash message. Optional — when
// nil, plain http.Redirect is used.
FlashRedirect shared.FlashRedirectFunc
// SecureCookie controls whether the impersonation cookie is marked
// Secure. Set to false for HTTP (development), true for HTTPS
// (production). Defaults to true.
SecureCookie bool
// FuncLayout is an optional function to render the admin interface
// inside your own layout (branding, menus, etc.). It receives the
// request and response writer so the host project can access
// request context (auth user, locale, etc.) when rendering the
// layout.
FuncLayout func(w http.ResponseWriter, r *http.Request, title string, body string, options struct {
Styles []string
StyleURLs []string
Scripts []string
ScriptURLs []string
}) string
// AdminHomeURL is the URL for the admin home page (default: "/admin")
AdminHomeURL string
// UserAdminURL is the base URL for the user admin (default: "/admin/users")
UserAdminURL string
// UserHomeURL is the URL the impersonate controller redirects to
// after a successful impersonation (default: "/"). This is where
// the impersonated user lands.
UserHomeURL string
}
AdminOptions contains all dependencies and configuration for the user admin.
UserStore, GeoStore, and Logger are required. SessionStore is required for the impersonate controller. Blind index stores, TaskStore, and VaultTokenizer are optional — when nil, the corresponding features degrade gracefully (filtered search disabled, email-change rebuild skipped, user fields treated as plain text).
FuncLayout is an optional function to render the admin interface inside your own layout (branding, menus, etc.). If nil, a default bare-bones HTML page is used (Bootstrap + Vue CDN). Uses an anonymous struct to match blogadmin/shopadmin exactly, so consumers can reuse their existing layout function for useradmin.