BaseController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\filters\AccessControl;
  6. use yii\web\ForbiddenHttpException;
  7. use common\traits\BaseAction;
  8. use common\helpers\Auth;
  9. /**
  10. * 基类
  11. *
  12. * Class BaseController
  13. * @package backend\controllers
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class BaseController extends Controller
  17. {
  18. use BaseAction;
  19. /**
  20. * @var string
  21. */
  22. public $layout = "@backend/views/layouts/main";
  23. /**
  24. * @return array
  25. */
  26. public function behaviors()
  27. {
  28. return [
  29. 'access' => [
  30. 'class' => AccessControl::class,
  31. 'rules' => [
  32. [
  33. 'allow' => true,
  34. 'roles' => ['@'], // 登录
  35. ],
  36. ],
  37. ],
  38. ];
  39. }
  40. /**
  41. * @param \yii\base\Action $action
  42. * @return bool
  43. * @throws ForbiddenHttpException
  44. * @throws \yii\web\BadRequestHttpException
  45. * @throws \yii\web\UnauthorizedHttpException
  46. */
  47. public function beforeAction($action)
  48. {
  49. if (!parent::beforeAction($action)) {
  50. return false;
  51. }
  52. // 每页数量
  53. $this->pageSize = Yii::$app->request->get('per-page', 10);
  54. $this->pageSize > 50 && $this->pageSize = 50;
  55. // 判断当前模块的是否为主模块, 模块+控制器+方法
  56. $permissionName = '/' . Yii::$app->controller->route;
  57. // 判断是否忽略校验
  58. if (in_array($permissionName, Yii::$app->params['noAuthRoute'])) {
  59. return true;
  60. }
  61. // 开始权限校验
  62. if (!Auth::verify($permissionName)) {
  63. throw new ForbiddenHttpException('对不起,您现在还没获此操作的权限');
  64. }
  65. // 记录上一页跳转
  66. $this->setReferrer($action->id);
  67. return true;
  68. }
  69. }
粤ICP备19079148号