Search by

beebmx / kirby-patrol

beebmx

Patrol for Kirby

Package info

github.com/beebmx/kirby-patrol

Type:kirby-plugin

pkg:composer/beebmx/kirby-patrol

Fund package maintenance!

beebmx

Statistics

Installs: 120

Dependents: 0

Suggesters: 0

Stars: 9

Open Issues: 0

2.0.0 2026-11-29 05:53 UTC

This package is auto-updated.

Last update: 2026-11-29 05:53:57 UTC


README

Patrol Logo

Build Status Total Downloads Latest Stable Version License

Patrol for Kirby

An easy and customizable way to manage access to website pages according to the roles assigned to users within the Kirby panel interface.

Overview

Installation

composer require beebmx/kirby-patrol

Compatibility

Version Kirby 4 Kirby 5 Kirby 6 PHP
2.x ❌ ❌ ✅ 8.4+
1.2 – 1.3 ✅ ✅ ❌ 8.2+
1.0.0 – 1.1.1 ✅ ❌ ❌ 8.2+

Note

Version 2.x requires Kirby 6 and PHP 8.4 or higher (tested up to PHP 8.6). If you are using Kirby 4 or Kirby 5, install the 1.x version:

composer require beebmx/kirby-patrol:^1.3

Usage

Out of the box, you don't need to do anything to start using (except for installation), but if you require customizing the default behavior, there are some options to personalize Patrol.

Panel

By default, only admins see the new area and can update the access to the pages on the website.

You can enable Patrol for any other role by adding the permission in its user YAML file:

title: Editor

permissions:
  beebmx.patrol:
    access: true

And you can disable it for admins the same way:

title: Admin

permissions:
  beebmx.patrol:
    access: false

Note

The access is true for admins and false for every other role by default. Users without this permission can't see the area, the page button or update the permissions through the API.

The pages displayed in the new panel area will be all the site children published (with status listed and unlisted), except the home and error pages, and two levels inside every page. If you need a specific collection of pages you can change it with the query option in your config.php file:

use Kirby\Cms\App;
use Kirby\Cms\Pages;
use Kirby\Cms\Site;

'beebmx.patrol' => [
    'content' => [
        'query' => function (Site $site, Pages $pages, App $kirby) {
            return $site->find('secure-page')->children()->listed();
        },
    ],
],

And if you need to update the depth of the pages displayed, update the config.php file:

'beebmx.patrol' => [
    'content' => [
        'depth' => 3,
    ],
],

The Patrol view shows a tab for every role, with the number of restricted pages as a badge, and the pages as a tree where you can:

  • Toggle the access of every page between Allowed and Restricted.
  • Allow, restrict or invert a page with all its subpages, or open the page in the Panel or the website, from the options (…) of every row.
  • Search pages by title or path, and filter them by Allowed or Restricted.
  • Allow, restrict, invert or reset to default all pages at once, and expand or collapse the tree.
  • See the unsaved changes (marked with a dot), and save them with the Save button or ⌘/Ctrl + S.
  • Spot the pages that are still accessible even when a parent page is restricted (marked with a warning), and restrict them all at once.

Note

Every page is validated on its own, so restricting a page doesn't restrict its subpages. Use the Restrict with subpages option of a row to restrict the whole branch.

Here's an example of Patrol view page:

Patrol panel example

Frontend

When a logged-in user visits any page, Patrol will automatically validate the request. If the user has access to the visited page, they can normally view the content, but if not, an error page will be thrown with a 401 status code.

Note

Visitors without a logged-in user (guests) are not patrolled by default. To restrict pages for guests, enable the guest support.

Middleware

Even when Patrol tries to validate a user, it's possible that behavior won't be enough for your own validation. In that case, you can customize and add additional restrictions to every page.

The middleware process is powered by Middleware, and you can use all features if you need to.

Closure middleware

The easiest way to add additional validation is with Closures. Add this in the config.php file:

use Kirby\Http\Response;
use Beebmx\KirbyMiddleware\Request;

'beebmx.patrol' => [
    'permissions' => [
        'middleware' => [
            function (Request $request, Closure $next) {
                if (page($request->path())?->is('secure-page')) {
                    return Response::redirect('login');
                }

                return $next($request);
            },
        ],
    ],
],

As you can see, the Closure requires two parameters: a Request called $request and a Closure called $next. The $request contains the stack of previous validations from Patrol and any other middleware triggered.

The second parameter $next, you should call it at the end of the process to proceed to the next validation with the $request.

Note

You can return a Response::class object. When you do that, Patrol will automatically send the request.

Class middleware

If your own validation is more complex for a simple Closure, you can use a custom class for that purpose:

'beebmx.patrol' => [
    'permissions' => [
        'middleware' => [
            MyCustomMiddleware::class,
        ],
    ],
],

And your class should look like:

use Beebmx\KirbyMiddleware\Request;
use Closure;
use Kirby\Cms\App;
use Kirby\Exception\ErrorPageException;

class MyCustomMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $kirby = App::instance();

        if ($kirby->site()->isDisabled()->toBool()) {
            throw new ErrorPageException(
                fallback: 'Unauthorized',
                httpCode: 401,
            );
        }

        return $next($request);
    }
}

Your middleware logic should be inside the handle method; otherwise, the middleware will never be triggered.

Note

You can throw an exception ErrorPageException::class with your custom data in case you need it.

Warning

Since Kirby 6, exceptions no longer accept an array as the only argument; use named arguments instead.

Redirection

Sometimes you don't want to display an error page when a user doesn't have access; in that case, you can redirect them instead:

'beebmx.patrol' => [
    'permissions' => [
        'redirect' => 'login',
    ],
],

As you can see, when a redirection is set, you don't need to customize an extra middleware.

Page button

You can manage the access to a single page directly from its page view with the patrol view button. The button is not displayed by default; enable it in every page blueprint where you need it:

title: Article
buttons:
  - patrol
  - open
  - preview
  - settings
  - status

Note

When you set the buttons of a blueprint, Kirby only displays the buttons in the list, so include the default buttons you want to keep.

The button opens a dialog with an Allowed/Restricted toggle for every role (and guests, when enabled), and displays a badge with the number of roles restricted for that page. It's only displayed for users with access to Patrol, and it's disabled for pages not patrolled by your content options.

Multilanguage

Patrol works with multi-language sites. In the Patrol view, the titles and URLs of the pages are displayed in the current content language, and you can switch the language from the header. The permissions are the same for every language.

The default middleware resolves the language of every request, so pages are patrolled with any language prefix (/es/blog) and with translated slugs (/es/bitacora).

Guests

By default, visitors without a logged in user (guests) are not patrolled. To control which pages guests can visit, enable guest support:

'beebmx.patrol' => [
    'permissions' => [
        'guest' => true,
    ],
],

A Guest tab is added to the Patrol view, where you can allow or restrict pages for guests, just like any other role. The guest permissions use the Kirby built-in nobody role and are applied by the default middleware and the $pages->patrol() utility.

Logs

Every time the permissions of a role are saved, from the Patrol view or the page button, Patrol logs who saved them, when, and which pages were allowed or restricted. Switch to the Logs view from the header of the Patrol view to see the logs of every role, newest first and paginated.

Only the newest 100 logs are kept for every role. You can change the limit or disable logs:

'beebmx.patrol' => [
    'logs' => [
        'enabled' => true,
        'limit' => 500,
    ],
],

To disable logs, 'logs' => false works the same as 'logs' => ['enabled' => false].

Logs are stored as JSON in their own file for every role (storage/patrol/logs/{role}.json), so the permissions read on every request are not affected by the amount of logs.

Hooks

Patrol triggers hooks every time the permissions of a role are saved:

use Kirby\Cms\Role;
use Kirby\Cms\User;

'hooks' => [
    'beebmx.patrol.save:before' => function (Role $role, array $permissions, ?User $user) {
        // Return an array to modify the permissions before they are saved
        return [...$permissions, 'secret-page' => false];
    },
    'beebmx.patrol.save:after' => function (Role $role, array $permissions, bool $saved, ?User $user) {
        // e.g. flush the pages cache or log the change
        kirby()->cache('pages')->flush();
    },
],
Hook Arguments Description
beebmx.patrol.save:before $role, $permissions, $user Runs before saving. Return an array to replace the permissions.
beebmx.patrol.save:after $role, $permissions, $saved, $user Runs after saving. $saved is true when the file was written.

Utilities

You have utilities available to incorporate into your existing workflow:

User utilities

If you want to validate if a user has access to a given Page:

user()->can($page)

Note

Page can be a string or a Kirby\Cms\Page object

If you want to retrieve all the pages with access or without access

user()->patrol(bool)

Note

A true value returns all pages with access. A false value returns all pages without access.

Pages utility

If you want to filter a pages collection by the access of the current user:

pages()->patrol(bool)

Note

A true value returns all pages with access. A false value returns all pages without access.

Options

Option Default Type Description
beebmx.patrol.enabled true bool Enable access in Kirby Panel
beebmx.patrol.icon keyhole string Icon displayed in Kirby Panel. Options available are: flash keyhole police shield siren star user
beebmx.patrol.name Patrol string Set a string to display in the Kirby Panel.
beebmx.patrol.content.columns 4 int Set how many columns are used to display subpages without children in the Patrol view.
beebmx.patrol.content.depth 2 int Set the depth to dig into the pages collection.
beebmx.patrol.content.direction asc string Set the sort direction of the content.
beebmx.patrol.content.sort title string Set the sort value for the content.
beebmx.patrol.content.query null ?Closure Use a specific query to display and validate by Patrol. It requires returning a collection of pages.
beebmx.patrol.logs true bool|array Set false to disable the logs, the same as logs.enabled.
beebmx.patrol.logs.enabled true bool Log who changed the permissions of every role, when, and which pages.
beebmx.patrol.logs.limit 100 int Max amount of logs kept for every role, older logs are removed.
beebmx.patrol.permissions.default true bool Set the default access of every page when no patrol values are set.
beebmx.patrol.permissions.enabled true bool Enable/Disable the default middleware functionality.
beebmx.patrol.permissions.guest false bool Patrol guests (visitors without a logged in user) and display a Guest tab in the Patrol view.
beebmx.patrol.permissions.middleware [] array Additional middleware functionality.
beebmx.patrol.permissions.redirect null ?string Redirect to a specific URL path instead of throwing a 401 error page.

Warning

Since version 1.3.0, Patrol changes the plugin prefix from beebmx.kirby-patrol to beebmx.patrol.

Here's an example of a full use of the options from the config.php file:

use Beebmx\KirbyMiddleware\Request;
use Kirby\Cms\App;
use Kirby\Cms\Pages;
use Kirby\Cms\Site;
use Kirby\Exception\ErrorPageException;

'beebmx.patrol' => [
    'name' => 'Profiles',
    'icon' => 'shield',
    'content' => [
        'columns' => 4,
        'depth' => 3,
        'query' => function (Site $site, Pages $pages, App $kirby) {
            return $site->find('private-content')->children()->listed();
        },
    ],
    'permissions' => [
        'redirect' => 'login',
        'default' => true,
        'middleware' => [
            function (Request $request, Closure $next) {
                if (page($request->path())?->id() === 'super-secret-page') {
                    throw new ErrorPageException(
                        fallback: 'Unauthorized',
                        httpCode: 401,
                    );
                }

                return $next($request);
            },
        ]
    ],
],

License

Licensed under the MIT.

Credits