Controller.php 5.2 KB

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