我刚刚发现并开始使用基于角色的访问控制 。
由于我正在为yii2使用高级模板,因此我想知道后端和前端层之间是否共享角色和权限,或者它们是否分开。
例如
<?php
namespace app\commands;
use Yii;
use yii\console\Controller;
class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;
// add "createPost" permission
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);
// add "author" role and give this role the "createPost" permission
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);
}
}
后端和前端都可以使用author和createpost吗?
谢谢!
RBAC组件基于公共部分。通常,如果它们基于数据库,则使用公共模型并共享相关的数据库表。
您可以在cofig区域的main.php的组件部分中声明此元素,如果在公共目录中执行此操作,则此组件可以在环境(前端,后端)之间以及最终在您分发给projectc的所有应用之间正确共享。
例如:common / config / main.php
'components' => [
.....
'authManager' => [
'class' => 'yii\rbac\DbManager',
'cache' => 'cache',
....
],
这意味着它们可以在前端和后端之间自然共享。