adminpanel

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAdminConfig = NewDefaultAdminConfig()

DefaultAdminConfig provides default configuration settings for the admin panel.

Functions

func GetAppsWithReadPermissions

func GetAppsWithReadPermissions(panel *AdminPanel, data interface{}) ([]map[string]interface{}, error)

GetAppsWithReadPermissions returns apps for which the user has read permissions.

func GetErrorHTML

func GetErrorHTML(code uint, err error) (uint, string)

GetErrorHTML generates an HTML string representing an error message with the given code and error.

func GetModelsWithReadPermissions

func GetModelsWithReadPermissions(app *App, data interface{}) ([]map[string]interface{}, error)

GetModelsWithReadPermissions returns models for which the user has read permissions.

Types

type Action

type Action string

Action represents an action type for permissions.

const (
	// ReadAction represents read permissions.
	ReadAction Action = "read"
	// CreateAction represents create permissions.
	CreateAction Action = "create"
	// UpdateAction represents update permissions.
	UpdateAction Action = "update"
	// DeleteAction represents delete permissions.
	DeleteAction Action = "delete"
	// LogViewAction represents log viewing permissions.
	LogViewAction Action = "log_view"
)

type AdminConfig

type AdminConfig struct {
	Name                    string
	Prefix                  string
	Renderer                TemplateRenderer
	AssetsPrefix            string
	GroupPrefix             string
	DefaultInstancesPerPage uint
	NavBarGenerators        []NavBarGenerator
	UserFetcher             UserFetchFunction
	LogStore                logging.LogStore
	LogStoreLevel           logging.LogStoreLevel
}

AdminConfig holds configuration settings for the admin panel.

func NewDefaultAdminConfig

func NewDefaultAdminConfig() *AdminConfig

NewDefaultAdminConfig returns a new AdminConfig with default settings.

func (*AdminConfig) CreateLog

func (c *AdminConfig) CreateLog(ctx interface{}, action logging.LogStoreLevel, contentType string, objectID interface{}, objectRepr string, message string) error

CreateLog creates a log entry using the admin panel's log store.

func (c *AdminConfig) GetAssetLink(fileName string) string

GetAssetLink constructs a full asset link by combining the group prefix, assets prefix, and the file name.

func (*AdminConfig) GetAssetsPrefix

func (c *AdminConfig) GetAssetsPrefix() string

GetAssetsPrefix returns the URL prefix for admin panel assets.

func (c *AdminConfig) GetLink(link string) string

GetLink constructs a full link by combining the group prefix, admin prefix, and the provided link.

func (*AdminConfig) GetNavBarItems

func (c *AdminConfig) GetNavBarItems(ctx interface{}) []NavBarItem

GetNavBarItems generates the navigation bar items using the registered generators.

func (*AdminConfig) GetPrefix

func (c *AdminConfig) GetPrefix() string

GetPrefix returns the URL prefix for the admin panel.

type AdminFormFieldInterface

type AdminFormFieldInterface interface {
	// AdminFormField returns a custom form field for the given field name and operation (isEdit).
	AdminFormField(name string, isEdit bool) form.Field
}

AdminFormFieldInterface allows a model to customize form fields for add and edit operations.

type AdminInstanceReprInterface

type AdminInstanceReprInterface interface {
	// AdminInstanceRepr returns a string representation of the instance.
	AdminInstanceRepr() string
}

AdminInstanceReprInterface allows customizing the string representation of an instance.

type AdminModelDisplayNameInterface

type AdminModelDisplayNameInterface interface {
	AdminDisplayName() string
}

type AdminModelGetIDInterface

type AdminModelGetIDInterface interface {
	AdminGetID() interface{}
}

type AdminModelNameInterface

type AdminModelNameInterface interface {
	AdminName() string
}

type AdminPanel

type AdminPanel struct {
	Apps              map[string]*App
	AppsSlice         []*App
	PermissionChecker PermissionFunc
	ORM               ORMIntegrator
	Web               WebIntegrator
	Config            AdminConfig
}

AdminPanel represents the admin panel, which manages apps, models, permissions, and configuration.

func NewAdminPanel

func NewAdminPanel(orm ORMIntegrator, web WebIntegrator, permissionsCheck PermissionFunc, config *AdminConfig) (*AdminPanel, error)

NewAdminPanel creates a new admin panel with the given ORM integrator, web integrator, permission function, and configuration.

func (*AdminPanel) CreateLogViewLog

func (ap *AdminPanel) CreateLogViewLog(ctx interface{}, entry logging.LogEntry) error

CreateLogViewLog creates a log entry when a log entry is viewed.

func (*AdminPanel) CreateViewLog

func (ap *AdminPanel) CreateViewLog(ctx interface{}) error

CreateViewLog creates a log entry when the admin panel is viewed.

func (ap *AdminPanel) GetFullLink() string

GetFullLink returns the full URL path to the admin panel.

func (ap *AdminPanel) GetFullLogBaseLink() string

GetFullLogBaseLink returns the full URL path for logs, including the admin prefix.

func (*AdminPanel) GetHandler

func (ap *AdminPanel) GetHandler() HandlerFunc

GetHandler returns the HTTP handler function for the admin panel's root page.

func (ap *AdminPanel) GetLogBaseLink() string

GetLogBaseLink returns the base URL path for logs.

func (*AdminPanel) GetLogEntries

func (ap *AdminPanel) GetLogEntries(ctx interface{}, maxCount uint) []*logging.LogEntry

GetLogEntries retrieves log entries up to the specified maximum count.

func (*AdminPanel) GetLogHandler

func (ap *AdminPanel) GetLogHandler() HandlerFunc

GetLogHandler returns the HTTP handler function for viewing a log entry.

func (*AdminPanel) GetORM

func (ap *AdminPanel) GetORM() ORMIntegrator

GetORM returns the ORM integrator for the admin panel.

func (*AdminPanel) RegisterApp

func (ap *AdminPanel) RegisterApp(name, displayName string, orm ORMIntegrator) (*App, error)

RegisterApp registers a new application with the admin panel.

type App

type App struct {
	Name        string
	DisplayName string
	Models      map[string]*Model
	ModelsSlice []*Model
	Panel       *AdminPanel
	ORM         ORMIntegrator
}

App represents an application within the admin panel, grouping related models together.

func (*App) CreateViewLog

func (a *App) CreateViewLog(ctx interface{}) error

CreateViewLog creates a log entry when the app is viewed.

func (a *App) GetFullLink() string

GetFullLink returns the full URL path to the app, including the admin prefix.

func (*App) GetHandler

func (a *App) GetHandler() HandlerFunc

GetHandler returns the HTTP handler function for the app's main page.

func (a *App) GetLink() string

GetLink returns the relative URL path to the app.

func (*App) GetORM

func (a *App) GetORM() ORMIntegrator

GetORM returns the ORM integrator for the app.

func (*App) RegisterModel

func (a *App) RegisterModel(model interface{}, orm ORMIntegrator) (*Model, error)

RegisterModel registers a model with the app, making it available in the admin interface.

type DefaultTemplateRenderer

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

func NewDefaultTemplateRenderer

func NewDefaultTemplateRenderer() *DefaultTemplateRenderer

func (*DefaultTemplateRenderer) AddCustomAsset

func (tr *DefaultTemplateRenderer) AddCustomAsset(name string, asset []byte)

func (*DefaultTemplateRenderer) AddCustomCompositeTemplate

func (tr *DefaultTemplateRenderer) AddCustomCompositeTemplate(name string, baseNames ...string) error

func (*DefaultTemplateRenderer) AddCustomTemplate

func (tr *DefaultTemplateRenderer) AddCustomTemplate(name, tmplText string) error

func (*DefaultTemplateRenderer) GetAsset

func (tr *DefaultTemplateRenderer) GetAsset(name string) ([]byte, error)

func (*DefaultTemplateRenderer) RegisterAssetsFunc

func (tr *DefaultTemplateRenderer) RegisterAssetsFunc(assetsFunc func(string) string)

func (*DefaultTemplateRenderer) RegisterCompositeDefaultTemplate

func (tr *DefaultTemplateRenderer) RegisterCompositeDefaultTemplate(name string, baseNames ...string) error

func (*DefaultTemplateRenderer) RegisterDefaultAssets

func (tr *DefaultTemplateRenderer) RegisterDefaultAssets(assets embed.FS, prefix string)

func (*DefaultTemplateRenderer) RegisterDefaultData

func (tr *DefaultTemplateRenderer) RegisterDefaultData(data map[string]interface{}) error

func (*DefaultTemplateRenderer) RegisterDefaultTemplates

func (tr *DefaultTemplateRenderer) RegisterDefaultTemplates(templates embed.FS, prefix string)

func (*DefaultTemplateRenderer) RegisterLinkFunc

func (tr *DefaultTemplateRenderer) RegisterLinkFunc(linkFunc func(string) string)

func (*DefaultTemplateRenderer) RenderTemplate

func (tr *DefaultTemplateRenderer) RenderTemplate(name string, data map[string]interface{}) (string, error)

type FieldConfig

type FieldConfig struct {
	Name                  string
	DisplayName           string
	FieldType             reflect.Type
	IsPointer             bool
	IncludeInListFetch    bool
	IncludeInListDisplay  bool
	IncludeInSearch       bool
	IncludeInInstanceView bool
	AddFormField          form.Field
	EditFormField         form.Field
}

FieldConfig holds configuration for a model field in the admin panel.

type HandlerFunc

type HandlerFunc = func(interface{}) (uint, string)

HandlerFunc represents a handler function used in the admin panel routes.

type Instance

type Instance struct {
	InstanceID  interface{}
	Data        interface{}
	Model       *Model
	Permissions Permissions
}

Instance represents a single instance of a model in the admin panel.

func (*Instance) CreateCreateLog

func (i *Instance) CreateCreateLog(ctx interface{}) error

CreateCreateLog creates a log entry when the instance is created.

func (*Instance) CreateDeleteLog

func (i *Instance) CreateDeleteLog(ctx interface{}) error

CreateDeleteLog creates a log entry when the instance is deleted.

func (*Instance) CreateUpdateLog

func (i *Instance) CreateUpdateLog(ctx interface{}, updates map[string]interface{}) error

CreateUpdateLog creates a log entry when the instance is updated.

func (*Instance) CreateViewLog

func (i *Instance) CreateViewLog(ctx interface{}) error

CreateViewLog creates a log entry when the instance is viewed.

func (i *Instance) GetEditLink() string

GetEditLink returns the relative URL to edit the instance.

func (i *Instance) GetFullEditLink() string

GetFullEditLink returns the full URL to edit the instance.

func (i *Instance) GetFullLink() string

GetFullLink returns the full URL to view the instance.

func (i *Instance) GetLink() string

GetLink returns the relative URL to view the instance.

func (*Instance) GetRepr

func (i *Instance) GetRepr() string

GetRepr returns a string representation of the instance.

type Model

type Model struct {
	Name        string
	DisplayName string
	PTR         interface{}
	App         *App
	Fields      []FieldConfig
	ORM         ORMIntegrator
}

Model represents a registered model within an app in the admin panel.

func (*Model) CreateViewLog

func (m *Model) CreateViewLog(ctx interface{}) error

CreateViewLog creates a log entry when the model's list view is accessed.

func (*Model) GetAddHandler

func (m *Model) GetAddHandler() HandlerFunc

GetAddHandler returns the HTTP handler function for adding a new instance.

func (m *Model) GetAddLink() string

GetAddLink returns the relative URL path to add a new instance of the model.

func (*Model) GetEditHandler

func (m *Model) GetEditHandler() HandlerFunc

GetEditHandler returns the HTTP handler function for editing an existing instance.

func (m *Model) GetFullAddLink() string

GetFullAddLink returns the full URL path to add a new instance of the model.

func (m *Model) GetFullLink() string

GetFullLink returns the full URL path to the model, including the admin prefix.

func (*Model) GetInstanceDeleteHandler

func (m *Model) GetInstanceDeleteHandler() HandlerFunc

func (*Model) GetInstanceViewHandler

func (m *Model) GetInstanceViewHandler() HandlerFunc
func (m *Model) GetLink() string

GetLink returns the relative URL path to the model.

func (*Model) GetORM

func (m *Model) GetORM() ORMIntegrator

GetORM returns the ORM integrator for the model.

func (*Model) GetPrimaryKeyType

func (m *Model) GetPrimaryKeyType() (reflect.Type, error)

GetPrimaryKeyType retrieves the primary key type of the model.

func (*Model) GetPrimaryKeyValue

func (m *Model) GetPrimaryKeyValue(instance interface{}) (interface{}, error)

GetPrimaryKeyValue retrieves the primary key value of an instance.

func (*Model) GetViewHandler

func (m *Model) GetViewHandler() HandlerFunc

GetViewHandler returns the HTTP handler function for the model's list view.

func (*Model) NewAddForm

func (m *Model) NewAddForm() (form.Form, error)

NewAddForm creates a new form for adding an instance of the model.

func (*Model) NewEditForm

func (m *Model) NewEditForm(instanceID interface{}) (form.Form, error)

NewEditForm creates a new form for editing an existing instance of the model.

type ModelAddForm

type ModelAddForm struct {
	forms.BaseForm
	Model *Model
}

ModelAddForm represents the form used to add a new instance of a model.

func (*ModelAddForm) Save

func (f *ModelAddForm) Save(values map[string]form.HTMLType) (interface{}, error)

Save processes the form data and creates a new instance of the model.

type ModelEditForm

type ModelEditForm struct {
	forms.BaseForm
	Model      *Model
	InstanceID interface{}
}

ModelEditForm represents the form used to edit an existing instance of a model.

func (*ModelEditForm) Save

func (f *ModelEditForm) Save(values map[string]form.HTMLType) (interface{}, error)

Save processes the form data and updates the existing instance of the model.

type NavBarGenerator = func(ctx interface{}) NavBarItem

NavBarGenerator defines a function type for generating navigation bar items.

type NavBarItem struct {
	Name              string
	Link              string
	Bold              bool
	NavBarAppendSlash bool
}

NavBarItem represents an item in the navigation bar.

func (i *NavBarItem) HTML() string

HTML returns the HTML representation of the navigation bar item.

type ORMIntegrator

type ORMIntegrator interface {
	// GetPrimaryKeyValue returns the primary key value of the given model instance.
	GetPrimaryKeyValue(model interface{}) (interface{}, error)

	// GetPrimaryKeyType returns the reflect.Type of the primary key for the model.
	GetPrimaryKeyType(model interface{}) (reflect.Type, error)

	// FetchInstances retrieves all instances of the given model.
	FetchInstances(model interface{}) (interface{}, error)

	// FetchInstancesOnlyFields retrieves instances with only the specified fields.
	FetchInstancesOnlyFields(model interface{}, fields []string) (interface{}, error)

	// FetchInstancesOnlyFieldWithSearch retrieves instances matching the search query on specified fields.
	FetchInstancesOnlyFieldWithSearch(model interface{}, fields []string, query string, searchFields []string) (interface{}, error)

	// DeleteInstance deletes an instance of the model by its primary key.
	DeleteInstance(model interface{}, id interface{}) error

	// FetchInstanceOnlyFields retrieves a single instance with only the specified fields.
	FetchInstanceOnlyFields(model interface{}, id interface{}, fields []string) (interface{}, error)

	// FetchInstance retrieves a single instance of the model by its primary key.
	FetchInstance(model interface{}, id interface{}) (interface{}, error)

	// CreateInstance creates a new instance of the model.
	CreateInstance(instance interface{}) error

	// UpdateInstance updates an existing instance of the model.
	UpdateInstance(instance interface{}, primaryKey interface{}) error

	// CreateInstanceOnlyFields creates a new instance with only the specified fields.
	CreateInstanceOnlyFields(instance interface{}, fields []string) error

	// UpdateInstanceOnlyFields updates an existing instance with only the specified fields.
	UpdateInstanceOnlyFields(instance interface{}, fields []string, primaryKey interface{}) error
}

ORMIntegrator defines the interface for integrating ORMs with the admin panel.

type PermissionFunc

type PermissionFunc func(PermissionRequest, interface{}) (bool, error)

PermissionFunc defines a function type for checking permissions.

func (PermissionFunc) HasAppReadPermission

func (p PermissionFunc) HasAppReadPermission(appName string, data interface{}) (bool, error)

HasAppReadPermission checks if the user has read permission for the specified app.

func (PermissionFunc) HasInstanceDeletePermission

func (p PermissionFunc) HasInstanceDeletePermission(appName, modelName string, instanceID interface{}, data interface{}) (bool, error)

HasInstanceDeletePermission checks if the user has delete permission for the specified instance.

func (PermissionFunc) HasInstanceReadPermission

func (p PermissionFunc) HasInstanceReadPermission(appName, modelName string, instanceID interface{}, data interface{}) (bool, error)

HasInstanceReadPermission checks if the user has read permission for the specified instance.

func (PermissionFunc) HasInstanceUpdatePermission

func (p PermissionFunc) HasInstanceUpdatePermission(appName, modelName string, instanceID interface{}, data interface{}) (bool, error)

HasInstanceUpdatePermission checks if the user has update permission for the specified instance.

func (PermissionFunc) HasLogViewPermission

func (p PermissionFunc) HasLogViewPermission(data interface{}, logID interface{}) (bool, error)

HasLogViewPermission checks if the user has permission to view logs.

func (PermissionFunc) HasModelCreatePermission

func (p PermissionFunc) HasModelCreatePermission(appName string, modelName string, data interface{}) (bool, error)

HasModelCreatePermission checks if the user has create permission for the specified model.

func (PermissionFunc) HasModelDeletePermission

func (p PermissionFunc) HasModelDeletePermission(appName string, modelName string, data interface{}) (bool, error)

HasModelDeletePermission checks if the user has delete permission for the specified model.

func (PermissionFunc) HasModelReadPermission

func (p PermissionFunc) HasModelReadPermission(appName string, modelName string, data interface{}) (bool, error)

HasModelReadPermission checks if the user has read permission for the specified model.

func (PermissionFunc) HasModelUpdatePermission

func (p PermissionFunc) HasModelUpdatePermission(appName string, modelName string, data interface{}) (bool, error)

HasModelUpdatePermission checks if the user has update permission for the specified model.

func (PermissionFunc) HasPermission

func (p PermissionFunc) HasPermission(r PermissionRequest, data interface{}) (bool, error)

HasPermission checks if the user has the specified permission.

func (PermissionFunc) HasReadPermission

func (p PermissionFunc) HasReadPermission(data interface{}) (bool, error)

HasReadPermission checks if the user has read permission for the admin panel.

type PermissionRequest

type PermissionRequest struct {
	AppName    *string
	ModelName  *string
	InstanceID interface{}
	Action     *Action
}

PermissionRequest represents a request to check permissions for a specific action.

type Permissions

type Permissions struct {
	Read   bool
	Create bool
	Update bool
	Delete bool
}

Permissions holds the permissions for a specific operation.

type TemplateRenderer

type TemplateRenderer interface {
	RenderTemplate(name string, data map[string]interface{}) (string, error)
	RegisterDefaultTemplates(templates embed.FS, prefix string)
	RegisterCompositeDefaultTemplate(name string, baseNames ...string) error
	RegisterDefaultData(data map[string]interface{}) error
	AddCustomTemplate(name string, tmplText string) error
	AddCustomCompositeTemplate(name string, baseNames ...string) error
	RegisterDefaultAssets(assets embed.FS, prefix string)
	AddCustomAsset(name string, asset []byte)
	GetAsset(name string) ([]byte, error)
	RegisterLinkFunc(func(string) string)
	RegisterAssetsFunc(func(string) string)
}

type UserFetchFunction

type UserFetchFunction = func(ctx interface{}) (userID interface{}, repr string, err error)

UserFetchFunction defines a function type for fetching user information from the context.

type WebIntegrator

type WebIntegrator interface {
	// HandleRoute registers a route with the given method, path, and handler function.
	HandleRoute(method, path string, handler HandlerFunc)

	// ServeAssets serves static assets under the specified prefix using the provided renderer.
	ServeAssets(prefix string, renderer TemplateRenderer)

	// GetQueryParam retrieves the value of a query parameter from the context.
	GetQueryParam(ctx interface{}, name string) string

	// GetPathParam retrieves the value of a path parameter from the context.
	GetPathParam(ctx interface{}, name string) string

	// GetRequestMethod retrieves the HTTP method of the request from the context.
	GetRequestMethod(ctx interface{}) string

	// GetFormData retrieves form data from the context.
	GetFormData(ctx interface{}) map[string][]string
}

WebIntegrator defines the interface for integrating web frameworks with the admin panel.

Jump to

Keyboard shortcuts

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