godisco/acltree-bundle 问题修复 & 功能扩展

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

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

godisco/acltree-bundle

Composer 安装命令:

composer require godisco/acltree-bundle

包简介

Symfony2 Bundle for hierarchy relationship between entities with ACL Permissions.

README 文档

README

AclTree Bundle allows you to create hierarchy relationship between your entities for ACL Permissions.

For example, If I have Edit permissions for Dan Brown books entity, I will able to edit also the author Dan Brown, and all of his books.

Table of contents:

Installation:

Add the following line into your composer.json, at the require section:

composer.json
    "require": {
       "godisco/acltree-bundle": "dev-master"

Add the AclTree Bundle to your AppKernel (at the registerBundles() section):

app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new GoDisco\AclTreeBundle\AclTreeBundle(),
    }

Usage:

Using the AclTree is pretty simple! First, you need to Define your entities parents, then just use the voter to know if access is granted to user, or apply the helpers for complex queries.


Defining entity parent

Just add the @AclParent annotation to the parent member of your entity.

Example:

use GoDisco\AclTreeBundle\Annotation\AclParent;
use Acme\AuthorBundle\Entity\Author;

/**
 * Book
 * @ORM\Table
 * @ORM\Entity
 */
class Book
{
    /**
     * @var Author
     *
     * @ORM\ManyToOne(targetEntity="Acme\AuthorBundle\Entity\Author")
     * @ORM\JoinColumn(name="author_id", referencedColumnName="id")
     * @AclParent
     */
    private $author;
}

*** Don't forget to include the annotation by adding the use GoDisco\AclTreeBundle\Annotation\AclParent; part! ***

Using the voter

You can use the regular Symfony ACL voter, in order to know if access is granted to the user:

$vote= $this->get('security.context')->isGranted('VIEW', $entity);

(*) For more Information about using voters, visit the Symfony documentation.


In addition, you can also use the voter as annotation (duh?):

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

class BookController extends Controller
{
    /**
     * @Security("is_granted('VIEW', post)")
     */
    public function showAction(Post $post)
    {
        //...
    }
}

(*) For more Information about using security annotations, visit the Symfony documentation.

Using the AclTree helper

For filtering only entities the user have access to, apply the @acl.tree.helper service on your QueryBuilder object:

Methods:

  • apply(QueryBuilder $queryBuilder, [array $permissions = array("VIEW"), UserInterface $user = null])
    • $queryBuilder(*) - QueryBuilder object which select all the entities, before the filtering.
    • $permissions - array of permissions to check, default will check only for VIEW
    • $user - User entity to check, default will check for the logged-in user
    • returns modified Query object.

Example:

    /** @var \Doctrine\ORM\EntityManager\EntityManager $em */
    $em = $this->getDoctrine()->getManager();
    /** @var \GoDisco\AclTreeBundle\Security\Helper\AclTreeHelper $aclHelper */
    $aclHelper = $this->get("acl.tree.helper");
    
    $qb = $em->createQueryBuilder();
    $qb = $qb->select('e')
        ->from('GoDisco\EventBundle\Entity\Event', 'e')
        ->join("e.line", "l");

    // The query object returned here is a clone obj so, you can always use $qb->getQuery() to get the original query obj
    $query = $aclHelper->apply($qb);  // showing for current logged-in user
    $result = $query->getArrayResult();
    
    return $result;

Using the AclUsers helper

For showing all the users who have directly access to the entity, apply the acl.object.users service on your Entity object:

Methods:

  • get($entity, $user_class[array $permissions = array("VIEW"))
    • $entity(*) - entity to check
    • $user_class(*) - the class of the user object
    • $permissions - array of permissions to check, default will check only for EDIT
    • returns list of users.

Example:

    /** @var \Doctrine\ORM\EntityManager\EntityManager $em */
    $em = $this->getDoctrine()->getManager();

    $repo = $em->getRepository("VenueBundle:Venue");
    $entity = $repo->find(1);

    /** @var \GoDisco\AclTreeBundle\Security\Helper\AclUsersHelper $aclHelper */
    $aclHelper = $this->get("acl.object.users");
    
    $users = $aclUsers->get($entity, 'Acme\UserBundle\Entity\User'); // showing for current logged-in user
    return $users;

Changing the default MaskBuilder

In some cases, you may wish to change the default MaskBuilder, to a custom mask map. You can just modify the MaskBuilder by overriding the security.acl.mask_builder parameter.

For instance, example for changing the AclTree to use The Sonata's ACL MaskBuilder:

parameters:
    security.acl.mask_builder: Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder

统计信息

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

GitHub 信息

  • Stars: 17
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-01