yii2之RBAC权限控制ACF

1. 配置:

'components' => [
    'authManager' => [
        'class' => 'yii\rbac\PhpManager',
    ],
]

2. controllers/AuthController.php

<?php
namespace backend\controllers;

use Yii;
use backend\controllers\base\BaseController;

class AuthController extends BaseController
{
    public function actionAccess()
    {
        $auth = Yii::$app->authManager;
        // 创建和添加角色
        $admin = $auth->createRole('admin');
        $auth->add($admin);
        // 将角色分配到用户
        $auth->assign($admin, 1);
    }
}
# 访问auth/access进行创建,需要新建文件夹rbac

3. 注册控制

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['captcha', 'logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [ // 控制器方法绑定到角色
                        'actions' => ['signup'],
                        'allow' => true,
                        'roles' => ['admin']
                    ]
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

猜你喜欢

转载自www.cnblogs.com/maoriaty/p/9273012.html
今日推荐