我建议阅读:http ://www.yiiframework.com/doc-2.0/guide-security-authorization.html#using-rules
您(可能)不想手动将数据添加到此表中。该name
字段将包含规则的名称,这也是auth_item
表中的外键。该data
列将包含实际上是规则的类的序列化版本。
因此,在文档的示例中,有一个 AuthorRule:
namespace app\rbac;
use yii\rbac\Rule;
use app\models\Post;
class AuthorRule extends Rule
{
public $name = 'isAuthor';
public function execute($user, $item, $params)
{
return isset($params['post']) ? $params['post']->createdBy == $user : false;
}
}
auth 项和规则使用 authManager 组件连接:
$auth = Yii::$app->authManager;
// add the rule
$rule = new \app\rbac\AuthorRule;
$auth->add($rule);
// add the "updateOwnPost" permission and associate the rule with it.
$updateOwnPost = $auth->createPermission('updateOwnPost');
$updateOwnPost->description = 'Update own post';
$updateOwnPost->ruleName = $rule->name;
$auth->add($updateOwnPost);
// "updateOwnPost" will be used from "updatePost"
$auth->addChild($updateOwnPost, $updatePost);
// allow "author" to update their own posts
$auth->addChild($author, $updateOwnPost);
使用 DbManager 时,表中的数据会自动填充正确的值。