BackEndResourceFilter.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace app\common\filters;
  3. use Yii;
  4. use yii\base\ActionFilter;
  5. use app\modules\admin\models\Resource;
  6. class BackEndResourceFilter extends ActionFilter
  7. {
  8. public function beforeAction($action)
  9. {
  10. $uniqueid = $action->controller->action->uniqueid;
  11. if($uniqueid=='admin/default/index')return true;//每个人都有打开首页的权限
  12. //未登录或COOKIE认证失败
  13. $identityInfo = Yii::$app->controller->getIdentityInfo();
  14. $role_id = $identityInfo['role_id'];
  15. $module = Yii::$app->controller->moduleName;
  16. $controller = Yii::$app->controller->controllerName;
  17. $actionName = Yii::$app->controller->actionName;
  18. $resource = Resource::find()->where('module = :module and controller=:controller and action=:action', [':module' => $module,':controller' => $controller,':action' => $actionName])->one();
  19. if($role_id==1)
  20. {
  21. return true;
  22. }
  23. else
  24. {
  25. $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();
  26. foreach($resultList as $k=>$v)
  27. {
  28. $resourceHashList[] = $v['hash'];
  29. }
  30. if(in_array($resource->hash,$resourceHashList))
  31. {
  32. return true;
  33. }
  34. else
  35. {
  36. 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));
  37. return false;
  38. }
  39. }
  40. }
  41. }