Passed
Push — master ( 9002ff...62782a )
by Marcel
04:34
created

ShareMapper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 133
dl 0
loc 280
rs 10
c 0
b 0
f 0
wmc 15

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getReportByToken() 0 14 1
A getAllSharedReports() 0 15 1
A updateSharePassword() 0 9 1
A createShare() 0 31 3
A deleteByUser() 0 8 1
A deleteShareByReport() 0 7 1
A getShares() 0 14 1
A __construct() 0 9 1
A getSharedReceiver() 0 11 1
A updateShareDomain() 0 9 1
A getAllSharedPanoramas() 0 15 1
A deleteShare() 0 11 1
A updateSharePermissions() 0 11 1
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
namespace OCA\Analytics\Db;
10
11
use OCP\DB\Exception;
0 ignored issues
show
Bug introduced by
The type OCP\DB\Exception 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...
12
use OCP\DB\QueryBuilder\IQueryBuilder;
0 ignored issues
show
Bug introduced by
The type OCP\DB\QueryBuilder\IQueryBuilder 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...
13
use OCP\IDBConnection;
0 ignored issues
show
Bug introduced by
The type OCP\IDBConnection 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...
14
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...
15
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...
16
17
class ShareMapper
18
{
19
    /** @var IUserSession */
20
    private $userSession;
21
    /** @var IDBConnection */
22
    private $db;
23
    private $logger;
24
    const TABLE_NAME = 'analytics_share';
25
	const TABLE_NAME_REPORT = 'analytics_report';
26
	const TABLE_NAME_PANORAMA = 'analytics_panorama';
27
28
    public function __construct(
29
        IDBConnection $db,
30
        IUserSession $userSession,
31
        LoggerInterface $logger
32
    )
33
    {
34
        $this->userSession = $userSession;
35
        $this->db = $db;
36
        $this->logger = $logger;
37
    }
38
39
    /**
40
     * get all shared reports by token
41
     * uses for public pages
42
     * @param $token
43
     * @return array
44
     * @throws Exception
45
     */
46
    public function getReportByToken($token)
47
    {
48
        $sql = $this->db->getQueryBuilder();
49
        $sql->from(self::TABLE_NAME_REPORT, 'DS')
50
            ->leftJoin('DS', self::TABLE_NAME, 'SH', $sql->expr()->eq('DS.id', 'SH.report'))
51
            ->select('DS.*')
52
            ->addSelect('SH.permissions')
53
            ->selectAlias('SH.domain', 'domain')
54
            ->selectAlias('SH.password', 'password')
55
            ->where($sql->expr()->eq('SH.token', $sql->createNamedParameter($token)));
56
        $statement = $sql->executeQuery();
57
        $result = $statement->fetch();
58
        $statement->closeCursor();
59
        return $result;
60
    }
61
62
    /**
63
     * get all shared reports
64
     * @return array
65
     * @throws Exception
66
     */
67
    public function getAllSharedReports()
68
    {
69
        $sql = $this->db->getQueryBuilder();
70
        $sql->from(self::TABLE_NAME_REPORT, 'REPORT')
71
            ->rightJoin('REPORT', self::TABLE_NAME, 'SHARE', $sql->expr()->eq('REPORT.id', 'SHARE.report'))
72
            ->select('REPORT.*')
73
            ->selectAlias('SHARE.id', 'shareId')
74
            ->selectAlias('SHARE.type', 'shareType')
75
            ->selectAlias('SHARE.uid_owner', 'shareUid_owner')
76
            ->addSelect('SHARE.permissions')
77
			->where($sql->expr()->isNotNull('SHARE.report'));
78
        $statement = $sql->executeQuery();
79
        $result = $statement->fetchAll();
80
        $statement->closeCursor();
81
        return $result;
82
    }
83
84
	/**
85
	 * get all shared panoramas
86
	 * @return array
87
	 * @throws Exception
88
	 */
89
	public function getAllSharedPanoramas()
90
	{
91
		$sql = $this->db->getQueryBuilder();
92
		$sql->from(self::TABLE_NAME_PANORAMA, 'PANORAMA')
93
			->rightJoin('PANORAMA', self::TABLE_NAME, 'SHARE', $sql->expr()->eq('PANORAMA.id', 'SHARE.panorama'))
94
			->select('PANORAMA.*')
95
			->selectAlias('SHARE.id', 'shareId')
96
			->selectAlias('SHARE.type', 'shareType')
97
			->selectAlias('SHARE.uid_owner', 'shareUid_owner')
98
			->addSelect('SHARE.permissions')
99
			->where($sql->expr()->isNotNull('SHARE.panorama'));
100
		$statement = $sql->executeQuery();
101
		$result = $statement->fetchAll();
102
		$statement->closeCursor();
103
		return $result;
104
	}
105
106
	/**
107
     * Create a new share
108
     * @param $reportId
109
     * @param $type
110
     * @param $uid_owner
111
     * @param $token
112
     * @param $parent
113
     * @return bool
114
     * @throws \OCP\DB\Exception
115
     */
116
    public function createShare($reportId, $type, $uid_owner, $token, $parent = null)
117
    {
118
        $sql = $this->db->getQueryBuilder();
119
        $sql->from(self::TABLE_NAME)
120
            ->Select('id')
121
            ->where($sql->expr()->eq('report', $sql->createNamedParameter($reportId)))
122
            ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter($type)))
123
            ->andWhere($sql->expr()->eq('uid_owner', $sql->createNamedParameter($uid_owner)))
124
            ->andWhere($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())));
125
        $statement = $sql->executeQuery();
126
        $result = $statement->fetchAll();
127
        $statement->closeCursor();
128
129
        if ($result && ($type !== 3)) {
130
            // don´t create double shares
131
            // multiple link shares (3) are possible
132
            return false;
133
        } else {
134
            $sql = $this->db->getQueryBuilder();
135
            $sql->insert(self::TABLE_NAME)
136
                ->values([
137
                    'report' => $sql->createNamedParameter($reportId),
138
                    'type' => $sql->createNamedParameter($type),
139
                    'uid_owner' => $sql->createNamedParameter($uid_owner),
140
                    'uid_initiator' => $sql->createNamedParameter($this->userSession->getUser()->getUID()),
141
                    'token' => $sql->createNamedParameter($token),
142
                    'parent' => $sql->createNamedParameter($parent),
143
                ]);
144
            $sql->executeStatement();
145
        }
146
        return $sql->getLastInsertId();
147
    }
148
149
    /**
150
     * Get all shares of a report
151
     * @param $reportId
152
     * @return array
153
     * @throws Exception
154
     */
155
    public function getShares($reportId)
156
    {
157
        $sql = $this->db->getQueryBuilder();
158
        $sql->from(self::TABLE_NAME)
159
            ->select('id', 'type', 'uid_owner', 'token', 'permissions', 'domain')
160
            ->selectAlias('password', 'pass')
161
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
162
            ->andWhere($sql->expr()->eq('report', $sql->createNamedParameter($reportId)))
163
            ->andWhere($sql->expr()->neq('type', $sql->createNamedParameter(2)))
164
            ->orderBy('id', 'ASC');
165
        $statement = $sql->executeQuery();
166
        $result = $statement->fetchAll();
167
        $statement->closeCursor();
168
        return $result;
169
    }
170
171
    /**
172
     * Get the all receivers of shares of a report
173
     * Used to derive who has to receive activities when a report changes
174
     * @param $reportId
175
     * @return array
176
     * @throws Exception
177
     */
178
    public function getSharedReceiver($reportId)
179
    {
180
        $sql = $this->db->getQueryBuilder();
181
        $sql->from(self::TABLE_NAME)
182
            ->select('uid_owner')
183
            ->where($sql->expr()->eq('report', $sql->createNamedParameter($reportId)))
184
            ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter(0)));
185
        $statement = $sql->executeQuery();
186
        $result = $statement->fetchAll();
187
        $statement->closeCursor();
188
        return $result;
189
    }
190
191
    /**
192
     * Update the password of a share
193
     * @param $shareId
194
     * @param $password
195
     * @return bool
196
     * @throws Exception
197
     */
198
    public function updateSharePassword($shareId, $password)
199
    {
200
        $sql = $this->db->getQueryBuilder();
201
        $sql->update(self::TABLE_NAME)
202
            ->set('password', $sql->createNamedParameter($password))
203
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
204
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId)));
205
        $sql->executeStatement();
206
        return true;
207
    }
208
209
    /**
210
     * Update the password of a share
211
     * @param $shareId
212
     * @param $domain
213
     * @return bool
214
     * @throws Exception
215
     */
216
    public function updateShareDomain($shareId, $domain)
217
    {
218
        $sql = $this->db->getQueryBuilder();
219
        $sql->update(self::TABLE_NAME)
220
            ->set('domain', $sql->createNamedParameter($domain))
221
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
222
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId)));
223
        $sql->executeStatement();
224
        return true;
225
    }
226
227
    /**
228
     * Update the permissions of a share
229
     * @param $shareId
230
     * @param $permissions
231
     * @return bool
232
     * @throws Exception
233
     */
234
    public function updateSharePermissions($shareId, $permissions)
235
    {
236
        // update the share itself
237
        $sql = $this->db->getQueryBuilder();
238
        $sql->update(self::TABLE_NAME)
239
            ->set('permissions', $sql->createNamedParameter($permissions))
240
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
241
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId)));
242
        $sql->executeStatement();
243
244
        return true;
245
    }
246
247
    /**
248
     * Delete an own share (sharee or receiver)
249
     * @param $shareId
250
     * @return bool
251
     * @throws Exception
252
     */
253
    public function deleteShare($shareId)
254
    {
255
        $sql = $this->db->getQueryBuilder();
256
        $sql->delete(self::TABLE_NAME)
257
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($shareId)))
258
            ->andWhere($sql->expr()->orX(
259
                $sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())),
260
                $sql->expr()->eq('uid_owner', $sql->createNamedParameter($this->userSession->getUser()->getUID()))
261
            ));
262
        $sql->executeStatement();
263
        return true;
264
    }
265
266
    /**
267
     * Delete all shares of a report
268
     * Used during report deletion
269
     * @param $reportId
270
     * @return bool
271
     * @throws Exception
272
     */
273
    public function deleteShareByReport($reportId)
274
    {
275
        $sql = $this->db->getQueryBuilder();
276
        $sql->delete(self::TABLE_NAME)
277
            ->where($sql->expr()->eq('report', $sql->createNamedParameter($reportId)));
278
        $sql->executeStatement();
279
        return true;
280
    }
281
282
    /**
283
     * delete all shares when a share-receiving-user is deleted
284
     *
285
     * @param $userId
286
     * @return bool
287
     * @throws Exception
288
     */
289
    public function deleteByUser($userId)
290
    {
291
        $sql = $this->db->getQueryBuilder();
292
        $sql->delete(self::TABLE_NAME)
293
            ->where($sql->expr()->eq('uid_owner', $sql->createNamedParameter($userId)))
294
            ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter(0)));
295
        $sql->executeStatement();
296
        return true;
297
    }
298
299
}