1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\rest; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\base\Model; |
12
|
|
|
use yii\web\ForbiddenHttpException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ActiveController implements a common set of actions for supporting RESTful access to ActiveRecord. |
16
|
|
|
* |
17
|
|
|
* The class of the ActiveRecord should be specified via [[modelClass]], which must implement [[\yii\db\ActiveRecordInterface]]. |
18
|
|
|
* By default, the following actions are supported: |
19
|
|
|
* |
20
|
|
|
* - `index`: list of models |
21
|
|
|
* - `view`: return the details of a model |
22
|
|
|
* - `create`: create a new model |
23
|
|
|
* - `update`: update an existing model |
24
|
|
|
* - `delete`: delete an existing model |
25
|
|
|
* - `options`: return the allowed HTTP methods |
26
|
|
|
* |
27
|
|
|
* You may disable some of these actions by overriding [[actions()]] and unsetting the corresponding actions. |
28
|
|
|
* |
29
|
|
|
* To add a new action, either override [[actions()]] by appending a new action class or write a new action method. |
30
|
|
|
* Make sure you also override [[verbs()]] to properly declare what HTTP methods are allowed by the new action. |
31
|
|
|
* |
32
|
|
|
* You should usually override [[checkAccess()]] to check whether the current user has the privilege to perform |
33
|
|
|
* the specified action against the specified model. |
34
|
|
|
* |
35
|
|
|
* For more details and usage information on ActiveController, see the [guide article on rest controllers](guide:rest-controllers). |
36
|
|
|
* |
37
|
|
|
* @author Qiang Xue <[email protected]> |
38
|
|
|
* @since 2.0 |
39
|
|
|
*/ |
40
|
|
|
class ActiveController extends Controller |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var string the model class name. This property must be set. |
44
|
|
|
*/ |
45
|
|
|
public $modelClass; |
46
|
|
|
/** |
47
|
|
|
* @var string the scenario used for updating a model. |
48
|
|
|
* @see \yii\base\Model::scenarios() |
49
|
|
|
*/ |
50
|
|
|
public $updateScenario = Model::SCENARIO_DEFAULT; |
51
|
|
|
/** |
52
|
|
|
* @var string the scenario used for creating a model. |
53
|
|
|
* @see \yii\base\Model::scenarios() |
54
|
|
|
*/ |
55
|
|
|
public $createScenario = Model::SCENARIO_DEFAULT; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
6 |
|
public function init() |
62
|
|
|
{ |
63
|
6 |
|
parent::init(); |
64
|
6 |
|
if ($this->modelClass === null) { |
65
|
|
|
throw new InvalidConfigException('The "modelClass" property must be set.'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function actions() |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
'index' => [ |
76
|
|
|
'class' => 'yii\rest\IndexAction', |
77
|
|
|
'modelClass' => $this->modelClass, |
78
|
|
|
'checkAccess' => [$this, 'checkAccess'], |
79
|
|
|
], |
80
|
|
|
'view' => [ |
81
|
|
|
'class' => 'yii\rest\ViewAction', |
82
|
|
|
'modelClass' => $this->modelClass, |
83
|
|
|
'checkAccess' => [$this, 'checkAccess'], |
84
|
|
|
], |
85
|
|
|
'create' => [ |
86
|
|
|
'class' => 'yii\rest\CreateAction', |
87
|
|
|
'modelClass' => $this->modelClass, |
88
|
|
|
'checkAccess' => [$this, 'checkAccess'], |
89
|
|
|
'scenario' => $this->createScenario, |
90
|
|
|
], |
91
|
|
|
'update' => [ |
92
|
|
|
'class' => 'yii\rest\UpdateAction', |
93
|
|
|
'modelClass' => $this->modelClass, |
94
|
|
|
'checkAccess' => [$this, 'checkAccess'], |
95
|
|
|
'scenario' => $this->updateScenario, |
96
|
|
|
], |
97
|
|
|
'delete' => [ |
98
|
|
|
'class' => 'yii\rest\DeleteAction', |
99
|
|
|
'modelClass' => $this->modelClass, |
100
|
|
|
'checkAccess' => [$this, 'checkAccess'], |
101
|
|
|
], |
102
|
|
|
'options' => [ |
103
|
|
|
'class' => 'yii\rest\OptionsAction', |
104
|
|
|
], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
1 |
|
protected function verbs() |
112
|
|
|
{ |
113
|
1 |
|
return [ |
114
|
1 |
|
'index' => ['GET', 'HEAD'], |
115
|
1 |
|
'view' => ['GET', 'HEAD'], |
116
|
1 |
|
'create' => ['POST'], |
117
|
1 |
|
'update' => ['PUT', 'PATCH'], |
118
|
1 |
|
'delete' => ['DELETE'], |
119
|
1 |
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Checks the privilege of the current user. |
124
|
|
|
* |
125
|
|
|
* This method should be overridden to check whether the current user has the privilege |
126
|
|
|
* to run the specified action against the specified data model. |
127
|
|
|
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown. |
128
|
|
|
* |
129
|
|
|
* @param string $action the ID of the action to be executed |
130
|
|
|
* @param object|null $model the model to be accessed. If null, it means no specific model is being accessed. |
131
|
|
|
* @param array $params additional parameters |
132
|
|
|
* @throws ForbiddenHttpException if the user does not have access |
133
|
|
|
*/ |
134
|
|
|
public function checkAccess($action, $model = null, $params = []) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.