1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* Date: 2017-12-07 |
5
|
|
|
* Time: 03:18 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace MP\ExtendedApi; |
9
|
|
|
|
10
|
|
|
use MP\Services\ImplementServices; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\data\DataProviderInterface; |
13
|
|
|
use yii\db\ActiveRecordInterface; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
use yii\rest\ActiveController; |
16
|
|
|
use yii\web\ForbiddenHttpException; |
17
|
|
|
use yii\web\NotFoundHttpException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class EActiveController |
21
|
|
|
* @package MP\ExtendedApi |
22
|
|
|
* @author Yarmaliuk Mikhail |
23
|
|
|
* @version 1.0 |
24
|
|
|
*/ |
25
|
|
|
class EActiveController extends ActiveController |
26
|
|
|
{ |
27
|
|
|
use ImplementServices; |
28
|
|
|
|
29
|
|
|
const FILTER_ERROR_CODE = 405; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Search model class |
33
|
|
|
* |
34
|
|
|
* @var ActiveRecordInterface |
35
|
|
|
*/ |
36
|
|
|
public $searchClass; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Return error if empty filtered result |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
public $errorFilter = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* List external actions |
47
|
|
|
* |
48
|
|
|
* 'delete-all' => true, |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
public $externalActions = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check action access |
56
|
|
|
* |
57
|
|
|
* 'index' => 'rule', |
58
|
|
|
* 'update' => 'permission', |
59
|
|
|
* and etc... |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
public $checkAccessRules = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
public $actionsParams = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function actions(): array |
74
|
|
|
{ |
75
|
|
|
$actions = ArrayHelper::merge(parent::actions(), [ |
76
|
|
|
'index' => [ |
77
|
|
|
'class' => EIndexAction::class, |
78
|
|
|
], |
79
|
|
|
'create' => [ |
80
|
|
|
'class' => ECreateAction::class, |
81
|
|
|
], |
82
|
|
|
'view' => [ |
83
|
|
|
'class' => EViewAction::class, |
84
|
|
|
], |
85
|
|
|
'update' => [ |
86
|
|
|
'class' => EUpdateAction::class, |
87
|
|
|
], |
88
|
|
|
'delete' => [ |
89
|
|
|
'class' => EDeleteAction::class, |
90
|
|
|
], |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$actions = ArrayHelper::merge($actions, $this->actionsParams); |
94
|
|
|
|
95
|
|
|
if (!empty($this->searchClass)) { |
96
|
|
|
$actions['index']['dataFilter'] = [ |
97
|
|
|
'class' => EActiveDataFilter::class, |
98
|
|
|
'searchModel' => $this->searchClass, |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($this->externalActions as $externalAction => $value) { |
103
|
|
|
if ($value) { |
104
|
|
|
switch ($externalAction) { |
105
|
|
|
case 'delete-all': |
106
|
|
|
$actions[$externalAction] = $actions['index']; |
107
|
|
|
$actions[$externalAction]['class'] = EDeleteAllAction::class; |
108
|
|
|
break; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $actions; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
* |
119
|
|
|
* @param EIndexAction $action |
120
|
|
|
* @param mixed $result |
121
|
|
|
* |
122
|
|
|
* @throws NotFoundHttpException |
123
|
|
|
*/ |
124
|
|
|
public function afterAction($action, $result) |
125
|
|
|
{ |
126
|
|
|
if ($action->id === 'index' && $result instanceof DataProviderInterface) { |
127
|
|
|
if ($this->errorFilter && !empty($action->dataFilter->filter) && empty($result->getModels())) { |
128
|
|
|
$this->filterError(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return parent::afterAction($action, $result); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Throw error empty filtered result |
137
|
|
|
* |
138
|
|
|
* @throws NotFoundHttpException |
139
|
|
|
*/ |
140
|
|
|
public function filterError(): void |
141
|
|
|
{ |
142
|
|
|
throw new NotFoundHttpException(Yii::t('app', 'Nothing found'), self::FILTER_ERROR_CODE); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritdoc |
147
|
|
|
*/ |
148
|
|
|
public function checkAccess($action, $model = null, $params = []) |
149
|
|
|
{ |
150
|
|
|
if ($this->checkAccessRules[$action] ?? null) { |
151
|
|
|
$allow = Yii::$app->user->can($this->checkAccessRules[$action], ['model' => $model, 'params' => $params]); |
|
|
|
|
152
|
|
|
|
153
|
|
|
if (!$allow) { |
154
|
|
|
$this->forbidden(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Throw forbidden error |
161
|
|
|
* |
162
|
|
|
* @throws ForbiddenHttpException |
163
|
|
|
*/ |
164
|
|
|
protected function forbidden(): void |
165
|
|
|
{ |
166
|
|
|
throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.')); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.