data-dog / acl-bundle
访问控制管理包
Requires
- php: >=5.4.0
- symfony/symfony: ~2.5
Requires (Dev)
- phpspec/prophecy-phpunit: ~1.0
- phpunit/phpunit: ~4.4
This package is not auto-updated.
Last update: 2024-09-14 16:29:25 UTC
README
ACL 不依赖于任何数据库。它是一个裸露的 ACL 管理器。该包仅注册 资源 和 访问策略 提供者。请参阅 DOCTRINE.md,它展示了如何配置数据库以管理策略。
- 有 symfony 分析器栏
- 不依赖于数据库
- 基本资源与策略概念
配置
这是默认的 ACL 包配置
acl: default_allowed: false # means that by default all ACL resources are denied resource: providers: config: true # by default looks in bundles for ACL resources annotations: true # looks for controller annotations transformers: doctrine: true # transforms entities or document resources with an ID at the end
ACL 资源
资源基本上由一个字符串表示。
$acl->isGranted("action", "app.resource.string");
例如 "app.resource.string.action"。动作是连接的。这样更容易存储和匹配资源。
- app.resource.string - 是资源访问点。
- 动作 - 是可以对该资源执行的操作。
ACL 资源提供者
提供者用于从包中收集所有 ACL 资源。ACL 提供者接口
namespace AclBundle\Resource; interface ProviderInterface { /** * Get a list of available ACL resources * * @return array - ['resource.string.action', ...] */ function resources(); }
所有提供者服务必须带有 acl.resource.provider 标签。它们应该根据接口要求构建资源映射。
包配置
默认情况下启用了此类型的 ACL 资源提供者。它查找配置文件: ../VendorBundle/Resources/config/acl_resources.yml 并从每个包中加载所有资源。
resources: - app_bundle.entity.page.view - app_bundle.entity.page.edit
ACL 策略提供者
ACL 策略提供者必须实现 AclBundle\Access\PolicyProviderInterface 并实现一个方法,该方法返回一个策略列表,其中键是资源或资源分支,值是布尔值 - 资源是否被授权或拒绝。
假设我们有这些资源
resources: - app.user.edit - app.user.view - app.user.remove - app.user.add
我们可以为叶子操作创建策略
acl: access: policies: luke@skywalker.com: - { resource: app.user.edit, granted: true } - { resource: app.user.view, granted: true } - { resource: app.user.add, granted: true }
或者我们可以通过授权分支并拒绝叶子来实现相同的事情
acl: access: policies: luke@skywalker.com: - { resource: app.user, granted: true } - { resource: app.user.remove, granted: false }
注意: 上面的配置是 ACL 包扩展配置。它应位于内核配置目录。
配置提供者
对于非常简单的用例,可以使用配置提供者。要启用它,acl 配置必须在映射中包含一些访问权限
acl: access: policies: admin: - { resource: app_bundle, allow: true } # allow every action for all resources under app_bundle someusername: - { resource: some.resource, allow: true } # allow all actions on some.resource - { resource: some.resource.edit, allow: false } # but deny - some.resource.edit - { another.resource.somewhere.create } # default allowed
它将根据当前登录用户的用户名从安全上下文中加载此访问映射。尽管用户模型必须实现 Symfony\Component\Security\Core\User\UserInterface
ACL 资源转换器
有时将对象转换为具有标识符的特定资源以进行深度权限检查可能很有用。例如,我们可以有 表单类型 资源,由名称标识
use AclBundle\Util; use AclBundle\Resource\TransformerInterface; use Symfony\Component\Form\FormTypeInterface; class FormTransformer implements TransformerInterface { public function supports($object) { return $object instanceof FormTypeInterface; } public function transform($object) { return 'form.' . Util::underscore($object->getName()); } }
然后,此转换器服务可以带有标签 acl.resource.transformer 进行注册,它接受一个优先级属性。当检查 acl 动作时
$container->get('acl.access.decision_manager')->isGranted('edit', $formTypeObject);
注意: 这些资源必须通过配置或资源提供者服务提供。
为了方便,创建一个服务别名
# app/config/config.yml or other services: acl: @acl.access.decision_manager
常见问题解答
问: 为什么它没有供应商命名空间。 答: 希望在你的项目中只需要一个 AclBundle,谢谢。
测试
使用 phpunit 进行测试。要运行所有测试
composer install
bin/phpunit