角色与权限

Mautic 通过权限对象定义自定义的角色权限。

权限的工作方式

Mautic 根据分配给插件级别的权限的位来计算权限。 位是整数,其值以翻倍的方式递增 - 1、2、4、8、16、32、64、128、512、1024,以此类推。 避免使用中间数字,例如 3 或 5,因为这样权限计算将不正确。

例如,如果 HelloWorldBundle 管理对 worlds 实体的访问,则 plugin:helloWorld:worlds 的权限集如下所示:

权限

view

1

edit

2

create

4

delete

8

full

16

Note

通常,plugin:helloWorld:worlds:view 用于在 Mautic 中请求权限。 此表示法告诉 Mautic 验证插件 HelloWorldBundleworlds 级别上的 view 权限。 级别允许插件为多个区域设置权限。

Mautic 将授予角色的所有权限的位值相加,并将结果存储在数据库中。 例如,如果一个角色具有 viewedit 访问权限,则存储的位值为 3。 如果授予 viewcreate 访问权限,则存储的位值为 5。

当需要进行权限验证时 - 例如 plugin:helloWorld:worlds:create - Mautic 会检查角色的 plugin:helloWorld:worlds 生成的位值是否包含位 4。 如果是,Mautic 将授予权限。

Note

full 权限授予对所有先前权限的访问权限,并且始终应该是最高的位。

使用权限

<?php

$security = $this->get('mautic.security');

// 检查用户是否具有单个权限
if ($security->isGranted('plugin:helloWorld:worlds:view')) {
    // do something
}

// 检查用户是否具有多个权限(必须授予所有才能为真)
if ($security->isGranted(
    array(
        'plugin:helloWorld:worlds:view',
        'plugin:helloWorld:worlds:create',
    )
)) {
    // do something
}

// 检查用户是否至少具有一个权限
if ($security->isGranted(
    array(
        'plugin:helloWorld:worlds:view',
        'plugin:helloWorld:worlds:edit',
    ),
    'MATCH_ONE'
)) {
    // do something
}

// 获取用户权限的数组
$permissions = $security->isGranted(
    array(
        'plugin:helloWorld:worlds:view',
        'plugin:helloWorld:worlds:edit',
    ),
    'RETURN_ARRAY'
);

if ($permissions['plugin:helloWorld:worlds:view']) {
    // do something
}

// 检查用户是否可以查看潜在客户
if ($security->isGranted('lead:leads:viewother')) {
    // do something
}

要确定用户是否具有特定权限,请使用 Mautic 安全服务,可以通过 mautic.security 服务获取。

Mautic 使用特定的表示方法来标识权限:

  • 核心模块: 使用格式 bundleName:permissionLevel:permission

  • 插件: 在前面添加 plugin: 前缀。例如,plugin:bundleName:permissionLevel:permission。 插件需要此前缀,因为它可以指示 Mautic 在 plugins/ 目录和 MauticPlugin 命名空间中搜索权限类。

核心模块或插件设置权限级别和权限。 例如,核心 UserBundle 有 usersroles 级别,每个级别都有 vieweditcreatedeletefull 权限。 要验证用户是否具有编辑角色的权限,请使用:

$security->isGranted('user:roles:edit');

创建自定义权限

插件通过定义一个权限类来创建自己的权限集。 每个权限类都必须扩展 Mautic\CoreBundle\Security\Permissions\AbstractPermissions

<?php
// plugins/HelloWorldBundle/Security/Permissions/HelloWorldPermissions.php

namespace MauticPlugin\HelloWorldBundle\Security\Permissions;

use Symfony\Component\Form\FormBuilderInterface;
use Mautic\CoreBundle\Security\Permissions\AbstractPermissions;

class HelloWorldPermissions extends AbstractPermissions
{
    public function __construct($params)
    {
        parent::__construct($params);

        $this->permissions = array(
            'worlds' => array(
                'use_telescope' => 1,
                'send_probe'    => 2,
                'visit'         => 4,
                'full'          => 1024
            )
        );

        // Add standard category permissions
        $this->addStandardPermissions('categories');
    }

    public function buildForm(FormBuilderInterface &$builder, array $options, array $data)
    {
        // Add standard category form fields
        $this->addStandardFormFields('helloWorld', 'categories', $builder, $data);
// 添加自定义的 ‘worlds’ 等级表单字段
$builder->add(

‘helloWorld:worlds’, ‘permissionlist’, array(

‘choices’ => array(

‘use_telescope’ => ‘plugin.helloworld.permissions.use_telescope’, ‘send_probe’ => ‘plugin.helloworld.permissions.send_probe’, ‘visit’ => ‘plugin.helloworld.permissions.visit’, ‘full’ => ‘mautic.core.permissions.full’,

), ‘label’ => ‘plugin.helloworld.permissions’, ‘data’ => (!empty($data[‘worlds’]) ? $data[‘worlds’] : array()), ‘bundle’ => ‘helloWorld’, ‘level’ => ‘worlds’

)

);

}

public function getName() {

return ‘helloWorld’;

}

}

大多数权限类需要三个方法:__construct()buildForm()getName()

__construct()

构造函数执行两个任务。它调用 parent::__construct($params) 或设置 $this->params = $params;。然后,它定义 $this->permissions 数组。此数组将权限组织成级别,其中每个级别包含分配给位的特定权限。

例如,在代码示例中,一个自定义的 worlds 级别包括 use_telescopesend_probevisitfull。要验证用户是否具有 worlds 级别的 send_probe 权限,请使用:

$mauticSecurity->isGranted('plugin:helloWorld:worlds:send_probe');

Mautic 提供辅助方法来处理常见的权限集:

方法

描述

addStandardPermissions()

设置 vieweditcreatedeletepublish`(可以选择排除)和 `full 权限。

addExtendedPermissions()

设置创建者级别的限制,包括 viewownviewothereditowneditothercreatedeleteowndeleteotherpublishownpublishotherfull

addManagePermission()

设置单个 manage 权限。

buildForm()

buildForm() 方法将权限切换添加到角色的表单中。有关表单构建器的详细信息,请参阅 Forms,并查看代码示例中的注释以了解实现细节。

Mautic 提供互补的辅助方法来处理常见的权限集:

方法

描述

addStandardFormFields()

将标准的权限集添加到表单中。

addExtendedFormFields()

将扩展的(创建者受限)权限添加到表单中。

addManageFormFields()

将单个管理元素添加到表单中。

getName()

This method is mandatory. The return value must match the bundleName and the filename. For example, if the bundle name is HelloWorldBundle, this method returns helloWorld and the file is HelloWorldPermissions.php.

Permission aliases

<?php

protected function getSynonym($name, $level)
{
    if ($name == 'send_satellite') {
        $name = 'send_probe';
    }

    return array($name, $level);
}

To add a permission alias, use the getSynonym() method. Mautic calls this method before determining each requested permission, giving an opportunity to change the permission level or name as needed.

For example, parent::getSynonym() recognizes editown as edit if the $this->permissions property for that level doesn’t define editown.

Manipulating permissions before saving

<?php

public function analyzePermissions(array &$permissions, $allPermissions, $isSecondRound = false)
{
    foreach ($permissions as $level => &$perms) {
        foreach ($perms as $perm) {
            $include = array();
            switch ($perm) {
                case 'send_probe':
                    $include = array('use_telescope');
                    break;
                case 'visit':
                    $include = array('use_telescope', 'send_probe');
                    break;
            }
            if (!empty($include)) {
                foreach ($include as $r) {
                    list($ignore, $r) = $this->getSynonym($level, $r);
                    if ($this->isSupported($level, $r) && !in_array($r, $perms)) {
                        $perms[] = $r;
                    }
                }
            }
        }
    }

    return false;
}

Plugins can adjust permissions based on other selections to prevent User error. For example, if a User has edit permission, they also require view permission. Use the analyzePermissions() method to modify permissions before Mautic persists them to the database.

To re-adjust permissions based on factors outside the Plugin’s control, return true from analyzePermissions(). This triggers a second round of analysis after all other bundles and Plugins finish their processing. During this round, the $isSecondRound argument is true.

Advanced isGranted() logic

Override the parent method - public function isGranted($userPermissions, $name, $level) - to run custom logic rather than simple bit comparisons. Use this to define unique behavior for the class’s own levels and individual permissions.

Advanced isSupported() logic

isSupported() 方法相同的规则适用。此方法用于确定一个 bundle 或 Plugin 是否包含请求的权限和权限级别。使用它来提供向后兼容性(BC)支持。