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.

91 lines
2.1 KiB

  1. <?php
  2. namespace api\controllers;
  3. use api\logic\AddressLogic;
  4. use api\logic\CartLogic;
  5. use api\logic\CollectionLogic;
  6. use api\logic\CommentLogic;
  7. use api\logic\OrderLogic;
  8. use yii\filters\auth\HttpBearerAuth;
  9. use yii\helpers\ArrayHelper;
  10. use yii\rest\ActiveController;
  11. use yii\web\NotFoundHttpException;
  12. use yii\base\InvalidConfigException;
  13. use Yii;
  14. /**
  15. * @author iron
  16. * @email weiriron@gmail.com
  17. */
  18. class CommonController extends ActiveController
  19. {
  20. public $className;
  21. /**
  22. * @var OrderLogic|CommentLogic|CartLogic|CollectionLogic|AddressLogic;
  23. */
  24. public $object;
  25. /**
  26. * @throws InvalidConfigException
  27. */
  28. public function init()
  29. {
  30. parent::init();
  31. $this->object = Yii::createObject([
  32. 'class' => $this->className,
  33. ]);
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function behaviors()
  39. {
  40. return ArrayHelper::merge(parent::behaviors(), [
  41. 'authenticatior' => [
  42. 'class' => HttpBearerAuth::className(),
  43. 'except' => ['token'],
  44. ]
  45. ]);
  46. }
  47. public function actions()
  48. {
  49. $action = parent::actions();
  50. unset($action['create']);
  51. unset($action['update']);
  52. unset($action['index']);
  53. return $action;
  54. }
  55. protected function getFilter()
  56. {
  57. return [];
  58. }
  59. /**
  60. * @param string $action
  61. * @param null $model
  62. * @param array $params
  63. * @throws NotFoundHttpException
  64. * 权限控制
  65. */
  66. public function checkAccess($action, $model = null, $params = [])
  67. {
  68. if ($model && isset($model->user_id) && $model->user_id !== Yii::$app->user->getId()) {
  69. switch ($action) {
  70. case 'view':
  71. $message = '您无权访问该数据';
  72. break;
  73. case 'delete':
  74. $message = '您无权删除该数据';
  75. break;
  76. default:
  77. $message = '无相关权限';
  78. }
  79. throw new NotFoundHttpException($message);
  80. }
  81. }
  82. }