gpt4 book ai didi

php - yii\rbac\DbManager 设置

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:59 24 4
gpt4 key购买 nike

尝试为 Yii2 设置 DbManager。有很多关于 php 版本的线程,但是,关于 DB 版本的线程不多。

我所知道的:

第一步:迁移脚本

./yii migrate --migrationPath=@yii/rbac/migrations/

第二步:配置

...
'authManager' => [
'class' => 'yii\rbac\DbManager',
'defaultRoles' => ['admin', 'user', 'guest'],
],
...

第三步:设置角色/规则

????

最佳答案

    $auth = Yii::$app->authManager;

// add "createPost" permission
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);

// add "updatePost" permission
$updatePost = $auth->createPermission('updatePost');
$updatePost->description = 'Update post';
$auth->add($updatePost);

// add "author" role and give this role the "createPost" permission
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);

// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole('admin');
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);

// Assign roles to users. 1 and 2 are IDs returned by IdentityInterface::getId()
// usually implemented in your User model.
$auth->assign($author, 2);
$auth->assign($admin, 1);

http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

关于php - yii\rbac\DbManager 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26004497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com