Files
sm-journal-plugin/controllers/categories.go
Jakub Zych cb1c688213 feat(admin): category content on the Blog Editor and editor image uploads
- category content is the blogeditor type (uploadFolder categories): it is
  stripped and reported like post content; the 422 CheckHTML gate is
  retired and blank content still becomes NULL
- blogeditor and mlblogeditor Upload: magic-byte check for JPEG, PNG, GIF
  and WebP matching the extension, safe lowercase names, storage through
  classes.StoreMedia in the field's uploadFolder
- TestBlogEditorUploadSmoke, TestCategoriesAdminContent asserts the strip,
  README describes the field types, admin saves and editor uploads
2026-10-10 12:08:51 +02:00

89 lines
2.9 KiB
Go

package controllers
import (
"context"
"errors"
"strings"
"git.golem15.com/golem15/sm-journal-plugin/models"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/pact"
)
const (
// CategoriesControllerID is the cabana controller id of the Categories screen.
CategoriesControllerID = "golem15.journal.categories"
// PermissionAccessCategories is Categories::$requiredPermissions.
PermissionAccessCategories = "golem15.journal.access_categories"
)
type categoriesAdminController struct {
app func() *backpack.App
}
func (categoriesAdminController) ID() string { return CategoriesControllerID }
func (categoriesAdminController) ModelName() string { return `Golem15\Journal\Models\Category` }
func (categoriesAdminController) ConfigDir() string { return "controllers/categories" }
func (categoriesAdminController) RequiredPermissions() []string {
return []string{PermissionAccessCategories}
}
func (categoriesAdminController) NewRecord() any { return &models.Category{} }
func (categoriesAdminController) AdminFieldRelations() []cabana.FieldRelationContract {
return []cabana.FieldRelationContract{{
Field: "parent",
Kind: "belongsTo",
NewRelated: func() any { return &models.Category{} },
ForeignKey: "parent_id",
LabelColumn: "name",
}}
}
func (categoriesAdminController) FormBeforeCreate(ctx context.Context, model any) error {
cat, ok := model.(*models.Category)
if !ok || cat == nil {
return errors.New("categories admin: form model is not a category")
}
return prepareCategoryWrite(cat)
}
func (categoriesAdminController) FormBeforeUpdate(ctx context.Context, model any) error {
cat, ok := model.(*models.Category)
if !ok || cat == nil {
return errors.New("categories admin: form model is not a category")
}
return prepareCategoryWrite(cat)
}
// prepareCategoryWrite stores blank category content (white space only) as
// NULL and accepts any other content it receives. Since Phase 16.2 the
// content field is the blogeditor type, whose Decode already stored the HTML
// policy's output (classes.SanitizeHTMLReport) and told the administrator
// what it removed, so an admin save is never refused because of markup
// (D-10 amended, see the bluemonday decision note). Rows written by SQL or
// by the import commands do not pass through here and are not sanitized by
// it.
func prepareCategoryWrite(cat *models.Category) error {
if cat.Content == nil {
return nil
}
if strings.TrimSpace(*cat.Content) == "" {
cat.Content = nil
}
return nil
}
var (
_ pact.AdminController = categoriesAdminController{}
_ pact.AdminPermissioned = categoriesAdminController{}
_ pact.AdminRecordSource = categoriesAdminController{}
_ pact.FormBeforeCreate = categoriesAdminController{}
_ pact.FormBeforeUpdate = categoriesAdminController{}
_ cabana.FieldRelationProvider = categoriesAdminController{}
)