Passed
Push — master ( ce974b...f4e246 )
by Marcel
02:50
created

OutputController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
dl 0
loc 154
rs 10
c 1
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A evaluateCanFilter() 0 9 4
A getData() 0 34 3
A readPublic() 0 18 4
A read() 0 11 3
A __construct() 0 24 1
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 2020 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\DataResponse;
18
use OCP\AppFramework\Http\NotFoundResponse;
19
use OCP\Files\IRootFolder;
20
use OCP\Files\NotFoundException;
21
use OCP\ILogger;
22
use OCP\IRequest;
23
24
class OutputController extends Controller
25
{
26
    private $logger;
27
    private $DatasetController;
28
    private $rootFolder;
29
    private $userId;
30
    private $DataSession;
31
    private $ShareService;
32
    private $DatasourceController;
33
    private $StorageController;
34
    private $ThresholdController;
35
36
    public function __construct(
37
        string $AppName,
38
        IRequest $request,
39
        $userId,
40
        ILogger $logger,
41
        IRootFolder $rootFolder,
42
        DatasetController $DatasetController,
43
        ShareService $ShareService,
44
        DataSession $DataSession,
45
        DatasourceController $DatasourceController,
46
        StorageController $StorageController,
47
        ThresholdController $ThresholdController
48
    )
49
    {
50
        parent::__construct($AppName, $request);
51
        $this->userId = $userId;
52
        $this->logger = $logger;
53
        $this->DatasetController = $DatasetController;
54
        $this->rootFolder = $rootFolder;
55
        $this->DataSession = $DataSession;
56
        $this->ShareService = $ShareService;
57
        $this->DatasourceController = $DatasourceController;
58
        $this->StorageController = $StorageController;
59
        $this->ThresholdController = $ThresholdController;
60
    }
61
62
    /**
63
     * get the data when requested from internal page
64
     *
65
     * @NoAdminRequired
66
     * @param int $datasetId
67
     * @param $filteroptions
68
     * @return DataResponse|NotFoundResponse
69
     * @throws NotFoundException
70
     */
71
    public function read(int $datasetId, $filteroptions)
72
    {
73
        $datasetMetadata = $this->DatasetController->getOwnDataset($datasetId);
74
        if (empty($datasetMetadata)) $datasetMetadata = $this->ShareService->getSharedDataset($datasetId);
75
76
        if (!empty($datasetMetadata)) {
77
            $datasetMetadata = $this->evaluateCanFilter($datasetMetadata, $filteroptions);
78
            $result = $this->getData($datasetMetadata);
79
            return new DataResponse($result);
80
        } else {
81
            return new NotFoundResponse();
82
        }
83
    }
84
85
    /**
86
     * Get the data from backend;
87
     * pre-evaluation of valid datasetId within read & readPublic is trusted here
88
     *
89
     * @NoAdminRequired
90
     * @param $datasetMetadata
91
     * @return array|NotFoundException
92
     * @throws NotFoundException
93
     */
94
    private function getData($datasetMetadata)
95
    {
96
        $datasourceId = (int)$datasetMetadata['type'];
97
98
        if ($datasourceId === DatasourceController::DATASET_TYPE_INTERNAL_DB) {
99
            $result = $this->StorageController->read($datasetMetadata);
100
        } else {
101
            $option = array();
102
            // before 3.1.0, the options were in another format. as of 3.1.0 the standard option array is used
103
            if ($datasetMetadata['link'][0] !== '{') {
104
                $option['link'] = $datasetMetadata['link'];
105
            } else {
106
                $option = json_decode($datasetMetadata['link'], true);
107
            }
108
            $option['user_id'] = $datasetMetadata['user_id'];
109
110
            $result = $this->DatasourceController->read($datasourceId, $option);
111
            unset($result['error']);
112
        }
113
114
        $result['thresholds'] = $this->ThresholdController->read($datasetMetadata['id']);
115
116
        unset($datasetMetadata['parent']
117
            , $datasetMetadata['user_id']
118
            , $datasetMetadata['link']
119
            , $datasetMetadata['dimension1']
120
            , $datasetMetadata['dimension2']
121
            , $datasetMetadata['dimension3']
122
            , $datasetMetadata['value']
123
            , $datasetMetadata['password']
124
        );
125
        $result['options'] = $datasetMetadata;
126
127
        return $result;
128
    }
129
130
    /**
131
     * get the data when requested from public page
132
     *
133
     * @NoAdminRequired
134
     * @PublicPage
135
     * @UseSession
136
     * @param $token
137
     * @param $filteroptions
138
     * @return DataResponse|NotFoundResponse
139
     * @throws NotFoundException
140
     */
141
    public function readPublic($token, $filteroptions)
142
    {
143
        $share = $this->ShareService->getDatasetByToken($token);
144
        if (empty($share)) {
145
            // Dataset not shared or wrong token
146
            return new NotFoundResponse();
147
        } else {
148
            if ($share['password'] !== null) {
149
                $password = $this->DataSession->getPasswordForShare($token);
150
                $passwordVerification = $this->ShareService->verifyPassword($password, $share['password']);
151
                if ($passwordVerification === false) {
152
                    return new NotFoundResponse();
153
                }
154
            }
155
            $share = $this->evaluateCanFilter($share, $filteroptions);
156
            //TODO: umstellung auf Options
157
            $result = $this->getData($share);
158
            return new DataResponse($result);
159
        }
160
    }
161
162
    /**
163
     * evaluate if the user did filter in the report and if he is allowed to filter (shared reports)
164
     *
165
     * @param $metadata
166
     * @param $filteroptions
167
     * @return mixed
168
     */
169
    private function evaluateCanFilter($metadata, $filteroptions)
170
    {
171
        if ($filteroptions and $filteroptions !== '{}' and $metadata['permissions'] !== \OCP\Constants::PERMISSION_READ) {
172
            // send current user filteroptions to the datarequest
173
            // only if the shared report has update-permissions
174
            // if nothing is changed by the user, the filter which is stored for the report, will be used
175
            $metadata['filteroptions'] = $filteroptions;
176
        }
177
        return $metadata;
178
    }
179
}