【问题标题】: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】:
如果您使用的是高级模板,请按以下步骤操作;
- 在
frontend下创建一个目录并重命名为rbac
- 在这个新目录下创建一个文件,例如
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;
}
}
- 下一步是导航到
http://localhost/path/to/index.php?r=admin/rule 并创建一个类名为\app\rbac\AuthorRule 的新规则
- 最后,您可以根据需要将新规则添加到
roles 和permissions。
您可以阅读官方文档以获取有关规则的更多信息; the official docs.