Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Extend/Core/Arch/AdminController.php (1 issue)

1
<?php
2
3
namespace Extend\Core\Arch;
4
5
use Ffcms\Core\App;
6
use Apps\ActiveRecord\App as AppRecord;
7
use Ffcms\Core\Exception\ForbiddenException;
8
use Ffcms\Core\Helper\Serialize;
9
use Ffcms\Core\Helper\Type\Any;
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
    /**
33
     * AdminController constructor.
34
     * @param bool $checkVersion
35
     * @throws ForbiddenException
36
     */
37
    public function __construct(bool $checkVersion = true)
38
    {
39
        parent::__construct();
40
        $this->buildExtensions();
41
        $this->checkAccess();
42
43
        // if version is not necessary to check - continue
44
        if (!$checkVersion) {
45
            return;
46
        }
47
48
        // get extension record based on type
49
        $record = $this->getTypeItem();
50
51
        // check if extension is loaded
52
        if (!$record) {
53
            throw new ForbiddenException(__('This extension is not installed'));
54
        }
55
56
        // check extension version
57
        if (!method_exists($record, 'checkVersion') || $record->checkVersion() !== true) {
58
            App::$Session->getFlashBag()->add(
59
                'error',
60
                __('Attention! Version of this extension scripts is no match to database version. Please, make update!')
61
            );
62
        }
63
    }
64
65
    /**
66
     * Build apps/widgets table in local property
67
     */
68
    private function buildExtensions()
69
    {
70
        $controller = Str::lastIn(get_class($this), '\\', true);
71
        foreach ($this->table as $item) {
72
            if ($item->type === 'app') {
73
                $this->applications[] = $item;
74
                if ($this->type === 'app' && $item->sys_name === $controller) {
75
                    $this->application = $item;
76
                }
77
            } elseif ($item->type === 'widget') {
78
                $this->widgets[] = $item;
79
                if ($this->type === 'widget' && $item->sys_name === $controller) {
80
                    $this->widget = $item;
81
                }
82
            }
83
        }
84
    }
85
86
    /**
87
     * Check if current user can access to admin controllers
88
     * @throws ForbiddenException
89
     */
90
    private function checkAccess()
91
    {
92
        $user = App::$User->identity();
93
        // user is not authed ?
94
        if (!$user || !App::$User->isAuth()) {
95
            $redirectUrl = App::$Alias->scriptUrl . '/user/login';
96
            App::$Response->redirect($redirectUrl, true);
97
            exit();
98
        }
99
100
        $permission = env_name . '/' . App::$Request->getController() . '/' . App::$Request->getAction();
101
        // doesn't have permission? get the f*ck out
102
        if (!$user->role->can($permission)) {
103
            throw new ForbiddenException(__('You got no access rights to this page'));
104
        }
105
    }
106
107
    /**
108
     * Get all extensions as table active record
109
     * @return \Illuminate\Database\Eloquent\Collection|static[]
110
     */
111
    public function getTable()
112
    {
113
        return $this->table;
114
    }
115
116
    /**
117
     * Get all extensions as active records by current type
118
     * @param string|null $type
119
     * @return mixed
120
     */
121
    public function getTypeTable($type = null)
122
    {
123
        if (!$type) {
124
            $type = $this->type;
125
        }
126
127
        return $type === 'widget' ? $this->widgets : $this->applications;
128
    }
129
130
    /**
131
     * Get current extension active record
132
     * @param string|null $type
133
     * @return mixed
134
     */
135
    public function getTypeItem($type = null)
136
    {
137
        if (!$type) {
138
            $type = $this->type;
139
        }
140
141
        return $type === 'widget' ? $this->widget : $this->application;
142
    }
143
144
    /**
145
     * Get current application configs as array
146
     * @return array
147
     */
148
    public function getConfigs(): ?array
149
    {
150
        $configs = $this->type === 'widget' ? (array)$this->widget->configs : (array)$this->application->configs;
151
        foreach ($configs as $cfg => $value) {
152
            if (Any::isInt($value)) {
153
                $configs[$cfg] = $value;
154
            }
155
        }
156
        return $configs;
157
    }
158
159
    /**
160
     * Save extension configs
161
     * @param array $configs
162
     * @return bool
163
     */
164
    public function setConfigs(array $configs = null): bool
165
    {
166
        if ($configs === null || !Any::isArray($configs) || count($configs) < 1) {
167
            return false;
168
        }
169
170
        // get extension is based on it type
171
        $id = 0;
172
        if ($this->type === 'app') {
173
            $id = $this->application->id;
174
        } elseif ($this->type === 'widget') {
175
            $id = $this->widget->id;
176
        }
177
178
        // get active record relation for this id
179
        $obj = \Apps\ActiveRecord\App::find($id);
180
181
        if (!$obj) {
182
            return false;
183
        }
184
185
        // save data in db
186
        $obj->configs = $configs;
0 ignored issues
show
Documentation Bug introduced by zenn
It seems like $configs of type array is incompatible with the declared type string of property $configs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
187
        $obj->save();
188
        return true;
189
    }
190
}
191