1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace app\common\filters;
- use Yii;
- use yii\base\ActionFilter;
- use app\modules\admin\models\Resource;
- class BackEndResourceFilter extends ActionFilter
- {
- public function beforeAction($action)
- {
- $uniqueid = $action->controller->action->uniqueid;
- if($uniqueid=='admin/default/index')return true;//每个人都有打开首页的权限
- //未登录或COOKIE认证失败
- $identityInfo = Yii::$app->controller->getIdentityInfo();
- $role_id = $identityInfo['role_id'];
- $module = Yii::$app->controller->moduleName;
- $controller = Yii::$app->controller->controllerName;
- $actionName = Yii::$app->controller->actionName;
- $resource = Resource::find()->where('module = :module and controller=:controller and action=:action', [':module' => $module,':controller' => $controller,':action' => $actionName])->one();
- if($role_id==1)
- {
- return true;
- }
- else
- {
- $resultList = Resource::findBySql('SELECT hash FROM {{%resource}} where hash in(select hash from {{%role_resource}} where role_id='.$role_id.')')->orderBy(['role_resource_id' => SORT_ASC])->asArray()->all();
- foreach($resultList as $k=>$v)
- {
- $resourceHashList[] = $v['hash'];
- }
- if(in_array($resource->hash,$resourceHashList))
- {
- return true;
- }
- else
- {
- Yii::$app->controller->showMessage(array('class'=>'danger','message'=>Yii::t('admin','you do not have rights to this resource'),'url'=>Yii::$app->controller->createRealUrl('admin/default/logout'),'time'=>2000));
- return false;
- }
- }
- }
- }
|