forked from Insolita/yii2-fractal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPostController.php
executable file
·273 lines (259 loc) · 10.8 KB
/
PostController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* @copyright Copyright (c) 2020 Insolita <[email protected]> and contributors
* @license https://github.com/insolita/yii2-fractal/blob/master/LICENSE
*/
namespace app\controllers;
use app\models\Comment;
use app\models\Post;
use app\transformers\PostTransformer;
use app\transformers\PostWithRelationsTransformer;
use DateTime;
use insolita\fractal\actions\CountAction;
use insolita\fractal\actions\CreateAction;
use insolita\fractal\actions\CreateRelationshipAction;
use insolita\fractal\actions\DeleteAction;
use insolita\fractal\actions\DeleteRelationshipAction;
use insolita\fractal\actions\ListAction;
use insolita\fractal\actions\UpdateAction;
use insolita\fractal\actions\UpdateRelationshipAction;
use insolita\fractal\actions\ViewAction;
use insolita\fractal\actions\ViewRelationshipAction;
use insolita\fractal\ActiveJsonApiController;
use insolita\fractal\IdOnlyTransformer;
use insolita\fractal\providers\CursorActiveDataProvider;
use insolita\fractal\providers\JsonApiActiveDataProvider;
use yii\base\DynamicModel;
use yii\data\ActiveDataFilter;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\helpers\StringHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use const SORT_ASC;
use const SORT_DESC;
class PostController extends ActiveJsonApiController
{
public $modelClass = Post::class;
public $resourceKey = 'posts';
public $transformer = PostTransformer::class;
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::class,
'only' => ['create-for-category', 'update-for-category'],
'authMethods' => [
HttpBearerAuth::class,
],
];
return $behaviors;
}
public function actions()
{
$actions = parent::actions();
$actions['list']['dataFilter'] = [
'class' => ActiveDataFilter::class,
'searchModel' => function () {
return (new DynamicModel(['name' => null, 'category_id' => null, 'author_id' => null]))
->addRule('category_id', 'integer')
->addRule('author_id', 'integer')
->addRule('name', 'trim')
->addRule('name', 'string');
},
];
$actions['list-for-user'] = [
'class' => ListAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'parentIdParam' => 'id',
'parentIdAttribute' => 'author_id',
];
$actions['list-for-category'] = [
'class' => ListAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'parentIdParam' => 'id',
'parentIdAttribute' => 'category_id',
];
$actions['view-for-category'] = [
'class' => ViewAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'parentIdParam' => 'categoryId',
'parentIdAttribute' => 'category_id',
];
$actions['create-for-category'] = [
'class' => CreateAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'parentIdParam' => 'categoryId',
'parentIdAttribute' => 'category_id',
];
$actions['update-for-category'] = [
'class' => UpdateAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'parentIdParam' => 'categoryId',
'parentIdAttribute' => 'category_id',
];
$actions['delete-for-category'] = [
'class' => DeleteAction::class,
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'parentIdParam' => 'categoryId',
'parentIdAttribute' => 'category_id',
'findModelFor' => function ($id, $parentId) {
return Post::find()->where(['category_id' => $parentId, 'id' => $id])->one();
},
];
$actions['related-category'] = [
'class' => ViewRelationshipAction::class,
'modelClass' => $this->modelClass,
'relationName' => 'category',
'resourceKey' => 'categories',
];
$actions['delete-related-category'] = [
'class' => DeleteRelationshipAction::class,
'modelClass' => $this->modelClass,
'relationName' => 'category',
];
$actions['related-author'] = [
'class' => ViewRelationshipAction::class,
'modelClass' => $this->modelClass,
'relationName' => 'author',
'resourceKey' => 'users',
];
$actions['related-comments'] = [
'class' => ViewRelationshipAction::class,
'modelClass' => $this->modelClass,
'relationName' => 'comments',
'resourceKey' => 'comments',
'transformer' => IdOnlyTransformer::class,
'dataProvider' => ['class' => CursorActiveDataProvider::class],
];
$actions['delete-related-comments'] = [
'class' => DeleteRelationshipAction::class,
'modelClass' => $this->modelClass,
'relationName' => 'comments',
];
$actions['update-related-comments'] = [
'class' => UpdateRelationshipAction::class,
'modelClass' => $this->modelClass,
'relationName' => 'comments',
];
$actions['create-related-comments'] = [
'class' => CreateRelationshipAction::class,
'modelClass' => $this->modelClass,
'relationName' => 'comments',
'resourceKey' => 'comments',
'transformer' => IdOnlyTransformer::class,
];
$actions['create2'] = [
'class' => CreateAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->createScenario,
'allowedRelations' => [
'comments' => ['idType' => 'integer'],
],
];
$actions['update2'] = [
'class' => UpdateAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'scenario' => $this->updateScenario,
'allowedRelations' => [
'comments' => ['idType' => 'integer', 'unlinkOnly' => true, 'validator' => function($model, $ids) {
$comments = Comment::findAll(['id' => $ids]);
foreach ($comments as $comment) {
$date =DateTime::createFromFormat('Y-m-d H:i:s', $comment->created_at);
if((int) $date->format('Y') < 2020) {
throw new HttpException(422, 'Old comments cannot be linked to this post');
}
}
}],
],
];
$actions['list-with-join'] = [
'class' => ListAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'prepareDataProvider' => function ($action, JsonApiActiveDataProvider $dp) {
$dp->query->joinWith(['category category'])
->select(['posts.*', 'category.id as cid', 'category.name as cat']);
$dp->sort->addAttributes([
'category.name',
'category.id'=>['asc' =>['category.id' => SORT_ASC], 'desc' => ['category.id' => SORT_DESC]]
]);
return $dp;
},
];
$actions['list-parent-with-join'] = [
'class' => ListAction::class,
'modelClass' => $this->modelClass,
'resourceKey' => $this->resourceKey,
'transformer' => $this->transformer,
'checkAccess' => [$this, 'checkAccess'],
'parentIdParam' => 'id',
'parentIdAttribute' => 'category_id',
'prepareDataProvider' => function ($action, JsonApiActiveDataProvider $dp) {
$dp->query->joinWith(['category category', 'comments'])
->addSelect(['posts.*', 'category.*', 'comments.*']);
return $dp;
},
];
$actions['count'] = [
'class' => CountAction::class,
'modelClass' => $this->modelClass,
'dataFilter' => [
'class' => ActiveDataFilter::class,
'searchModel' => function () {
return (new DynamicModel(['category_id' => null, 'author_id' => null]))
->addRule('category_id', 'integer')
->addRule('author_id', 'integer');
},
]
];
$actions['count-for-category'] = [
'class' => CountAction::class,
'modelClass' => $this->modelClass,
'parentIdAttribute' => 'category_id',
'parentIdParam' => 'id'
];
return $actions;
}
/**
* Checks the privilege of the current user.
* This method should be overridden to check whether the current user has the privilege
* to run the specified action against the specified data model.
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
* @param string $action the ID of the action to be executed
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
* @param array $params additional parameters
* @throws ForbiddenHttpException if the user does not have access
*/
public function checkAccess($action, $model = null, $params = [])
{
if ($model && $model->id > 7 && $model->id < 10) {
throw new ForbiddenHttpException("You haven't access permissions to this data", 403);
}
}
}