ifgm / acl-interface-bundle
Symfony2 的工作级联访问控制(ACL),以及作为表单生成器的ACL管理器
Requires
- doctrine/common: ~2.2
- doctrine/dbal: ~2.2
- doctrine/doctrine-bundle: 1.2.*@dev
- symfony/form: 2.*
- symfony/framework-bundle: 2.*
- symfony/security-bundle: 2.*
This package is not auto-updated.
Last update: 2024-09-24 06:16:24 UTC
README
IfgmACLInterfaceBundle 是一个高度可定制的包,它提供了一个ACL管理器和接口,允许您管理任何实体的用户数组的ACL。这是一种方便的方法,可以让用户在几个步骤中管理对他们的内容的访问。
您将能够通过简单的表单来管理ACL,并使用标准指令 $this->get('security.context')->isGranted('EDIT', $object) 检查访问权限
安装
1. 获取包
在 composer.json 中添加(见 https://composer.php.ac.cn/)
"require" : {
// ...
"ifgm/acli-interface-bundle":"dev-master",
}
2. 注册包
要开始使用此包,请将其注册到您的Kernel中
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Ifgm\ACLInterfaceBundle\IfgmACLInterfaceBundle(), ); // ... }
3. 准备一些实体
您必须声明一些实体以使ACL正常工作。
您的 用户 实体必须实现 Ifgm\ACLInterfaceBundle\Model\UserInterface,可以受到角色影响的实体必须实现 Ifgm\ACLInterfaceBundle\Model\EntityInterface
不必担心,符合这些接口所需做的唯一事情是提供一个 getId() 函数。
4. 配置包
您需要添加一些配置才能使其工作
# app/config/config.yml ifgm_acl_interface: user: class: Acme\DemoBundle\Entity\User
您还需要声明可以为您的实体提供的访问权限。让我们假设您想管理对私有论坛的访问,您首先需要定义可管理的访问权限。为此,您必须在您的包中创建一个新的配置文件。
# Acme/DemoBundle/Resource/config/acls.yml acl_config: Acme\DemoBundle\Entity\Forum: - EDIT : Can edit forum details - CREATE : Can create sub-forums - DELETE : Can delete this forum - UNDELETE : Can undelete this forum - OWNER : Is owner of the forum
这些ACL(编辑、创建等)是由默认的MaskBuilder提供的,这是用于symfony2内置ACL系统的。您将在最后一章中了解到如何自定义它。
您还需要声明一个服务以注入您的配置
# Acme/DemoBundle/Resource/config/services.yml services: acme_demo.front_acls: class: %ifgm_acl_interface.config_manager.class% arguments: - @kernel - AcmeDemoBundle/Resources/config/acl_config.yml # Take care to the path syntax ! The same as Ressource location (@Acme...) but without the "@" - %ifgm_acl_interface.mask_builder.class% tags : - {name: ifgm_acl_interface.config_manager}
用法
现在您可以使用以下方式通过表单管理ACL
// Acme/DemoBundle/Controller/ForumController.php public function manageAclAction() { // Get an array of users, please note they have to be indexed by id $users = $this->getDoctrine() ->getRepository('AcmeDemoBundle:User') ->findAllIndexedById(); $forum = $this->getDoctrine ->getRepository('AcmeDemoBundle:Forum') ->find(1); // This will create the form, process request if form is submitted, persist and flush updates $form = $this->get('ifgm.acl_manager')->manageForm($users, $forum); // Or you can disable flushing by setting third argument to false // $form = $this->get('ifgm.acl_manager')->manageForm($users, $forum, false); // You can display the forum with the default template, or just do what you want with $form // (which just need a $form->createView() to be displayed) return $this->get('ifgm.acl_manager')->renderForm($form); }
因此,您已经了解了,使ACL表单工作不过是代码中的一行!请注意,您可能希望使用以下Trait以便在您的UserRepository中使用 $this->setIndexBy($qb, 'u.id')。这似乎需要在 ->getQuery() 调用之后进行。 Ifgm\ACLInterfaceBundle\Repository\Helper\IndexByTrait
还有更多吗?
好吧!请注意,ACLManager有很多酷炫的功能供您使用
<?php $manager = $this->get('ifgm.acl_manager'); $manager->addRole('EDIT', $user, $object) $manager->revokeRole('EDIT', $user, $object) $manager->setRoles(array('EDIT'), $user, $object) // Will replace current user's roles by the ones provided in the array $manager->setBitmask(28) // Adds roles by using bitmask, use with care $manager->revokeAll($user, $object) // Revokes all roles of a user on target object $manager->revokeAllRolesFromUser($user) // Delete all ACLs entries for this user (e.g. for user deletion) $manager->revokeAllRolesOnObject($object) // The same for object deletion $manager->getUsersRoles($user, $object) // Get an array of roles array(in bitmask => 'role label', ...) $manager->getAllRolesOnObject($object) // Get an array of roles for each user $manager->manageForm($user, $object, $flush) $manager->renderForm($form) // Returns a Response object
自定义MaskBuilder
在这里,您可以找到完整的配置选项和默认值
ifgm_acl_interface: form_manager: class: Ifgm\ACLInterfaceBundle\Manager\FormManager mask_builder: class: Ifgm\ACLInterfaceBundle\Security\Acl\Permission\MaskBuilder # Please note this MaskBuilder refers to the default one provided with Symfony2, just flavoured with some custom functions permission_map: class: Symfony\Component\Security\Acl\Permission\BasicPermissionMap acl_voter: class: Ifgm\ACLInterfaceBundle\Security\Authorization\Voter\AclVoter acl: class: Ifgm\ACLInterfaceBundle\Entity\Acl user: class: # Must be set
您可以定义自己的MaskBuilder,只需确保它实现了以下接口: Ifgm\ACLInterfaceBundle\Security\Acl\Permission\MaskBuilderInterface
您可能希望使用以下trait以确保有包所需的方法: Ifgm\ACLInterfaceBundle\Security\Acl\Permission\MaskBuilderTrait