|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\v1\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use app\core\actions\CreateAction; |
|
6
|
|
|
use app\core\exceptions\InternalException; |
|
7
|
|
|
use app\core\exceptions\InvalidArgumentException; |
|
8
|
|
|
use app\core\helpers\SearchHelper; |
|
9
|
|
|
use sizeg\jwt\JwtHttpBearerAuth; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\InvalidConfigException; |
|
12
|
|
|
use yii\base\Model; |
|
13
|
|
|
use yii\data\ActiveDataProvider; |
|
14
|
|
|
use yii\db\ActiveRecord; |
|
15
|
|
|
use yii\filters\Cors; |
|
16
|
|
|
use yii\web\ForbiddenHttpException; |
|
17
|
|
|
use yiier\helpers\SearchModel; |
|
18
|
|
|
use yiier\helpers\Setup; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* @property-read int $pageSize |
|
23
|
|
|
*/ |
|
24
|
|
|
class ActiveController extends \yii\rest\ActiveController |
|
25
|
|
|
{ |
|
26
|
|
|
protected const MAX_PAGE_SIZE = 100; |
|
27
|
|
|
protected const DEFAULT_PAGE_SIZE = 20; |
|
28
|
|
|
public $defaultOrder = ['id' => SORT_DESC]; |
|
29
|
|
|
public $partialMatchAttributes = []; |
|
30
|
|
|
public $stringToIntAttributes = []; |
|
31
|
|
|
public $relations = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* 不参与校验的 actions |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
public $noAuthActions = []; |
|
38
|
|
|
|
|
39
|
|
|
// 序列化输出 |
|
40
|
|
|
public $serializer = [ |
|
41
|
|
|
'class' => 'yii\rest\Serializer', |
|
42
|
|
|
'collectionEnvelope' => 'items', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
public function behaviors() |
|
46
|
|
|
{ |
|
47
|
|
|
$behaviors = parent::behaviors(); |
|
48
|
|
|
|
|
49
|
|
|
// 跨区请求 必须先删掉 authenticator |
|
50
|
|
|
$behaviors['authenticator']; |
|
51
|
|
|
unset($behaviors['authenticator']); |
|
52
|
|
|
|
|
53
|
|
|
$behaviors['corsFilter'] = [ |
|
54
|
|
|
'class' => Cors::class, |
|
55
|
|
|
'cors' => [ |
|
56
|
|
|
'Origin' => ['*'], |
|
57
|
|
|
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], |
|
58
|
|
|
'Access-Control-Request-Headers' => ['*'], |
|
59
|
|
|
'Access-Control-Max-Age' => 86400, |
|
60
|
|
|
] |
|
61
|
|
|
]; |
|
62
|
|
|
$behaviors['authenticator'] = [ |
|
63
|
|
|
'class' => JwtHttpBearerAuth::class, |
|
64
|
|
|
'optional' => array_merge($this->noAuthActions, ['options']), |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
return $behaviors; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function actions() |
|
71
|
|
|
{ |
|
72
|
|
|
$actions = parent::actions(); |
|
73
|
|
|
$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider']; |
|
74
|
|
|
$actions['create']['class'] = CreateAction::class; |
|
75
|
|
|
return $actions; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return ActiveDataProvider |
|
80
|
|
|
* @throws InvalidArgumentException |
|
81
|
|
|
* @throws InternalException |
|
82
|
|
|
* @throws InvalidConfigException |
|
83
|
|
|
* @throws \Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function prepareDataProvider() |
|
86
|
|
|
{ |
|
87
|
|
|
/** @var ActiveRecord $modelClass */ |
|
88
|
|
|
$modelClass = $this->modelClass; |
|
89
|
|
|
$searchModel = new SearchModel([ |
|
90
|
|
|
'defaultOrder' => $this->defaultOrder, |
|
91
|
|
|
'model' => $modelClass, |
|
92
|
|
|
'relations' => $this->relations, |
|
93
|
|
|
'scenario' => 'default', |
|
94
|
|
|
'partialMatchAttributes' => $this->partialMatchAttributes, |
|
95
|
|
|
'pageSize' => $this->getPageSize() |
|
96
|
|
|
]); |
|
97
|
|
|
|
|
98
|
|
|
$params = $this->formatParams(Yii::$app->request->queryParams); |
|
99
|
|
|
foreach ($this->stringToIntAttributes as $attribute => $className) { |
|
100
|
|
|
if ($type = data_get($params, $attribute)) { |
|
101
|
|
|
$params[$attribute] = SearchHelper::stringToInt($type, $className); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
unset($params['sort']); |
|
105
|
|
|
|
|
106
|
|
|
$dataProvider = $searchModel->search(['SearchModel' => $params]); |
|
107
|
|
|
$dataProvider->query->andWhere([$modelClass::tableName() . '.user_id' => Yii::$app->user->id]); |
|
108
|
|
|
return $dataProvider; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
protected function formatParams(array $params) |
|
113
|
|
|
{ |
|
114
|
|
|
return $params; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return int |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getPageSize() |
|
121
|
|
|
{ |
|
122
|
|
|
if ($pageSize = (int)request('pageSize')) { |
|
123
|
|
|
if ($pageSize < self::MAX_PAGE_SIZE) { |
|
124
|
|
|
return $pageSize; |
|
125
|
|
|
} |
|
126
|
|
|
return self::MAX_PAGE_SIZE; |
|
127
|
|
|
} |
|
128
|
|
|
return self::DEFAULT_PAGE_SIZE; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param Model $model |
|
134
|
|
|
* @param array $params |
|
135
|
|
|
* @return Model |
|
136
|
|
|
* @throws InvalidArgumentException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function validate(Model $model, array $params): Model |
|
139
|
|
|
{ |
|
140
|
|
|
$model->load($params, ''); |
|
141
|
|
|
if (!$model->validate()) { |
|
142
|
|
|
throw new InvalidArgumentException(Setup::errorMessage($model->firstErrors)); |
|
143
|
|
|
} |
|
144
|
|
|
return $model; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $action |
|
149
|
|
|
* @param null $model |
|
|
|
|
|
|
150
|
|
|
* @param array $params |
|
151
|
|
|
* @throws ForbiddenHttpException |
|
152
|
|
|
*/ |
|
153
|
|
|
public function checkAccess($action, $model = null, $params = []) |
|
154
|
|
|
{ |
|
155
|
|
|
if (in_array($action, ['delete', 'update', 'view'])) { |
|
156
|
|
|
if ($model->user_id !== \Yii::$app->user->id) { |
|
157
|
|
|
throw new ForbiddenHttpException( |
|
158
|
|
|
t('app', 'You can only ' . $action . ' data that you\'ve created.') |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|