1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: floor12 |
5
|
|
|
* Date: 17.09.2018 |
6
|
|
|
* Time: 23:07 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace floor12\backup\controllers; |
10
|
|
|
|
11
|
|
|
use floor12\backup\logic\BackupCreate; |
12
|
|
|
use floor12\backup\logic\BackupRestore; |
13
|
|
|
use floor12\backup\models\Backup; |
14
|
|
|
use floor12\backup\models\BackupFilter; |
15
|
|
|
use floor12\backup\models\ImportForm; |
16
|
|
|
use Throwable; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\base\InvalidConfigException; |
19
|
|
|
use yii\db\StaleObjectException; |
20
|
|
|
use yii\filters\AccessControl; |
21
|
|
|
use yii\filters\VerbFilter; |
22
|
|
|
use yii\web\BadRequestHttpException; |
23
|
|
|
use yii\web\Controller; |
24
|
|
|
use yii\web\NotFoundHttpException; |
25
|
|
|
use yii\web\UploadedFile; |
26
|
|
|
|
27
|
|
|
class AdminController extends Controller |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Backup |
32
|
|
|
*/ |
33
|
|
|
protected $model; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function behaviors() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
'access' => [ |
43
|
|
|
'class' => AccessControl::class, |
44
|
|
|
'rules' => [ |
45
|
|
|
[ |
46
|
|
|
'allow' => true, |
47
|
|
|
'roles' => [Yii::$app->getModule('backup')->administratorRoleName], |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
'verbs' => [ |
52
|
|
|
'class' => VerbFilter::class, |
53
|
|
|
'actions' => [ |
54
|
|
|
'index' => ['get'], |
55
|
|
|
'delete' => ['delete'], |
56
|
|
|
'backup' => ['post'], |
57
|
|
|
'restore' => ['post'], |
58
|
|
|
'import' => ['post'], |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function init() |
68
|
|
|
{ |
69
|
|
|
$this->layout = Yii::$app->getModule('backup')->adminLayout; |
|
|
|
|
70
|
|
|
parent::init(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function actionIndex() |
77
|
|
|
{ |
78
|
|
|
$model = new BackupFilter(); |
79
|
|
|
return $this->render('index', [ |
80
|
|
|
'model' => $model, |
81
|
|
|
'configs' => Yii::$app->getModule('backup')->configs ?: [] |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
* @throws NotFoundHttpException |
88
|
|
|
* @throws Throwable |
89
|
|
|
* @throws StaleObjectException |
90
|
|
|
*/ |
91
|
|
|
public function actionDelete() |
92
|
|
|
{ |
93
|
|
|
$this->getBackup((int)Yii::$app->request->post('id')); |
94
|
|
|
$this->model->delete(); |
95
|
|
|
$this->model->delete(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws InvalidConfigException |
100
|
|
|
* @throws NotFoundHttpException |
101
|
|
|
* @throws StaleObjectException |
102
|
|
|
* @throws Throwable |
103
|
|
|
*/ |
104
|
|
|
public function actionBackup() |
105
|
|
|
{ |
106
|
|
|
$config_id = Yii::$app->request->post('config_id'); |
107
|
|
|
Yii::createObject(BackupCreate::class, [$config_id])->run(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @throws InvalidConfigException |
112
|
|
|
* @throws NotFoundHttpException |
113
|
|
|
*/ |
114
|
|
|
public function actionRestore() |
115
|
|
|
{ |
116
|
|
|
$this->getBackup((int)Yii::$app->request->post('id')); |
117
|
|
|
Yii::createObject(BackupRestore::class, [$this->model])->run(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $id |
122
|
|
|
* @throws NotFoundHttpException |
123
|
|
|
*/ |
124
|
|
|
public function actionDownload($id) |
125
|
|
|
{ |
126
|
|
|
$this->getBackup((int)$id); |
127
|
|
|
Yii::$app->response->sendFile($this->model->getFullPath()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $config_id |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function actionImport() |
135
|
|
|
{ |
136
|
|
|
$model = new ImportForm(); |
137
|
|
|
$model->load(Yii::$app->request->post()); |
|
|
|
|
138
|
|
|
$model->file = UploadedFile::getInstance($model, 'file'); |
139
|
|
|
if (!$model->import()) { |
140
|
|
|
throw new BadRequestHttpException($model->getFirstError('file')); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param int $id |
146
|
|
|
* @throws NotFoundHttpException |
147
|
|
|
*/ |
148
|
|
|
protected function getBackup(int $id) |
149
|
|
|
{ |
150
|
|
|
$this->model = Backup::findOne($id); |
151
|
|
|
if (!$this->model) |
152
|
|
|
throw new NotFoundHttpException(Yii::t('app.f12.backup', 'Backup is not found.')); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.