gpt4 book ai didi

Symfony Acl : How i can add custom mask (MaskBuilder)?

转载 作者:行者123 更新时间:2023-12-02 11:14:34 27 4
gpt4 key购买 nike

我想向掩码生成器添加print某些内容。我怎么能?从官方食谱( 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();

最佳答案

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
}

关于Symfony Acl : How i can add custom mask (MaskBuilder)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19332376/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com