Controller.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Controller is the customized base controller class.
  4. * All controller classes for this application should extend from this base class.
  5. */
  6. class Controller extends CController
  7. {
  8. /**
  9. * @var string the default layout for the controller view. Defaults to '//layouts/column1',
  10. * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
  11. */
  12. public $layout='//layouts/column1';
  13. public array $authIds = [];
  14. public array $companyIds = [];
  15. public array $schoolIds = [];
  16. /**
  17. * 检查请求方是否合法
  18. * @return void
  19. * @throws CHttpException
  20. */
  21. public function checkRequest(): void
  22. {
  23. if (LWM_ENV == 'dev') {
  24. header("Access-Control-Allow-Origin: *");
  25. } else {
  26. if (!str_contains(Yii::app()->request->hostInfo, Yii::app()->params['url'])) {
  27. throw new CHttpException(403, '非法访问');
  28. }
  29. header("Access-Control-Allow-Origin:" . Yii::app()->request->hostInfo);
  30. }
  31. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  32. header("Access-Control-Allow-Headers: Content-Type, Authorization, Cookie");
  33. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  34. exit(0); // 预检请求直接返回
  35. }
  36. }
  37. public function checkSign()
  38. {
  39. if (!\Yii::app()->request->isPostRequest) {
  40. return true;
  41. }
  42. if (!isset($_POST['sign'])) {
  43. return false;
  44. }
  45. $paramArray = [];
  46. unset($paramArray['sign']); // 签名不计算sign
  47. $stringArray = []; // 对参与签名的参数进行排序
  48. foreach ($_POST as $k => $v) {
  49. $stringArray[] = "{$k}={$v}";
  50. }
  51. sort($stringArray, SORT_STRING);
  52. $query = implode('&', $stringArray) . 'v1wqe21wmjhop';
  53. $sign = strtoupper(hash('sha256', $query));
  54. Logger::errorMult($query, $sign, $sign == $_POST['sign']);
  55. return $_POST['sign'] == $sign;
  56. }
  57. /**
  58. * @throws CHttpException
  59. */
  60. public function beforeAction($action): bool
  61. {
  62. $this->checkRequest();
  63. Yii::app()->language = 'zh_cn';
  64. $controller = Yii::app()->controller->id;
  65. $action = $this->getAction()->getId();
  66. $path = strtolower($controller . '/'. $action);
  67. if( !in_array($controller, ['site'])
  68. &&!in_array($path, LewaimaiAdminPingtaiAuth::$noLoginRouters)
  69. && Yii::app()->user->isGuest
  70. ){
  71. Helper::error('请先登入', 401);
  72. }
  73. // 获取权限相关数据
  74. $this->_formatAuth();
  75. if (!LewaimaiAdminPingtaiAuth::adminAuth($controller, $action)
  76. && (!Yii::app()->user->isGuest && Yii::app()->user->_id != 1)
  77. ) {
  78. Helper::error('您没有相应的权限');
  79. }
  80. return true;
  81. }
  82. private function _formatAuth(): void
  83. {
  84. $key = 'user_auth_' . Yii::app()->user->_id;
  85. $data = RedisInstance::getInstance()->get($key);
  86. if (!$data) {
  87. $model = Useradmin::model()->findByPk(Yii::app()->user->_id);
  88. $authIds = DB::getScalerWithCriteria(
  89. 'role',
  90. DbCriteria::simpleCompare(['id' => $model->role_id])->setSelect('auth_ids')
  91. );
  92. $this->authIds = $authIds ? explode(',', $authIds) : [];
  93. $this->companyIds = $model->company_ids ? explode(',', $model->company_ids) : [];
  94. $this->schoolIds = $model->school_ids ? explode(',', $model->school_ids) : [];
  95. $json = json_encode([
  96. 'authIds' => $this->authIds,
  97. 'companyIds' => $this->companyIds,
  98. 'schoolIds' => $this->schoolIds,
  99. ]);
  100. RedisInstance::getInstance()->set($key, $json, 86400);
  101. } else {
  102. $data = json_decode($data, true);
  103. $this->authIds = $data['authIds'];
  104. $this->companyIds = $data['companyIds'];
  105. $this->schoolIds = $data['schoolIds'];
  106. }
  107. }
  108. public function clearAuth($id = 0)
  109. {
  110. $id = $id ? $id : Yii::app()->user->_id;
  111. RedisInstance::getInstance()->delete('user_auth_' . $id);
  112. }
  113. public function getSchoolFilter($filed = 'school_id'):?array
  114. {
  115. if (Yii::app()->user->_id == 1 || in_array(-1, $this->companyIds)) {
  116. return null;
  117. }
  118. return $this->schoolIds;
  119. }
  120. public function checkSchoolId(int $id):bool
  121. {
  122. if (Yii::app()->user->_id == 1 || in_array(-1, $this->schoolIds)) {
  123. return true;
  124. }
  125. return in_array($id, $this->schoolIds);
  126. }
  127. public function getCompanyFilter():?array
  128. {
  129. if (Yii::app()->user->_id == 1 || in_array(-1, $this->companyIds)) {
  130. return null;
  131. }
  132. return $this->companyIds;
  133. }
  134. public function checkCompanyId(int $id):bool
  135. {
  136. if (Yii::app()->user->_id == 1 || in_array(-1, $this->companyIds)) {
  137. return true;
  138. }
  139. return in_array($id, $this->companyIds);
  140. }
  141. }