定制 archict/firewall 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

archict/firewall

最新稳定版本:v0.2.0

Composer 安装命令:

composer require archict/firewall

包简介

Control access to resources

README 文档

README

Tests

Control access to your resources

How to use

The majority of the work is inside the config file firewall.yml:

providers:
  my_provider: \Class\Name\Of\My\User\Provider
access_control:
  - path: ^/admin # Path to match (a regex)
    provider: my_provider
    roles: [ "ADMIN" ] # Roles user need to have to see resources
    error: 404 # If user not authorized, then return this error code
  - path: ^/profile
    provider: my_provider
    roles: [ "USER" ]
    redirect_to: /login # If user not authorized, then return to this uri

Let's go in details!

User provider

To help firewall to get current user, you need to give it a User provider.

This Brick provides you the interface \Archict\Firewall\UserProvider:

<?php

namespace Archict\Firewall;

interface UserProvider 
{
    public function getCurrentUser(ServerRequestInterface $request): UserWithRoles;
}

The class you pass in the config must implement this interface. It can have dependencies like a Service, they will be injected during instantiation.

User is an interface also provided by this Brick:

<?php

namespace Archict\Firewall;

interface UserWithRoles
{
    /**
     * @return string[]
     */
    public function getRoles(): array;
}

Access control

This config tag must contain an array of rules.

Each rule must have at least the path tag. This tag define the path to match, it can be a pattern with the same rules as in Archict\router.

Then you have the choice between let the firewall check if user can access the resource (the check is based on user roles), or implement your own checker.

Firewall checker

If you choose to use firewall checker, then you must provide these 2 tags:

  • provider ➡ One of the previously defined provider
  • roles ➡ An array of string. User must have one of these roles to access resource

Then you can define the behavior with one these rules (only one):

  • error ➡ a HTTP error code to return
  • redirect_to ➡ return a 301 response with the specified uri

Your own checker

To use your own checker, your class must implement this interface:

<?php

namespace Archict\Firewall;

interface UserAccessChecker
{
    public function canUserAccessResource(ServerRequestInterface $request): bool;
}

This method returns true if user is authorized to see resource. It can throw an exception the same way as defined in Archict\router. Implementation of this interface can have some dependencies in its constructor, they will be injected during instantiation.

Then you can provide the class name to your rule with the tag checker:

access_control:
  - path: some/path
    checker: \My\Own\Checker

You can also provide one of the behavior tag (see Firewall checker) in case your method returns false.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-10