Passed
Push — master ( f3eff3...a1eb93 )
by Marcel
02:40
created

PageController::indexPublicMin()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 29
rs 9.2568
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2021 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Controller;
13
14
use OCA\Analytics\DataSession;
15
use OCA\Analytics\Service\ShareService;
16
use OCP\AppFramework\Controller;
17
use OCP\AppFramework\Http\ContentSecurityPolicy;
18
use OCP\AppFramework\Http\StandaloneTemplateResponse;
19
use OCP\AppFramework\Services\IInitialState;
20
use OCP\AppFramework\Http\RedirectResponse;
21
use OCP\AppFramework\Http\TemplateResponse;
22
use OCP\IConfig;
23
use OCP\IRequest;
24
use OCP\IURLGenerator;
25
use OCP\IUserSession;
26
use Psr\Log\LoggerInterface;
27
28
/**
29
 * Controller class for main page.
30
 */
31
class PageController extends Controller
32
{
33
    /** @var IConfig */
34
    protected $config;
35
    /** @var IUserSession */
36
    private $userSession;
37
    private $logger;
38
    /** @var IURLGenerator */
39
    private $urlGenerator;
40
    /** @var DataSession */
41
    private $DataSession;
42
    /** @var ShareService */
43
    private $ShareService;
44
    /** @var OutputController */
45
    private $outputController;
46
    /** @var IInitialState */
47
    protected $initialState;
48
49
    public function __construct(
50
        string $appName,
51
        IRequest $request,
52
        LoggerInterface $logger,
53
        IURLGenerator $urlGenerator,
54
        ShareService $ShareService,
55
        IUserSession $userSession,
56
        IConfig $config,
57
        DataSession $DataSession,
58
        IInitialState $initialState,
59
        OutputController $outputController
60
    )
61
    {
62
        parent::__construct($appName, $request);
63
        $this->logger = $logger;
64
        $this->urlGenerator = $urlGenerator;
65
        $this->ShareService = $ShareService;
66
        $this->config = $config;
67
        $this->userSession = $userSession;
68
        $this->DataSession = $DataSession;
69
        $this->initialState = $initialState;
70
        $this->outputController = $outputController;
71
    }
72
73
    /**
74
     * @NoAdminRequired
75
     * @NoCSRFRequired
76
     */
77
    public function index()
78
    {
79
        $params = array();
80
        $params['token'] = '';
81
        $user = $this->userSession->getUser();
82
83
        $this->initialState->provideInitialState(
84
            'wizard',
85
            $this->config->getUserValue($user->getUID(), 'analytics', 'wizzard', 0)
86
        );
87
88
        return new TemplateResponse($this->appName, 'main', $params);
89
90
        //$response = new TemplateResponse($this->appName, 'main', $params);
91
        //$csp = new ContentSecurityPolicy();
92
        //$csp->addAllowedScriptDomain('*')
93
        //    ->addAllowedConnectDomain('*')
94
        //    ->addAllowedStyleDomain('*')
95
        //    ->addAllowedFontDomain('*')
96
        //    ->allowEvalScript(true);
97
        //$response->setContentSecurityPolicy($csp);
98
        //return $response;
99
    }
100
101
    /**
102
     * @NoAdminRequired
103
     * @NoCSRFRequired
104
     */
105
    public function advanced()
106
    {
107
        return new TemplateResponse($this->appName, 'main_advanced');
108
    }
109
110
    /**
111
     * @PublicPage
112
     * @NoCSRFRequired
113
     * @UseSession
114
     *
115
     * @param string $token
116
     * @param string $password
117
     * @return RedirectResponse|TemplateResponse
118
     */
119
    public function authenticatePassword(string $token, string $password = '')
120
    {
121
        return $this->indexPublic($token, $password);
122
    }
123
124
    /**
125
     * @PublicPage
126
     * @UseSession
127
     * @NoCSRFRequired
128
     * @param $token
129
     * @param string $password
130
     * @return TemplateResponse|RedirectResponse
131
     */
132
    public function indexPublic($token, string $password = '')
133
    {
134
        $share = $this->ShareService->getReportByToken($token);
135
136
        if (empty($share)) {
137
            // Dataset not shared or wrong token
138
            return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', [
139
                'redirect_url' => $this->urlGenerator->linkToRoute($this->appName . '.page.index', ['token' => $token]),
140
            ]));
141
        } else {
142
            if ($share['password'] !== null) {
143
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
144
                $passwordVerification = $this->ShareService->verifyPassword($password, $share['password']);
145
                if ($passwordVerification === true) {
146
                    $this->DataSession->setPasswordForShare($token, $password);
147
                } else {
148
                    $this->DataSession->removePasswordForShare($token);
149
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
150
                }
151
            }
152
            $params = array();
153
            $params['token'] = $token;
154
            return new TemplateResponse($this->appName, 'public', $params);
155
        }
156
    }
157
158
    /**
159
     * @PublicPage
160
     * @UseSession
161
     * @NoCSRFRequired
162
     * @param $token
163
     * @param string $password
164
     * @return TemplateResponse|RedirectResponse
165
     */
166
    public function indexPublicMin($token, string $password = '')
167
    {
168
        $share = $this->ShareService->getReportByToken($token);
169
170
        if (empty($share)) {
171
            // Dataset not shared or wrong token
172
            return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', [
173
                'redirect_url' => $this->urlGenerator->linkToRoute($this->appName . '.page.index', ['token' => $token]),
174
            ]));
175
        } else {
176
            if ($share['password'] !== null) {
177
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
178
                $passwordVerification = $this->ShareService->verifyPassword($password, $share['password']);
179
                if ($passwordVerification === true) {
180
                    $this->DataSession->setPasswordForShare($token, $password);
181
                } else {
182
                    $this->DataSession->removePasswordForShare($token);
183
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
184
                }
185
            }
186
            $params = array();
187
            $params['token'] = $token;
188
            $params['data'] = $this->outputController->getData($share);
189
            $params['baseurl'] = str_replace('/img/app.svg', '', $this->urlGenerator->imagePath('analytics', 'app.svg'));
190
            $response = new StandaloneTemplateResponse($this->appName, 'publicMin', $params, '');
191
            $csp = new ContentSecurityPolicy();
192
            $csp->addAllowedScriptDomain('*');
193
            $response->setContentSecurityPolicy($csp);
194
            return $response;
195
196
        }
197
    }
198
199
}