BaseController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace merchant\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\filters\AccessControl;
  6. use yii\web\UnauthorizedHttpException;
  7. use yii\web\ForbiddenHttpException;
  8. use common\traits\BaseAction;
  9. use common\helpers\Auth;
  10. use common\enums\StatusEnum;
  11. /**
  12. * Class BaseController
  13. * @package merchant\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 UnauthorizedHttpException
  45. * @throws \yii\web\BadRequestHttpException
  46. */
  47. public function beforeAction($action)
  48. {
  49. if (!parent::beforeAction($action)) {
  50. return false;
  51. }
  52. // 判断商户的有效性
  53. $merchantId = Yii::$app->user->identity->merchant_id ?? 0;
  54. if (
  55. !($merchant = Yii::$app->services->merchant->findById($merchantId)) ||
  56. $merchant->status == StatusEnum::DELETE ||
  57. $merchant->audit_status != StatusEnum::ENABLED
  58. ) {
  59. Yii::$app->user->logout();
  60. throw new ForbiddenHttpException('对不起,您还无法登陆请联系管理员');
  61. }
  62. // 每页数量
  63. $this->pageSize = Yii::$app->request->get('per-page', 10);
  64. $this->pageSize > 50 && $this->pageSize = 50;
  65. // 判断当前模块的是否为主模块, 模块+控制器+方法
  66. $permissionName = '/' . Yii::$app->controller->route;
  67. // 判断是否忽略校验
  68. if (in_array($permissionName, Yii::$app->params['noAuthRoute'])) {
  69. return true;
  70. }
  71. // 开始权限校验
  72. if (!Auth::verify($permissionName)) {
  73. throw new ForbiddenHttpException('对不起,您现在还没获此操作的权限');
  74. }
  75. // 记录上一页跳转
  76. $this->setReferrer($action->id);
  77. return true;
  78. }
  79. }
粤ICP备19079148号