Passed
Push — 3.0 ( e37bc1...f6f9cd )
by Rubén
04:03
created

ConfigDokuWikiController::saveAction()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 5
nop 0
dl 0
loc 39
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author nuxsmin
6
 * @link https://syspass.org
7
 * @copyright 2012-2018, 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 SP\Core\Acl\Acl;
28
use SP\Core\Acl\UnauthorizedPageException;
29
use SP\Core\Events\Event;
30
use SP\Core\Events\EventMessage;
31
use SP\Http\JsonResponse;
32
use SP\Modules\Web\Controllers\Traits\ConfigTrait;
33
34
/**
35
 * Class ConfigDokuWikiController
36
 *
37
 * @package SP\Modules\Web\Controllers
38
 */
39
final class ConfigDokuWikiController extends SimpleControllerBase
40
{
41
    use ConfigTrait;
42
43
    /**
44
     * saveAction
45
     *
46
     * @throws \SP\Core\Exceptions\SPException
47
     */
48
    public function saveAction()
49
    {
50
        $this->checkSecurityToken($this->previousSk, $this->request);
51
52
        $eventMessage = EventMessage::factory();
53
        $configData = $this->config->getConfigData();
54
55
        // DokuWiki
56
        $dokuWikiEnabled = $this->request->analyzeBool('dokuwiki_enabled', false);
57
        $dokuWikiUrl = $this->request->analyzeString('dokuwiki_url');
58
        $dokuWikiUrlBase = $this->request->analyzeString('dokuwiki_urlbase');
59
        $dokuWikiUser = $this->request->analyzeString('dokuwiki_user');
60
        $dokuWikiPass = $this->request->analyzeEncrypted('dokuwiki_pass');
61
        $dokuWikiNamespace = $this->request->analyzeString('dokuwiki_namespace');
62
63
        // Valores para la conexión a la API de DokuWiki
64
        if ($dokuWikiEnabled && (!$dokuWikiUrl || !$dokuWikiUrlBase)) {
65
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Missing DokuWiki parameters'));
66
        }
67
68
        if ($dokuWikiEnabled) {
69
            $configData->setDokuwikiEnabled(true);
70
            $configData->setDokuwikiUrl($dokuWikiUrl);
71
            $configData->setDokuwikiUrlBase(trim($dokuWikiUrlBase, '/'));
72
            $configData->setDokuwikiUser($dokuWikiUser);
73
            $configData->setDokuwikiPass($dokuWikiPass);
74
            $configData->setDokuwikiNamespace($dokuWikiNamespace);
75
76
            if ($configData->isDokuwikiEnabled() === false) {
77
                $eventMessage->addDescription(__u('DokuWiki enabled'));
78
            }
79
        } elseif ($dokuWikiEnabled === false && $configData->isDokuwikiEnabled()) {
80
            $configData->setDokuwikiEnabled(false);
81
82
            $eventMessage->addDescription(__u('DokuWiki disabled'));
83
        }
84
85
        return $this->saveConfig($configData, $this->config, function () use ($eventMessage) {
86
            $this->eventDispatcher->notifyEvent('save.config.dokuwiki', new Event($this, $eventMessage));
87
        });
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    protected function initialize()
94
    {
95
        try {
96
            $this->checks();
97
            $this->checkAccess(Acl::CONFIG_WIKI);
98
        } catch (UnauthorizedPageException $e) {
99
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
100
101
            return $this->returnJsonResponseException($e);
102
        }
103
104
        return true;
105
    }
106
}