Files
summercms/modules/cabana/backend_screens.go

239 lines
9.3 KiB
Go

package cabana
import (
"embed"
"io/fs"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/golem15/summercms/modules/party"
"git.golem15.com/golem15/summercms/modules/postcard"
)
// The admin's own screens for backend administrators and roles (Settings →
// System), registered by cabana itself through an internal pseudo-plugin.
const (
// PermissionManageUsers opens the Administrators screen. No role is
// granted it in code: until an administrator grants it, only superusers
// reach the screen.
PermissionManageUsers = "backend.manage_users"
// PermissionManageRoles opens the Roles screen. No role is granted it in
// code either.
PermissionManageRoles = "backend.manage_roles"
// AdministratorsControllerID is the admin controller ID of the
// Administrators screen, the target other plugins name to add record
// actions to it (pact.HasAdminActionExtensions).
AdministratorsControllerID = "summercms.cabana.users"
// RolesControllerID is the admin controller ID of the Roles screen.
RolesControllerID = "summercms.cabana.roles"
// PermissionViewMedia opens the Media Library screen and read-only media
// browsing/picking routes. No role is granted it in code.
PermissionViewMedia = "media.view_media"
// PermissionManageMedia allows uploading, organizing folders, editing
// metadata, and deleting items in the Media Library. No role is granted it
// in code.
PermissionManageMedia = "media.manage_media"
)
// The mail screens (Settings → Mail), registered by the same pseudo-plugin
// whenever the application publishes postcard's Mailer.
const (
// PermissionManageMailTemplates opens the Mail templates screen, the
// template preview and the template test send. No role is granted it in
// code.
PermissionManageMailTemplates = "system.manage_mail_templates"
// PermissionManageMailLayouts opens the Mail layouts, Mail partials, Mail
// branding and Mail images screens, because they change every message.
// No role is granted it in code.
PermissionManageMailLayouts = "system.manage_mail_layouts"
// MailTemplatesControllerID is the admin controller ID of the Mail
// templates screen.
MailTemplatesControllerID = "summercms.cabana.mailtemplates"
// MailLayoutsControllerID is the admin controller ID of the Mail layouts
// screen.
MailLayoutsControllerID = "summercms.cabana.maillayouts"
// MailPartialsControllerID is the admin controller ID of the Mail
// partials screen.
MailPartialsControllerID = "summercms.cabana.mailpartials"
// MailImagesControllerID is the admin controller ID of the Mail images
// screen, the singleton form of the mail image library.
MailImagesControllerID = "summercms.cabana.mailimages"
)
// backendScreensPluginID is the pseudo-plugin's ID. It is also the
// migration history ID of the backend tables (lagoon), which is untouched:
// the pseudo-plugin declares no migrations.
const backendScreensPluginID = "summercms.cabana"
// backendScreensFS holds the screens' admin YAML and partials. all: keeps
// the partials, whose names start with an underscore.
//
//go:embed all:backendscreens
var backendScreensFS embed.FS
// backendScreens is the internal pseudo-plugin that registers the
// Administrators and Roles screens, their two permissions and their settings
// entries. Activate appends it to the plugins only when some plugin
// registers admin controllers, so an application without an admin still
// boots without one. app is the activating application and reg the compiled
// registry, set once compilation is done; the controllers read both per
// request.
type backendScreens struct {
app *backpack.App
reg *Registry
// mail holds the mail screens' state; nil when the application has no
// postcard Mailer, and the mail screens, permissions and settings
// entries are then not registered.
mail *mailScreens
}
var (
_ party.Plugin = (*backendScreens)(nil)
_ pact.AdminAssets = (*backendScreens)(nil)
_ pact.HasAdminControllers = (*backendScreens)(nil)
_ pact.HasPermissions = (*backendScreens)(nil)
_ pact.HasSettings = (*backendScreens)(nil)
_ pact.HasTokenScopes = (*backendScreens)(nil)
)
func (*backendScreens) ID() string { return backendScreensPluginID }
func (*backendScreens) Requires() []string { return nil }
func (*backendScreens) Register(*backpack.App) error { return nil }
func (*backendScreens) Boot(*backpack.App) error { return nil }
// AdminFS serves the embedded tree rooted at backendscreens/, so its paths
// start at controllers/ and models/.
func (*backendScreens) AdminFS() fs.FS {
sub, err := fs.Sub(backendScreensFS, "backendscreens")
if err != nil {
return nil
}
return sub
}
func (s *backendScreens) AdminControllers() []pact.AdminController {
out := []pact.AdminController{usersAdmin{screens: s}, rolesAdmin{screens: s}}
if s.mail != nil {
out = append(out, s.mail.controllers()...)
}
return out
}
// Permissions declares the screen permissions with no role grants (D-04):
// an upgrade gives no existing role access to the screens. The Plugins
// screen (PermissionViewPlugins) is served by cabana itself, not by an
// admin controller. The two mail codes are declared only with the mail
// screens, and with no role grants either.
func (s *backendScreens) Permissions() []pact.Permission {
out := []pact.Permission{
{Code: PermissionManageUsers, Tab: "backend::lang.permissions.tab_system", Label: "backend::lang.permissions.manage_users"},
{Code: PermissionManageRoles, Tab: "backend::lang.permissions.tab_system", Label: "backend::lang.permissions.manage_roles"},
{Code: PermissionViewPlugins, Tab: "backend::lang.permissions.tab_system", Label: "backend::lang.permissions.view_plugins"},
{Code: PermissionViewMedia, Tab: "backend::lang.permissions.tab_media", Label: "backend::lang.permissions.view_media"},
{Code: PermissionManageMedia, Tab: "backend::lang.permissions.tab_media", Label: "backend::lang.permissions.manage_media"},
}
if s.mail != nil {
out = append(out, mailPermissions()...)
}
return out
}
// TokenScopes declares the framework Media Library token scopes (media:read
// and media) grouped under Media in Phase 36's ScopePicker.
func (*backendScreens) TokenScopes() []pact.TokenScope {
return mediaTokenScopes()
}
func mediaPermissions() []pact.Permission {
return []pact.Permission{
{Code: PermissionViewMedia, Tab: "backend::lang.permissions.tab_media", Label: "backend::lang.permissions.view_media"},
{Code: PermissionManageMedia, Tab: "backend::lang.permissions.tab_media", Label: "backend::lang.permissions.manage_media"},
}
}
func mediaTokenScopes() []pact.TokenScope {
return []pact.TokenScope{
{
Name: "media:read",
Label: "backend::lang.api_tokens.scope_media_read",
Description: "backend::lang.api_tokens.scope_media_read_description",
Group: "backend::lang.api_tokens.group_media",
Permissions: []string{PermissionViewMedia},
Methods: []string{"GET", "HEAD"},
},
{
Name: "media",
Label: "backend::lang.api_tokens.scope_media",
Description: "backend::lang.api_tokens.scope_media_description",
Group: "backend::lang.api_tokens.group_media",
Permissions: []string{PermissionViewMedia, PermissionManageMedia},
Methods: []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"},
},
}
}
// Settings links the screens from Settings → System, and the mail screens
// from Settings → Mail; there is no navigation entry.
func (s *backendScreens) Settings() []pact.SettingsItem {
out := s.systemSettings()
if s.mail != nil {
out = append(out, s.mail.settings()...)
}
return out
}
func (*backendScreens) systemSettings() []pact.SettingsItem {
return []pact.SettingsItem{
{
Code: "administrators",
Label: "backend::lang.user.menu_label",
Description: "backend::lang.user.menu_description",
Category: "backend::lang.settings.category_system",
Icon: "users",
Order: 100,
Permissions: []string{PermissionManageUsers},
Controller: AdministratorsControllerID,
},
{
Code: "roles",
Label: "backend::lang.user.role.menu_label",
Description: "backend::lang.user.role.menu_description",
Category: "backend::lang.settings.category_system",
Icon: "shield-check",
Order: 110,
Permissions: []string{PermissionManageRoles},
Controller: RolesControllerID,
},
}
}
// application is the activating app, or nil.
func (s *backendScreens) application() *backpack.App {
if s == nil {
return nil
}
return s.app
}
// registry is the compiled registry, or nil before compilation finished.
func (s *backendScreens) registry() *Registry {
if s == nil {
return nil
}
return s.reg
}
// withBackendScreens returns a new plugin slice with the pseudo-plugin
// appended, and the pseudo-plugin. plugins itself is never modified. The
// mail screens join it when app publishes postcard's Mailer (party.Activate
// runs postcard.Activate before any plugin boots).
func withBackendScreens(app *backpack.App, plugins []party.Plugin) ([]party.Plugin, *backendScreens) {
screens := &backendScreens{app: app}
if editor, ok := postcard.EditorFrom(app); ok {
screens.mail = &mailScreens{screens: screens, editor: editor}
}
out := make([]party.Plugin, 0, len(plugins)+1)
out = append(out, plugins...)
return append(out, screens), screens
}