ホームページ  >  記事  >  バックエンド開発  >  Yii2 は RBAC を使用します

Yii2 は RBAC を使用します

WBOY
WBOYオリジナル
2016-08-08 09:24:22992ブラウズ

1. /basic/config/console.php および /basic/config/web.php でコンポーネントを設定します。console.php のコードのみがここに掲載されます:

<?php

Yii::setAlias(&#39;@tests&#39;, dirname(__DIR__) . &#39;/tests&#39;);

$params = require(__DIR__ . &#39;/params.php&#39;);
$db = require(__DIR__ . &#39;/db.php&#39;);

return [
    &#39;id&#39; => 'basic-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log', 'gii'],
    'controllerNamespace' => 'app\commands',
    'modules' => [
        'gii' => 'yii\gii\Module',
    ],
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,'authManager' => [
    				'class' => 'yii\rbac\DbManager',
    				'itemTable' => 'web_auth_item',
    				'assignmentTable' => 'web_auth_assignment',
    				'itemChildTable' => 'web_auth_item_child',
        			'ruleTable'=>'web_auth_rule'
    		],
    ],
    'params' => $params,
];
console.php に設定がない場合は、次のエラーを報告します:

You should configure "authManager" component to use database before executing this migration.

2. コマンドライン

3.cd コマンドを開き、/php/basic ディレクトリに切り替えます

4. コマンドを入力します: yii merge --migrationPath=@yii/rbac /migrations/

5 .権限の作成:

    public function createPermission($item)
    {
        $auth = Yii::$app->authManager;

        $createPost = $auth->createPermission($item);
        $createPost->description = '创建了 ' . $item . ' 许可';
        $auth->add($createPost);
    }

6.ロールの作成:

public function createRole($item)
    {
        $auth = Yii::$app->authManager;

        $role = $auth->createRole($item);
        $role->description = '创建了 ' . $item . ' 角色';
        $auth->add($role);
    }

7.ロール割り当て権限

 static public function createEmpowerment($items)
    {
        $auth = Yii::$app->authManager;

        $parent = $auth->createRole($items['name']);
        $child = $auth->createPermission($items['description']);

        $auth->addChild($parent, $child);
    }

8.ロール割り当てユーザー:

 static public function assign($item)
    {
        $auth = Yii::$app->authManager;
        $reader = $auth->createRole($item['name']);
        $auth->assign($reader, $item['description']);
    }

9.権限の確認:

 public function beforeAction($action)
    {
        $action = Yii::$app->controller->action->id;
        if(\Yii::$app->user->can($action)){
            return true;
        }else{
            throw new \yii\web\UnauthorizedHttpException('对不起,您现在还没获此操作的权限');
        }
    }

10.コントローラーでの権限の確認

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\web\AccessControl::className(),
                'only' => ['login', 'logout', 'signup'],
                'rules' => [
                    [
                        'actions' => ['login', 'signup'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }
    // ...

11.コントローラーでの確認をカスタマイズする

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\web\AccessControl::className(),
                'only' => ['special-callback'],
                'rules' => [
                    [
                        'actions' => ['special-callback'],
                        'allow' => true,
                        'matchCallback' => function ($rule, $action) {
                            return date('d-m') === '31-10';
                        }
                    ],

   // ...
    // Match callback called! 此页面可以访问只有每个10月31日
    public function actionSpecialCallback()
    {
        return $this->render('happy-halloween');
    }

上記では、Yii2 での RBAC の使用法をさまざまな側面を含めて紹介しましたが、PHP チュートリアルに興味のある友人に役立つことを願っています。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。