有没有人有这个扩展的工作示例。 我在谈论: https://github.com/mdmsoft/yii2-admin
我正在寻找Yii app basic。
我安装并正常工作,但我不知道如何配置"角色"和"规则"
在Yii 1.xxx我使用了http://www.yiiframework.com/extension/authbooster/,但这在Yii2.xx中不起作用
答案 0 :(得分:0)
创建自定义模型AccessRules.php
<?php
namespace app\models;
class AccessRules extends \yii\filters\AccessRule
{
/**
* @inheritdoc
*/
protected function matchRole($user)
{
if (empty($this->roles)) {
return true;
}
foreach ($this->roles as $role) {
if ($role === '?') {
if ($user->getIsGuest()) {
return true;
}
} elseif ($role === '@') {
if (!$user->getIsGuest()) {
return true;
}
// Check if the user is logged in, and the roles match
} elseif (!$user->getIsGuest() && (int)$role === $user->identity->user_role) {
return true;
}
}
return false;
}
}
?>
现在在站点控制器中添加角色:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
// We will override the default rule config with the new AccessRule class
'ruleConfig' => [
'class' => AccessRules::className(),
],
'only' => ['create', 'update', 'delete','index'],
'rules' => [
[
'actions' => ['create', 'update', 'delete','index'],
'allow' => true,
// Allow admin to create
'roles' => [
'1'
],
]
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}