1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Extend\Core\Arch; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Ffcms\Core\App; |
7
|
|
|
use Apps\ActiveRecord\App as AppRecord; |
8
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
9
|
|
|
use Ffcms\Core\Helper\Serialize; |
10
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
11
|
|
|
use Ffcms\Core\Helper\Type\Str; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AdminController - class to extend classic admin controllers by extension type. |
15
|
|
|
* Used: access security control, application listing, widget listing, current extension data |
16
|
|
|
* @package Extend\Core\Arch |
17
|
|
|
*/ |
18
|
|
|
class AdminController extends Controller |
19
|
|
|
{ |
20
|
|
|
public $type = 'app'; |
21
|
|
|
|
22
|
|
|
/** @var array $applications */ |
23
|
|
|
protected $applications; |
24
|
|
|
/** @var array $widgets */ |
25
|
|
|
protected $widgets; |
26
|
|
|
|
27
|
|
|
/** @var AppRecord $application */ |
28
|
|
|
protected $application; |
29
|
|
|
/** @var AppRecord $widget */ |
30
|
|
|
protected $widget; |
31
|
|
|
|
32
|
|
|
public function __construct($checkVersion = true) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
$this->buildExtensions(); |
36
|
|
|
$this->checkAccess(); |
37
|
|
|
|
38
|
|
|
// if version is not necessary to check - continue |
39
|
|
|
if ($checkVersion === false) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// get extension record based on type |
44
|
|
|
$record = $this->getTypeItem(); |
45
|
|
|
|
46
|
|
|
// check if extension is loaded |
47
|
|
|
if ($record === null) { |
48
|
|
|
throw new ForbiddenException(__('This extension is not installed')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// check extension version |
52
|
|
|
if (!method_exists($record, 'checkVersion') || $record->checkVersion() !== true) { |
53
|
|
|
App::$Session->getFlashBag()->add( |
54
|
|
|
'error', |
55
|
|
|
__('Attention! Version of this extension scripts is no match to database version. Please, make update!') |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Build apps/widgets table in local property |
62
|
|
|
*/ |
63
|
|
|
private function buildExtensions() |
64
|
|
|
{ |
65
|
|
|
$controller = Str::lastIn(get_class($this), '\\', true); |
66
|
|
|
foreach ($this->table as $item) { |
|
|
|
|
67
|
|
|
if ($item->type === 'app') { |
68
|
|
|
$this->applications[] = $item; |
69
|
|
|
if ($this->type === 'app' && $item->sys_name === $controller) { |
70
|
|
|
$this->application = $item; |
71
|
|
|
} |
72
|
|
|
} elseif ($item->type === 'widget') { |
73
|
|
|
$this->widgets[] = $item; |
74
|
|
|
if ($this->type === 'widget' && $item->sys_name === $controller) { |
75
|
|
|
$this->widget = $item; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if current user can access to admin controllers |
83
|
|
|
*/ |
84
|
|
|
private function checkAccess() |
85
|
|
|
{ |
86
|
|
|
$user = App::$User->identity(); |
87
|
|
|
// user is not authed ? |
88
|
|
View Code Duplication |
if ($user === null || !App::$User->isAuth()) { |
|
|
|
|
89
|
|
|
$redirectUrl = App::$Alias->scriptUrl . '/user/login'; |
90
|
|
|
App::$Response->redirect($redirectUrl, true); |
91
|
|
|
exit(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$permission = env_name . '/' . App::$Request->getController() . '/' . App::$Request->getAction(); |
95
|
|
|
|
96
|
|
|
// doesn't have permission? get the f*ck out |
97
|
|
View Code Duplication |
if (!$user->getRole()->can($permission)) { |
|
|
|
|
98
|
|
|
App::$Session->invalidate(); |
99
|
|
|
|
100
|
|
|
$redirectUrl = App::$Alias->scriptUrl . '/user/login'; |
101
|
|
|
App::$Response->redirect($redirectUrl, true); |
102
|
|
|
exit(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get all extensions as table active record |
108
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
109
|
|
|
*/ |
110
|
|
|
public function getTable() |
111
|
|
|
{ |
112
|
|
|
return $this->table; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get all extensions as active records by current type |
117
|
|
|
* @param string|null $type |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function getTypeTable($type = null) |
121
|
|
|
{ |
122
|
|
|
if ($type === null) { |
123
|
|
|
$type = $this->type; |
124
|
|
|
} |
125
|
|
|
return $type === 'widget' ? $this->widgets : $this->applications; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get current extension active record |
130
|
|
|
* @param string|null $type |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function getTypeItem($type = null) |
134
|
|
|
{ |
135
|
|
|
if ($type === null) { |
136
|
|
|
$type = $this->type; |
137
|
|
|
} |
138
|
|
|
return $type === 'widget' ? $this->widget : $this->application; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get current extension configs |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function getConfigs() |
146
|
|
|
{ |
147
|
|
|
return $this->type === 'widget' ? (array)Serialize::decode($this->widget->configs) : (array)Serialize::decode($this->application->configs); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Save extension configs |
152
|
|
|
* @param array $configs |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function setConfigs(array $configs = null) |
156
|
|
|
{ |
157
|
|
|
if ($configs === null || !Obj::isArray($configs) || count($configs) < 1) { |
158
|
|
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// get extension is based on it type |
162
|
|
|
$id = 0; |
163
|
|
|
if ($this->type === 'app') { |
164
|
|
|
$id = $this->application->id; |
165
|
|
|
} elseif ($this->type === 'widget') { |
166
|
|
|
$id = $this->widget->id; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// serialize configs |
170
|
|
|
$serialized = Serialize::encode($configs); |
171
|
|
|
// get active record relation for this id |
172
|
|
|
$obj = \Apps\ActiveRecord\App::find($id); |
173
|
|
|
|
174
|
|
|
if ($obj === null) { |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// save data in db |
179
|
|
|
$obj->configs = $serialized; |
180
|
|
|
$obj->save(); |
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.