Passed
Push — master ( 617477...b5fea5 )
by Marcel
04:20 queued 14s
created

PageController::story()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 2019-2022 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;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use OCP\AppFramework\Http\ContentSecurityPolicy;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\ContentSecurityPolicy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\AppFramework\Http\StandaloneTemplateResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\StandaloneTemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\Te...\PublicTemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\AppFramework\Services\IInitialState;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Services\IInitialState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\AppFramework\Http\RedirectResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use OCP\AppFramework\Http\TemplateResponse;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Http\TemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use OCP\IRequest;
0 ignored issues
show
Bug introduced by
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use OCP\IUserSession;
0 ignored issues
show
Bug introduced by
The type OCP\IUserSession was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
29
/**
30
 * Controller class for main page.
31
 */
32
class PageController extends Controller
33
{
34
    /** @var IConfig */
35
    protected $config;
36
    /** @var IUserSession */
37
    private $userSession;
38
    private $logger;
39
    /** @var IURLGenerator */
40
    private $urlGenerator;
41
    /** @var DataSession */
42
    private $DataSession;
43
    /** @var ShareService */
44
    private $ShareService;
45
    /** @var OutputController */
46
    private $outputController;
47
    /** @var IInitialState */
48
    protected $initialState;
49
50
    public function __construct(
51
        string $appName,
52
        IRequest $request,
53
        LoggerInterface $logger,
54
        IURLGenerator $urlGenerator,
55
        ShareService $ShareService,
56
        IUserSession $userSession,
57
        IConfig $config,
58
        DataSession $DataSession,
59
        IInitialState $initialState,
60
        OutputController $outputController
61
    )
62
    {
63
        parent::__construct($appName, $request);
64
        $this->logger = $logger;
65
        $this->urlGenerator = $urlGenerator;
66
        $this->ShareService = $ShareService;
67
        $this->config = $config;
68
        $this->userSession = $userSession;
69
        $this->DataSession = $DataSession;
70
        $this->initialState = $initialState;
71
        $this->outputController = $outputController;
72
    }
73
74
    /**
75
     * @NoAdminRequired
76
     * @NoCSRFRequired
77
     */
78
    public function index()
79
    {
80
        $params = array();
81
        $params['token'] = '';
82
        $user = $this->userSession->getUser();
83
84
        $this->initialState->provideInitialState(
85
            'wizard',
86
            $this->config->getUserValue($user->getUID(), 'analytics', 'wizzard', 0)
87
        );
88
89
        try {
90
            $translationAvailable = \OCP\Server::get(\OCP\Translation\ITranslationManager::class)->hasProviders();
0 ignored issues
show
Bug introduced by
The type OCP\Translation\ITranslationManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type OCP\Server was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
91
            $translationLanguages = \OCP\Server::get(\OCP\Translation\ITranslationManager::class)->getLanguages();
92
        } catch (\Exception $e) {
93
            $translationAvailable = false;
94
            $translationLanguages = false;
95
        }
96
97
        $this->initialState->provideInitialState(
98
            'translationAvailable',
99
            $translationAvailable
100
        );
101
        $this->initialState->provideInitialState(
102
            'translationLanguages',
103
            $translationLanguages
104
        );
105
106
        return new TemplateResponse($this->appName, 'main', $params);
107
    }
108
109
    /**
110
     * @NoAdminRequired
111
     * @NoCSRFRequired
112
     */
113
    public function advanced()
114
    {
115
        return new TemplateResponse($this->appName, 'main_advanced');
116
    }
117
118
    /**
119
     * @NoAdminRequired
120
     * @NoCSRFRequired
121
     */
122
    public function story()
123
    {
124
        return new TemplateResponse($this->appName, 'main_story');
125
    }
126
127
    /**
128
     * @PublicPage
129
     * @NoCSRFRequired
130
     * @UseSession
131
     *
132
     * @param string $token
133
     * @param string $password
134
     * @return RedirectResponse|TemplateResponse
135
     */
136
    public function authenticatePassword(string $token, string $password = '')
137
    {
138
        return $this->indexPublic($token, $password);
139
    }
140
141
    /**
142
     * @PublicPage
143
     * @UseSession
144
     * @NoCSRFRequired
145
     * @param $token
146
     * @param string $password
147
     * @return TemplateResponse|RedirectResponse
148
     */
149
    public function indexPublic($token, string $password = '')
150
    {
151
        $share = $this->ShareService->getReportByToken($token);
152
153
        if (empty($share)) {
154
            // Dataset not shared or wrong token
155
            return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', [
156
                'redirect_url' => $this->urlGenerator->linkToRoute($this->appName . '.page.index', ['token' => $token]),
157
            ]));
158
        } else {
159
            if ($share['password'] !== null) {
160
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
161
                $passwordVerification = $this->ShareService->verifyPassword($password, $share['password']);
162
                if ($passwordVerification === true) {
163
                    $this->DataSession->setPasswordForShare($token, $password);
164
                } else {
165
                    $this->DataSession->removePasswordForShare($token);
166
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
167
                }
168
            }
169
            $params = array();
170
            $params['token'] = $token;
171
            $response = new PublicTemplateResponse($this->appName, 'public', $params);
172
            $response->setHeaderTitle('Nextcloud Analytics');
173
            $response->setFooterVisible(false);
174
            return $response;
175
        }
176
    }
177
178
    /**
179
     * @PublicPage
180
     * @UseSession
181
     * @NoCSRFRequired
182
     * @param $token
183
     * @param string $password
184
     * @return TemplateResponse|RedirectResponse
185
     */
186
    public function indexPublicMin($token, string $password = '')
187
    {
188
        $share = $this->ShareService->getReportByToken($token);
189
190
        if (empty($share)) {
191
            // Dataset not shared or wrong token
192
            return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', [
193
                'redirect_url' => $this->urlGenerator->linkToRoute($this->appName . '.page.index', ['token' => $token]),
194
            ]));
195
        } else {
196
            if ($share['password'] !== null) {
197
                $password = $password !== '' ? $password : (string)$this->DataSession->getPasswordForShare($token);
198
                $passwordVerification = $this->ShareService->verifyPassword($password, $share['password']);
199
                if ($passwordVerification === true) {
200
                    $this->DataSession->setPasswordForShare($token, $password);
201
                } else {
202
                    $this->DataSession->removePasswordForShare($token);
203
                    return new TemplateResponse($this->appName, 'authenticate', ['wrongpw' => $password !== '',], 'guest');
204
                }
205
            }
206
            $params = array();
207
            $params['data'] = $this->outputController->getData($share);
208
            $params['baseurl'] = str_replace('/img/app.svg', '', $this->urlGenerator->imagePath('analytics', 'app.svg'));
209
            $response = new StandaloneTemplateResponse($this->appName, 'publicMin', $params, '');
210
            $csp = new ContentSecurityPolicy();
211
            $csp->addAllowedScriptDomain('*');
212
            $csp->addAllowedFrameAncestorDomain($share['domain']);
213
            $response->setContentSecurityPolicy($csp);
214
            return $response;
215
        }
216
    }
217
}