【问题标题】:How and where to create Yii2 Access Rules using mdmsoft/yii2-admin如何以及在何处使用 mdmsoft/yii2-admin 创建 Yii2 访问规则
【发布时间】:2016-08-25 10:33:10
【问题描述】:

开发人员您好,我是 YII 新手,我已经安装了 YII2 框架并想要一个 RBAC 我已经安装了 mdmsoft/yii2-admin 模块,但是我不知道如何创建 RULE 类,在哪里创建它,然后如何采用。当我在管理部分创建一个角色时,它说,输入一个类名。我不知道如何创建和使用 YII 的 RULE 功能。我已附上屏幕截图。

【问题讨论】:

标签: php authentication frameworks rbac yii2-rbac


【解决方案1】:

如果您使用的是高级模板,请按以下步骤操作;

  1. frontend下创建一个目录并重命名为rbac
  2. 在这个新目录下创建一个文件,例如AuthorRule.php。这是官方文档中给出的示例文件;
namespace app\rbac;


use yii\rbac\Rule;
use app\models\Post;

/**
 * Checks if authorID matches user passed via params
 */
class AuthorRule extends Rule
{
    public $name = 'isAuthor';

    /**
     * @param string|int $user the user ID.
     * @param Item $item the role or permission that this rule is associated with
     * @param array $params parameters passed to ManagerInterface::checkAccess().
     * @return bool a value indicating whether the rule permits the role or permission it is associated with.
     */
    public function execute($user, $item, $params)
    {
        return isset($params['post']) ? $params['post']->createdBy == $user : false;
    }
}
  1. 下一步是导航到http://localhost/path/to/index.php?r=admin/rule 并创建一个类名为\app\rbac\AuthorRule 的新规则
  2. 最后,您可以根据需要将新规则添加到rolespermissions

您可以阅读官方文档以获取有关规则的更多信息; the official docs.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多