beebmx/kirby-patrol 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

beebmx/kirby-patrol

Composer 安装命令:

composer require beebmx/kirby-patrol

包简介

Patrol for Kirby

README 文档

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

Download

Download and copy this repository to /site/plugins/kirby-patrol.

Composer

composer require beebmx/kirby-patrol

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

All users with panel access will see the new area and will be able to update the access to the pages on the website.

If you need to restrict this behavior, you can do so by adding the permission in the user YAML file:

title: Editor

permissions:
  beebmx.patrol:
    access: false

Note

The access is set to true by default

The pages displayed in the new panel area will be all the site childrens published (with status listed and unlisted) 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,
    ],
],

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.

Warning

It's important that you use a logged-in user when the validation occurs; otherwise, an error will be thrown. If the default

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 easyest way to add additional validation is with Closures. Added 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: an 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()) {
            return throw new ErrorPageException([
                'fallback' => 'Unauthorized',
                'httpCode' => 401,
            ]);
        }

        return $next($data);
    }
}

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.

Redirection

Sometimes you don't need an error in your website to display an error, in that cases you can make a redireccion:

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

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

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 know if a pages collection have access or not:

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 will be displayed into 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.permissions.default true bool Set the default values of all the checkboxes when no patrol values are set.
beebmx.patrol.permissions.enabled true bool Enable/Disable the default middleware functionality.
beebmx.patrol.permissions.middleware [] array Additional middleware functionality.
beebmx.patrol.permissions.redirect null ?string Disabled the default middleware functionality and changed it to redirect to a specific URL path.

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 Closure;

'beebmx.patrol' => [
    'name' => 'Profiles',
    'icon' => 'shield',
    'content' => [
        'columns' => 4,
        'depth' => 3,
        'query' => function (Site $site, Pages $pages, Kirby $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') {
                    return throw new ErrorPageException([
                        'fallback' => 'Unauthorized',
                        'httpCode' => 401,
                    ]);
                }

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

Roadmap

  • Custom hooks
  • Multilanguage support
  • Guest support

License

Licensed under the MIT.

Credits

beebmx/kirby-patrol 适用场景与选型建议

beebmx/kirby-patrol 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 86 次下载、GitHub Stars 达 9, 最近一次更新时间为 2024 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「security」 「middleware」 「Users」 「roles」 「kirby」 「kirby-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 beebmx/kirby-patrol 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 beebmx/kirby-patrol 我们能提供哪些服务?
定制开发 / 二次开发

基于 beebmx/kirby-patrol 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 86
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-18