Passed
Push — master ( c6150b...ef80b0 )
by Mihail
05:01
created

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

Severity
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($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 === false) {
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 === null) {
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
     */
89
    private function checkAccess()
90
    {
91
        $user = App::$User->identity();
92
        // user is not authed ?
93
        if ($user === null || !App::$User->isAuth()) {
94
            $redirectUrl = App::$Alias->scriptUrl . '/user/login';
95
            App::$Response->redirect($redirectUrl, true);
96
            exit();
97
        }
98
99
        $permission = env_name . '/' . App::$Request->getController() . '/' . App::$Request->getAction();
100
101
        // doesn't have permission? get the f*ck out
102
        if (!$user->role->can($permission)) {
103
            App::$Session->invalidate();
104
105
            $redirectUrl = App::$Alias->scriptUrl . '/user/login';
106
            App::$Response->redirect($redirectUrl, true);
107
            exit();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
108
        }
109
    }
110
111
    /**
112
     * Get all extensions as table active record
113
     * @return \Illuminate\Database\Eloquent\Collection|static[]
114
     */
115
    public function getTable()
116
    {
117
        return $this->table;
118
    }
119
120
    /**
121
     * Get all extensions as active records by current type
122
     * @param string|null $type
123
     * @return mixed
124
     */
125
    public function getTypeTable($type = null)
126
    {
127
        if (!$type) {
128
            $type = $this->type;
129
        }
130
131
        return $type === 'widget' ? $this->widgets : $this->applications;
132
    }
133
134
    /**
135
     * Get current extension active record
136
     * @param string|null $type
137
     * @return mixed
138
     */
139
    public function getTypeItem($type = null)
140
    {
141
        if (!$type) {
142
            $type = $this->type;
143
        }
144
145
        return $type === 'widget' ? $this->widget : $this->application;
146
    }
147
148
    /**
149
     * Get current application configs as array
150
     * @return array
151
     */
152
    public function getConfigs(): ?array
153
    {
154
        $configs = $this->type === 'widget' ? (array)$this->widget->configs : (array)$this->application->configs;
155
        foreach ($configs as $cfg => $value) {
156
            if (Any::isInt($value)) {
157
                $configs[$cfg] = $value;
158
            }
159
        }
160
        return $configs;
161
    }
162
163
    /**
164
     * Save extension configs
165
     * @param array $configs
166
     * @return bool
167
     */
168
    public function setConfigs(array $configs = null): bool
169
    {
170
        if ($configs === null || !Any::isArray($configs) || count($configs) < 1) {
171
            return false;
172
        }
173
174
        // get extension is based on it type
175
        $id = 0;
176
        if ($this->type === 'app') {
177
            $id = $this->application->id;
178
        } elseif ($this->type === 'widget') {
179
            $id = $this->widget->id;
180
        }
181
182
        // get active record relation for this id
183
        $obj = \Apps\ActiveRecord\App::find($id);
184
185
        if (!$obj) {
186
            return false;
187
        }
188
189
        // save data in db
190
        $obj->configs = $configs;
191
        $obj->save();
192
        return true;
193
    }
194
}
195