Symfony Acl:我如何添加自定义掩码(MaskBuilder)?

时间:2013-10-12 08:51:19

标签: symfony acl

我想在屏蔽构建器中添加printsomething。我怎么能? 从官方食谱(http://symfony.com/doc/master/cookbook/security/acl.html#cumulative-permissions)我看不到如何添加特殊面具。

示例:

$builder = new MaskBuilder();
$builder
    ->add('view')
    ->add('edit')
    ->add('delete')
    ->add('undelete')
;
$mask = $builder->get(); // int(29)

根据我的问题,我想:

$builder = new MaskBuilder();
$builder
    ->add('view')
    ->add('print')
    ->add('or-something-i-want')
;
$mask = $builder->get();

3 个答案:

答案 0 :(得分:2)

您可以尝试扩展MaskBuilder并覆盖常量:

namespace Your\AclBundle\Permission;

use Symfony\Component\Security\Acl\Permission\MaskBuilder;

class YourMaskBuilder extends MaskBuilder
{
    const MASK_VIEW = 1; // 1 << 0
    const MASK_PRINT = 2; // 1 << 1
    const MASK_WHATYOUWANT = 4; // 1 << 2
    // ...

    const CODE_VIEW = 'V';
    const CODE_PRINT = 'P';
    const CODE_WHATYOUWANT = 'W';
    // ...
}

并使用:

$builder = new YourMaskBuilder();
$builder
    ->add('view')
    ->add('print')
    ->add('whatyouwant')
;
$mask = $builder->get();

答案 1 :(得分:2)

1-您需要创建自己的默认

的MAskBuilder
namespace Acm\DefaultBundle\Permission;

use Symfony\Component\Security\Acl\Permission\MaskBuilder as BaseMaskBuilder;


class MaskBuilder extends BaseMaskBuilder {

  const MASK_PRINT = 256; // 1 << 8
  const CODE_PRINT = 'A';   }

2-您需要替换PermissionMap

namespace Acm\DefaultBundle\Permission;

use Symfony\Component\Security\Acl\Permission\BasicPermissionMap;  
use Acm\DefaultBundle \Permission\MaskBuilder;

class PermissionMap extends BasicPermissionMap {


const PERMISSION_PRINT    = 'PRINT';
const PERMISSION_VIEW        = 'VIEW';
const PERMISSION_EDIT        = 'EDIT';
const PERMISSION_CREATE      = 'CREATE';
const PERMISSION_DELETE      = 'DELETE';
const PERMISSION_UNDELETE    = 'UNDELETE';
const PERMISSION_OPERATOR    = 'OPERATOR';
const PERMISSION_MASTER      = 'MASTER';
const PERMISSION_OWNER       = 'OWNER';

private $map = array(
    self::PERMISSION_VIEW => array(
        MaskBuilder::MASK_VIEW,
        MaskBuilder::MASK_EDIT,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),
    self::PERMISSION_PRINT => array(
        MaskBuilder::MASK_PRINT,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,),

    self::PERMISSION_EDIT => array(
        MaskBuilder::MASK_EDIT,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_CREATE => array(
        MaskBuilder::MASK_CREATE,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_DELETE => array(
        MaskBuilder::MASK_DELETE,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_UNDELETE => array(
        MaskBuilder::MASK_UNDELETE,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_OPERATOR => array(
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_MASTER => array(
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_OWNER => array(
        MaskBuilder::MASK_OWNER,
    ),
);

/**
 * {@inheritDoc}
 */
public function getMasks($permission, $object)
{
    if (!isset($this->map[$permission])) {
        return null;
    }

    return $this->map[$permission];
}

/**
 * {@inheritDoc}
 */
public function contains($permission)
{
    return isset($this->map[$permission]);
}

}

3-将此添加到config.yml以替换默认的PermissionsMap

parameters:
   security.acl.permission.map.class: Pitchbull\CalendarBundle\Permission\Permission\PermissionMap

4-您现在可以通过PRINT

来检查
     $securityContext = $this->get('security.context');

     // check for apply access
     if false === $securityContext->isGranted('PRINT', $service)) {

        //your access is denied
     }

答案 2 :(得分:1)

除了@Duvan s answer之外 在当前的symfony版本中,您必须覆盖地图,如下所示:

namespace Acm\DefaultBundle\Permission;

use Symfony\Component\Security\Acl\Permission\BasicPermissionMap;  
use Acm\DefaultBundle \Permission\MaskBuilder;

class PermissionMap extends BasicPermissionMap {

const PERMISSION_PRINT       = 'PRINT';
const PERMISSION_VIEW        = 'VIEW';
const PERMISSION_EDIT        = 'EDIT';
const PERMISSION_CREATE      = 'CREATE';
const PERMISSION_DELETE      = 'DELETE';
const PERMISSION_UNDELETE    = 'UNDELETE';
const PERMISSION_OPERATOR    = 'OPERATOR';
const PERMISSION_MASTER      = 'MASTER';
const PERMISSION_OWNER       = 'OWNER';

protected $map

public function __construct(){

    $this->map = array(
    self::PERMISSION_VIEW => array(
        MaskBuilder::MASK_VIEW,
        MaskBuilder::MASK_EDIT,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),
    self::PERMISSION_PRINT => array(
        MaskBuilder::MASK_PRINT,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,),

    self::PERMISSION_EDIT => array(
        MaskBuilder::MASK_EDIT,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_CREATE => array(
        MaskBuilder::MASK_CREATE,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_DELETE => array(
        MaskBuilder::MASK_DELETE,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_UNDELETE => array(
        MaskBuilder::MASK_UNDELETE,
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_OPERATOR => array(
        MaskBuilder::MASK_OPERATOR,
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_MASTER => array(
        MaskBuilder::MASK_MASTER,
        MaskBuilder::MASK_OWNER,
    ),

    self::PERMISSION_OWNER => array(
        MaskBuilder::MASK_OWNER,
    ),
);


}

    /**
     * {@inheritDoc}
     */
    public function getMasks($permission, $object)
    {
        if (!isset($this->map[$permission])) {
            return null;
        }

        return $this->map[$permission];
    }

    /**
     * {@inheritDoc}
     */
    public function contains($permission)
    {
        return isset($this->map[$permission]);
    }

}