alichry / laminas-authorization
Laminas 的授权
Requires
- php: ^7.2
- alichry/laminas-accesscontrol: ^1.0.0
- alichry/laminas-build-delegator: ^0.1.0
- doctrine/annotations: ^1.10
- doctrine/common: ^2.0
- laminas/laminas-authentication: ^2.7
- laminas/laminas-mvc: 3.1.1
- laminas/laminas-servicemanager: ^3.4
- laminas/laminas-session: ^2.9
Requires (Dev)
- phpunit/phpunit: ^7.5
README
默认情况下,Laminas 的认证模块提供用户认证接口和会话存储中保存用户身份的服务。该模块通过透明地重定向访问特定资源的未经授权用户,支持定义多个授权链接形成授权链,提供授权服务。您可以通过在方法上创建注解或在配置中设置来配置控制器/方法授权状态列表(策略)。
安装
使用 composer 安装,运行
$ composer require alichry/laminas-authorization
将模块 AliChry\Laminas\AccessControl
和 AliChry\Laminas\Authorization
添加到 config/modules.config.php
先决条件
此模块不涉及用户认证,而是仅检查(认证的)身份的授权状态。要创建授权链接,我们需要
AuthenticationServiceInterface
:您可以使用 doctrine/doctrine-module 简单地配置一个认证服务。AccessControlListInterface
: alichry/laminas-accesscontrol 提供可配置的服务。默认情况下,此模块使用IdentityAccessControlList
实例,并使用AnnotatedResourceManager
作为依赖项,同时传递实现IdentityInterface
的身份。
快速入门
创建具有授权支持的 Laminas 应用程序的最快途径是通过 Doctrine ORM 集成。如果您不熟悉 Doctrine ORM,请查看 doctrine 项目网站和 doctrine/doctrine-orm-module
- 让您的身份类型类实现
IdentityInterface
。要实现的方法是hasPermission
和hasRole
,在关联已定义的 ORM 环境中,实现此类方法很容易。 - 授权服务需要一个认证服务,配置
Doctrine\Authentication
以快速部署基于您的身份类型的认证服务。 - 通过定义一个“全局”授权链接来配置此模块
<?php # module.config.php use Laminas\Authentication\AuthenticationService; return [ // ... 'alichry' => [ 'authorization' => [ 'chain' => [ 'global' => [ 'redirect_route' => 'login', 'authentication_service' => AuthenticationService::class, 'access_control_list' => 'identity' ] ] ] ], // ... ];
授权服务已配置,您可以在方法上定义注解来指示授权策略。
使用注解定义方法策略
在控制器的方法或类 docblock 上,您可以定义 @Authorization
注解,指示
- 目标链接名称。
- 策略:允许、拒绝、认证或授权。
- 权限:如果指定的策略是授权,则还应指定权限。
您可以定义多个注解,每个注解具有不同的链接名称。此外,您还可以省略链接名称,它将被视为回退。
示例
<?php use AliChry\Laminas\Authorization\Annotation\Authorization; /** * Class-level annotations are treated as a fallback. First method annotations * are consulted, if no relevant method annotations were found then * class-level annotations are utilized. * * Default class policy is to reject unspecified links: * @Authorization(policy="Reject") * * Require valid authentication status for "global" link: * @Authorization(link="global", policy="Authenticate") */ class ApplicationController { /** * Allow this resource to be publicly accessible: * @Authorization(policy="Allow") * The above will override class-level annotations, and since the link * property was omitted, it will apply to all links. */ public function indexAction() { } /** * Allow this resource to be accessible by entities granted the "delete" * permission under the "global" link: * @Authorization(link="global", policy="Authorize", permission="delete") */ public function deleteAction() { } /** * No annotations are defined for this method, the class annotations * will be used as a fallback. This method requires the user to be * authenticated for the "global" link or the request is rejected for all * other links. */ public function profileAction() { } }
授权链接
授权链接可以推断(认证的)身份是否有权访问控制器或控制器操作。
这通过依赖 AuthenticationService
(用于身份验证状态)和来自alichry/laminas-accesscontrol 的 AccessControlListInterface
实现,后者表示控制器或控制器方法的访问级别或授权级别。
最终,授权链接可以表示(经过身份验证的)身份是否被授予访问特定资源(控制器/操作)的权限,并将返回结果。
授权链
授权链由一个或多个授权链接组成,授权结果使用指定的二元运算符(OR/AND)聚合。尽管大多数应用程序通常只使用一个链接,但这主要与设计相关。
如果您正在构建应用程序的管理端,您可能会使用不同的身份验证服务,因此需要额外的授权链接和 ACL。或者,您可以使用相同的身份验证服务,并为每个身份分配用户或管理员角色/权限(或类似的内容)。
未授权用户的重定向
我们在 MVC 生命周期中执行授权,并在向 RESTful 控制器派发请求之前。在 Laminas MVC 架构中,通过监听 MVC 派发事件,可以在派发之前检索基于操作的控制器要调用的目标方法。然而,对于 RESTful 控制器,目标方法无法在派发之前检索。我们提供了EigenRestfulController 作为临时解决方案。只需从 EigenRestfulController
扩展您的控制器,而不是从 AbstractRestfulController
扩展。
在授权过程中,无论是在 MVC 级别还是由 EigenRestfulController
执行,我们都会将未授权请求重定向到配置的路由。
配置
请参阅 config.md
如何提供帮助?
star 此存储库将很棒。这将有助于吸引更多贡献者,并让我很高兴收到 star!:)