beebmx / kirby-policy
Blueprint Policies for Kirby
Fund package maintenance!
Requires
- php: ^8.4
- getkirby/composer-installer: ^1.2.1
Requires (Dev)
- getkirby/cms: ^4.0 || ^5.0 || ^6.0
- laravel/pint: ^1.0
- pestphp/pest: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Policy for Kirby
An easy way to display different content in your Kirby panel for different user roles.
Overview
- 1. Compatibility
- 2. Installation
- 3. Usage
- 4. Options
- 5. Upgrading to 2.0
- 6. Development
- 7. License
- 8. Credits
Compatibility
| Version | Kirby 4 | Kirby 5 | Kirby 6 | PHP |
|---|---|---|---|---|
| 2.x | ✅ | ✅ | ✅ | 8.4+ |
| 1.1 – 1.2 | ✅ | ✅ | ❌ | 8.2+ |
| 1.0.x | ✅ | ❌ | ❌ | 8.2+ |
Version 2.x supports PHP 8.4, 8.5 and 8.6, within the PHP versions supported by your Kirby release: Kirby 4 supports PHP up to 8.4, Kirby 5 up to 8.5 and Kirby 6 up to 8.6. Use a recent Kirby release that supports your PHP version.
Installation
Download
Download and copy this repository to /site/plugins/kirby-policy.
Composer
composer require beebmx/kirby-policy
Usage
You can create your YAML files as usual; you only need to follow a file structure and file content.
File structure
In your blueprints directory, you can place your YAML files with the suffix policy after a dot (e.g. home.policy.yml; a file like privacy-policy.yml is a regular blueprint):
blueprints ├── pages │ ├── default.yml │ ├── home.policy.yml │ ├── simple.policy.yml │ ├── multiple.policy.yml │ ├── content.yml │ ├── blog.yml │ └── post.policy.yml ├── users │ ├── admin.yml │ └── editor.yml └── site.yml
Caution
You cannot have the same blueprint with and without the policy suffix, otherwise the policy file is ignored
e.g. home.yml and home.policy.yml (just use home.policy.yml)
File Content
In your YAML file, add a policy property with the user role to every element that you need to customize:
title: Page tabs: # Only an admin will see the admin tab admin: label: Admin policy: admin columns: main: type: fields fields: item: label: Item type: text content: label: Content icon: page columns: main: width: 2/3 sections: content: type: fields fields: text: label: Text type: text # Only an admin will see the admin field admin: label: Admin type: text policy: - admin # Only an editor will see the editor field editor: label: Editor type: text policy: - editor # An admin and editor will see the mixed field mixed: label: Mixed type: text policy: - admin - editor sidebar: sticky: true width: 1/3 sections: pages: type: pages template: default files: type: files
Note
The policy property can be a string or an array of roles. An empty policy hides the element for every role. Any other value (e.g. a field named policy) is not treated as a policy.
Users without Panel access, or when nobody is logged in, get Kirby's default blueprint (e.g. pages/default), as if the policy file didn't exist. Users whose role is not in a policy get the blueprint without the elements of that policy.
A policy at the top of a blueprint hides all its tabs, columns, sections and fields for other roles, and keeps its settings (title, options, status...).
Warning
A policy removes the element for other roles, so Kirby uses its defaults instead. Don't add a policy to restrictive settings like options, required or disabled, otherwise other roles get the less restrictive default.
Important
Policies decide what each role sees in the Panel. Fields hidden by a policy can't be created or changed through the API (pages, site and files), but their content can still be read by any user with access to the page. This protection only covers top-level fields: a policy on a subfield (e.g. inside a structure or object field) only hides it in the Panel. The kirby super user (e.g. scripts with $kirby->impersonate('kirby')) can change every field. Policy files can't be used as templates or roles directly, but set templates in your pages sections anyway, so home.policy doesn't appear as an option.
Kirby 6 blueprints
Kirby 6 uses fields in place of sections. Policies work with its field layout, including pagelist and filelist fields:
title: Page tabs: content: label: Content fields: text: type: textarea admin: label: Admin type: text policy: admin mixed: label: Shared field type: text policy: - admin - editor subpages: type: pagelist blueprints: default policy: admin
The section-based example above remains available for Kirby 4 and 5. Kirby 6 also converts legacy sections automatically; see the Kirby 6 migration guide.
Policies on nested elements are evaluated even when a parent element also has a policy. Boolean and numeric blueprint values retain their original types, and empty values (e.g. options: []) are kept. Elements left empty because all their children are hidden (e.g. a tab) are removed.
Multi-language
Policies work on multi-language sites. Translated values and translation keys, including keys registered by other plugins, are kept as they are, and Kirby translates them in the Panel for each user's language:
title: en: Article de: Artikel fields: summary: label: en: Summary de: Zusammenfassung type: textarea internal: label: en: Internal notes de: Interne Notizen type: textarea policy: admin
Note
Since version 2.0.0, policy blueprints are registered once all plugins are loaded, so permissions and translations registered by other plugins work as expected (#1). Each blueprint is read and resolved only when Kirby needs it, for the user logged in at that moment.
Options
| Option | Default | Types | Description |
|---|---|---|---|
| beebmx.policy.excluded | users | array string |
Excluded directories, relative to the blueprints directory |
| beebmx.policy.suffix | policy | string |
Blueprint suffix (e.g. file.policy.yml) |
Note
Policy files in the users directory are never registered, even if users is not excluded: user blueprints are roles, which Kirby needs to know the logged in user.
Warning
Since version 1.2.0, Policy changes the plugin prefix from beebmx.kirby-policy to beebmx.policy.
Upgrading to 2.0
- PHP 8.4+ is required.
- Policy files in the
usersdirectory are no longer registered. - Changing or creating content in fields hidden by a policy (e.g. through the API or another plugin) throws a permission error, unless it's done as the
kirbysuper user. - Policy files can't be used as templates or roles directly (e.g. a page with the
home.policytemplate). - An empty
policynow hides the element for every role, instead of breaking the Panel.
Development
PHP 8.4+ and Composer 2 are required for development. Run the tests and code style check with:
composer install
composer test
vendor/bin/pint --test
The test suite uses Pest 5 with Kirby 4, 5 and 6.
Policies are handled entirely in PHP, so the plugin has no Panel frontend and needs no JavaScript build.
License
Licensed under the MIT.
Credits
- Fernando Gutierrez @beebmx
- jonatanjonas
logo - All Contributors