You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB

  1. <?php
  2. namespace api\controllers;
  3. use yii\filters\auth\HttpBearerAuth;
  4. use yii\helpers\ArrayHelper;
  5. use yii\rest\ActiveController;
  6. use yii\web\NotFoundHttpException;
  7. use Yii;
  8. /**
  9. * @author iron
  10. * @email weiriron@gmail.com
  11. */
  12. class CommonController extends ActiveController
  13. {
  14. public $searchModel;
  15. public function behaviors()
  16. {
  17. return ArrayHelper::merge(parent::behaviors(), [
  18. 'authenticatior' => [
  19. 'class' => HttpBearerAuth::className(),
  20. 'except' => [],
  21. ]
  22. ]);
  23. }
  24. public function actions()
  25. {
  26. $action = parent::actions();
  27. unset($action['create']);
  28. unset($action['update']);
  29. unset($action['index']);
  30. return $action;
  31. }
  32. protected function getFilter()
  33. {
  34. return [];
  35. }
  36. public function checkAccess($action, $model = null, $params = [])
  37. {
  38. if ($model && isset($model->user_id) && $model->user_id !== Yii::$app->user->getId()) {
  39. switch ($action) {
  40. case 'view':
  41. $message = '您无权访问该数据';
  42. break;
  43. case 'delete':
  44. $message = '您无权删除该数据';
  45. break;
  46. default:
  47. $message = '无相关权限';
  48. }
  49. throw new NotFoundHttpException($message);
  50. }
  51. }
  52. }