ConfigManagerController::getTabs()   D
last analyzed

Complexity

Conditions 10
Paths 512

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 22
c 1
b 1
f 0
nc 512
nop 0
dl 0
loc 46
rs 4.1777

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use DI\DependencyException;
28
use DI\NotFoundException;
29
use Psr\Container\ContainerExceptionInterface;
30
use Psr\Container\NotFoundExceptionInterface;
31
use SP\Core\Acl\Acl;
32
use SP\Core\AppInfoInterface;
33
use SP\Core\Crypt\CryptSessionHandler;
34
use SP\Core\Events\Event;
35
use SP\Core\Exceptions\CheckException;
36
use SP\Core\Exceptions\ConstraintException;
37
use SP\Core\Exceptions\QueryException;
38
use SP\Core\Exceptions\SessionTimeout;
39
use SP\Core\Exceptions\SPException;
40
use SP\Core\Language;
41
use SP\Core\MimeTypes;
42
use SP\Modules\Web\Controllers\Helpers\TabsHelper;
43
use SP\Mvc\View\Components\DataTab;
44
use SP\Mvc\View\Components\SelectItemAdapter;
45
use SP\Plugin\PluginManager;
46
use SP\Providers\Auth\Ldap\LdapTypeInterface;
47
use SP\Providers\Log\LogInterface;
48
use SP\Providers\Mail\MailHandler;
49
use SP\Repositories\NoSuchItemException;
50
use SP\Services\Account\AccountService;
51
use SP\Services\Auth\AuthException;
52
use SP\Services\Backup\FileBackupService;
53
use SP\Services\Config\ConfigService;
54
use SP\Services\Crypt\TemporaryMasterPassService;
55
use SP\Services\Export\XmlExportService;
56
use SP\Services\ServiceException;
57
use SP\Services\Task\Task;
58
use SP\Services\User\UserService;
59
use SP\Services\UserGroup\UserGroupService;
60
use SP\Services\UserProfile\UserProfileService;
61
use SP\Storage\Database\DatabaseUtil;
62
use SP\Storage\File\FileException;
63
use SP\Storage\File\FileHandler;
64
use SP\Util\Util;
65
66
/**
67
 * Class ConfigManagerController
68
 *
69
 * @package SP\Modules\Web\Controllers
70
 */
71
final class ConfigManagerController extends ControllerBase
72
{
73
    /**
74
     * @var TabsHelper
75
     */
76
    protected $tabsHelper;
77
78
    /**
79
     * @throws ContainerExceptionInterface
80
     * @throws NotFoundExceptionInterface
81
     * @throws SPException
82
     */
83
    public function indexAction()
84
    {
85
        $this->getTabs();
86
    }
87
88
    /**
89
     * Returns a tabbed grid with items
90
     *
91
     * @throws ContainerExceptionInterface
92
     * @throws NotFoundExceptionInterface
93
     * @throws SPException
94
     */
95
    protected function getTabs()
96
    {
97
        $this->tabsHelper = $this->dic->get(TabsHelper::class);
98
99
        if ($this->checkAccess(Acl::CONFIG_GENERAL)) {
100
            $this->tabsHelper->addTab($this->getConfigGeneral());
101
        }
102
103
        if ($this->checkAccess(Acl::CONFIG_ACCOUNT)) {
104
            $this->tabsHelper->addTab($this->getAccountConfig());
105
        }
106
107
        if ($this->checkAccess(Acl::CONFIG_WIKI)) {
108
            $this->tabsHelper->addTab($this->getWikiConfig());
109
        }
110
111
        if ($this->checkAccess(Acl::CONFIG_LDAP)) {
112
            $this->tabsHelper->addTab($this->getLdapConfig());
113
        }
114
115
        if ($this->checkAccess(Acl::CONFIG_MAIL)) {
116
            $this->tabsHelper->addTab($this->getMailConfig());
117
        }
118
119
        if ($this->checkAccess(Acl::CONFIG_CRYPT)) {
120
            $this->tabsHelper->addTab($this->getEncryptionConfig());
121
        }
122
123
        if ($this->checkAccess(Acl::CONFIG_BACKUP)) {
124
            $this->tabsHelper->addTab($this->getBackupConfig());
125
        }
126
127
        if ($this->checkAccess(Acl::CONFIG_IMPORT)) {
128
            $this->tabsHelper->addTab($this->getImportConfig());
129
        }
130
131
        if ($this->checkAccess(Acl::CONFIG_GENERAL)) {
132
            $this->tabsHelper->addTab($this->getInfo());
133
        }
134
135
136
        $this->eventDispatcher->notifyEvent('show.config', new Event($this));
137
138
        $this->tabsHelper->renderTabs(Acl::getActionRoute(Acl::CONFIG), $this->request->analyzeInt('tabIndex', 0));
139
140
        $this->view();
141
    }
142
143
    /**
144
     * @return DataTab
145
     * @throws ContainerExceptionInterface
146
     * @throws NotFoundExceptionInterface
147
     * @throws CheckException
148
     */
149
    protected function getConfigGeneral()
150
    {
151
        $template = clone $this->view;
152
        $template->setBase('config');
153
        $template->addTemplate('general');
154
155
        $template->assign('langs', SelectItemAdapter::factory(Language::getAvailableLanguages())->getItemsFromArraySelected([$this->configData->getSiteLang()]));
156
        $template->assign('themes', SelectItemAdapter::factory($this->theme->getThemesAvailable())->getItemsFromArraySelected([$this->configData->getSiteTheme()]));
157
        $template->assign('isDemoMode', $this->configData->isDemoEnabled() && !$this->userData->getIsAdminApp());
158
        $template->assign('isDisabled', $this->configData->isDemoEnabled() && !$this->userData->getIsAdminApp() ? 'disabled' : '');
159
160
        $template->assign('users', SelectItemAdapter::factory(UserService::getItemsBasic())->getItemsFromModel());
161
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
162
        $template->assign('userProfiles', SelectItemAdapter::factory(UserProfileService::getItemsBasic())->getItemsFromModel());
163
164
        $template->assign('curlIsAvailable', $this->extensionChecker->checkCurlAvailable());
165
166
        $events = array_merge(LogInterface::EVENTS, $this->configData->getLogEvents());
167
168
        sort($events, SORT_STRING);
169
170
        $template->assign('logEvents', SelectItemAdapter::factory($events)
171
            ->getItemsFromArraySelected($this->configData->getLogEvents(), true)
172
        );
173
174
        return new DataTab(__('General'), $template);
175
    }
176
177
    /**
178
     * @return DataTab
179
     * @throws DependencyException
180
     * @throws NotFoundException
181
     * @throws CheckException
182
     * @throws SPException
183
     */
184
    protected function getAccountConfig()
185
    {
186
        $template = clone $this->view;
187
        $template->setBase('config');
188
        $template->addTemplate('accounts');
189
        $template->assign('gdIsAvailable', $this->extensionChecker->checkGdAvailable());
190
191
        $mimeTypesAvailable = array_map(function ($value) {
192
            return $value['type'];
193
        }, $this->dic->get(MimeTypes::class)->getMimeTypes());
194
195
        $mimeTypes = SelectItemAdapter::factory(
196
            array_merge($mimeTypesAvailable, $this->configData->getFilesAllowedMime())
197
        );
198
199
        $template->assign('mimeTypes', $mimeTypes->getItemsFromArraySelected(
200
            $this->configData->getFilesAllowedMime(),
201
            true)
202
        );
203
204
        return new DataTab(__('Accounts'), $template);
205
    }
206
207
    /**
208
     * @return DataTab
209
     * @throws CheckException
210
     */
211
    protected function getWikiConfig()
212
    {
213
        $template = clone $this->view;
214
        $template->setBase('config');
215
        $template->addTemplate('wiki');
216
217
        $template->assign('curlIsAvailable', $this->extensionChecker->checkCurlAvailable());
218
219
        return new DataTab(__('Wiki'), $template);
220
    }
221
222
    /**
223
     * @return DataTab
224
     * @throws ContainerExceptionInterface
225
     * @throws NotFoundExceptionInterface
226
     * @throws CheckException
227
     */
228
    protected function getLdapConfig()
229
    {
230
        $template = clone $this->view;
231
        $template->setBase('config');
232
        $template->addTemplate('ldap');
233
234
        $template->assign('ldapIsAvailable', $this->extensionChecker->checkIsAvailable('ldap'));
235
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
236
        $template->assign('userProfiles', SelectItemAdapter::factory(UserProfileService::getItemsBasic())->getItemsFromModel());
237
238
        $serverTypes = [
239
            LdapTypeInterface::LDAP_STD => 'Standard',
240
            LdapTypeInterface::LDAP_ADS => 'Active Directory',
241
            LdapTypeInterface::LDAP_AZURE => 'Azure Active Directory',
242
        ];
243
244
        $template->assign('serverTypes', SelectItemAdapter::factory($serverTypes)->getItemsFromArraySelected([$this->configData->getLdapType()]));
245
246
        return new DataTab(__('LDAP'), $template);
247
    }
248
249
    /**
250
     * @return DataTab
251
     * @throws ContainerExceptionInterface
252
     * @throws NotFoundExceptionInterface
253
     */
254
    protected function getMailConfig()
255
    {
256
        $template = clone $this->view;
257
        $template->setBase('config');
258
        $template->addTemplate('mail');
259
260
        $template->assign('mailSecurity', ['SSL', 'TLS']);
261
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
262
        $template->assign('userProfiles', SelectItemAdapter::factory(UserProfileService::getItemsBasic())->getItemsFromModel());
263
264
        $events = array_merge(MailHandler::EVENTS, $this->configData->getMailEvents());
265
266
        sort($events, SORT_STRING);
267
268
        $template->assign('mailEvents', SelectItemAdapter::factory($events)
269
            ->getItemsFromArraySelected($this->configData->getMailEvents(), true)
270
        );
271
272
        return new DataTab(__('Mail'), $template);
273
    }
274
275
    /**
276
     * @return DataTab
277
     * @throws DependencyException
278
     * @throws NotFoundException
279
     * @throws ConstraintException
280
     * @throws QueryException
281
     * @throws NoSuchItemException
282
     * @throws ServiceException
283
     */
284
    protected function getEncryptionConfig()
285
    {
286
        $template = clone $this->view;
287
        $template->setBase('config');
288
        $template->addTemplate('encryption');
289
290
        $numAccounts = $this->dic->get(AccountService::class)->getTotalNumAccounts();
291
        $template->assign('numAccounts', $numAccounts);
292
293
        if ($numAccounts > 150) {
294
            $template->assign('taskId', Task::genTaskId('masterpass'));
295
        }
296
297
        $configService = $this->dic->get(ConfigService::class);
298
299
        $template->assign('lastUpdateMPass', $configService->getByParam('lastupdatempass', 0));
300
301
        $template->assign('tempMasterPassTime', $configService->getByParam(TemporaryMasterPassService::PARAM_TIME, 0));
302
        $template->assign('tempMasterMaxTime', $configService->getByParam(TemporaryMasterPassService::PARAM_MAX_TIME, 0));
303
304
        $tempMasterAttempts = sprintf('%d/%d',
305
            $configService->getByParam(TemporaryMasterPassService::PARAM_ATTEMPTS, 0),
306
            TemporaryMasterPassService::MAX_ATTEMPTS);
307
308
        $template->assign('tempMasterAttempts', $tempMasterAttempts);
309
        $template->assign('tempMasterPass', $this->session->getTemporaryMasterPass());
310
311
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
312
313
        return new DataTab(__('Encryption'), $template);
314
    }
315
316
    /**
317
     * @return DataTab
318
     * @throws CheckException
319
     */
320
    protected function getBackupConfig()
321
    {
322
        $template = clone $this->view;
323
        $template->setBase('config');
324
        $template->addTemplate('backup');
325
        $template->assign('pharIsAvailable', $this->extensionChecker->checkPharAvailable());
326
327
        $template->assign('siteName', AppInfoInterface::APP_NAME);
328
329
        $backupAppFile = new FileHandler(FileBackupService::getAppBackupFilename(BACKUP_PATH, $this->configData->getBackupHash() ?: '', true));
330
        $backupDbFile = new FileHandler(FileBackupService::getDbBackupFilename(BACKUP_PATH, $this->configData->getBackupHash() ?: '', true));
331
        $exportFile = new FileHandler(XmlExportService::getExportFilename(BACKUP_PATH, $this->configData->getExportHash() ?: '', true));
332
333
        try {
334
            $backupAppFile->checkFileExists();
335
            $backupDbFile->checkFileExists();
336
337
            $template->assign('hasBackup', true);
338
            $template->assign('lastBackupTime', date('r', $backupAppFile->getFileTime()));
339
        } catch (FileException $e) {
340
            $template->assign('hasBackup', false);
341
            $template->assign('lastBackupTime', __('There aren\'t any backups available'));
342
        }
343
344
        try {
345
            $exportFile->checkFileExists();
346
347
            $template->assign('hasExport', true);
348
            $template->assign('lastExportTime', date('r', $exportFile->getFileTime()));
349
        } catch (FileException $e) {
350
            $template->assign('hasExport', false);
351
            $template->assign('lastExportTime', __('No export file found'));
352
        }
353
354
        return new DataTab(__('Backup'), $template);
355
    }
356
357
    /**
358
     * @return DataTab
359
     * @throws ContainerExceptionInterface
360
     * @throws NotFoundExceptionInterface
361
     */
362
    protected function getImportConfig()
363
    {
364
        $template = clone $this->view;
365
        $template->setBase('config');
366
        $template->addTemplate('import');
367
368
        $template->assign('userGroups', SelectItemAdapter::factory(
369
            UserGroupService::getItemsBasic())
370
            ->getItemsFromModelSelected([$this->userData->getUserGroupId()])
371
        );
372
        $template->assign('users', SelectItemAdapter::factory(
373
            UserService::getItemsBasic())
374
            ->getItemsFromModelSelected([$this->userData->getId()])
375
        );
376
377
        return new DataTab(__('Import Accounts'), $template);
378
    }
379
380
    /**
381
     * @return DataTab
382
     * @throws DependencyException
383
     * @throws NotFoundException
384
     * @throws NoSuchItemException
385
     * @throws ServiceException
386
     */
387
    protected function getInfo()
388
    {
389
        $template = clone $this->view;
390
        $template->setBase('config');
391
        $template->addTemplate('info');
392
393
        $databaseUtil = $this->dic->get(DatabaseUtil::class);
394
395
        $template->assign('dbInfo', $databaseUtil->getDBinfo());
396
        $template->assign('dbName', $this->configData->getDbName() . '@' . $this->configData->getDbHost());
397
        $template->assign('configBackupDate', date('r', $this->dic->get(ConfigService::class)->getByParam('config_backup_date', 0)));
398
        $template->assign('plugins', $this->dic->get(PluginManager::class)->getLoadedPlugins());
399
        $template->assign('locale', Language::$localeStatus ?: sprintf('%s (%s)', $this->configData->getSiteLang(), __('Not installed')));
400
        $template->assign('securedSession', CryptSessionHandler::$isSecured);
401
        $template->assign('missingExtensions', $this->extensionChecker->getMissing());
402
        $template->assign('downloadRate', round(Util::getMaxDownloadChunk() / 1024 / 1024));
403
404
        $isDemo = $this->configData->isDemoEnabled();
405
406
        $template->assign('downloadConfigBackup', !$isDemo && $this->userData->getIsAdminApp());
407
        $template->assign('downloadLog', !$isDemo && is_readable(LOG_FILE) && $this->userData->getIsAdminApp());
408
409
        return new DataTab(__('Information'), $template);
410
    }
411
412
    /**
413
     * @return TabsHelper
414
     */
415
    public function getTabsHelper()
416
    {
417
        return $this->tabsHelper;
418
    }
419
420
    /**
421
     * @throws AuthException
422
     * @throws DependencyException
423
     * @throws NotFoundException
424
     * @throws SessionTimeout
425
     */
426
    protected function initialize()
427
    {
428
        $this->checkLoggedIn();
429
    }
430
}