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
|
|||
12 | use OCP\DB\QueryBuilder\IQueryBuilder; |
||
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
13 | use OCP\IDBConnection; |
||
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
14 | use OCP\IUserSession; |
||
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
15 | use Psr\Log\LoggerInterface; |
||
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
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, 'SHARE', $sql->expr()->eq('DS.id', 'SHARE.item_source')) |
||
51 | ->select('DS.*') |
||
52 | ->addSelect('SHARE.permissions') |
||
53 | ->selectAlias('SHARE.domain', 'domain') |
||
54 | ->selectAlias('SHARE.password', 'password') |
||
55 | ->where($sql->expr()->eq('SHARE.token', $sql->createNamedParameter($token))) |
||
56 | ->andWhere($sql->expr()->eq('SHARE.item_type', $sql->createNamedParameter('report'))); |
||
57 | $statement = $sql->executeQuery(); |
||
58 | $result = $statement->fetch(); |
||
59 | $statement->closeCursor(); |
||
60 | return $result; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * get all shared reports |
||
65 | * @return array |
||
66 | * @throws Exception |
||
67 | */ |
||
68 | public function getAllSharedReports() |
||
69 | { |
||
70 | $sql = $this->db->getQueryBuilder(); |
||
71 | $sql->from(self::TABLE_NAME_REPORT, 'REPORT') |
||
72 | ->rightJoin('REPORT', self::TABLE_NAME, 'SHARE', $sql->expr()->eq('REPORT.id', 'SHARE.item_source')) |
||
73 | ->select('REPORT.*') |
||
74 | ->selectAlias('SHARE.id', 'shareId') |
||
75 | ->selectAlias('SHARE.type', 'shareType') |
||
76 | ->selectAlias('SHARE.uid_owner', 'shareUid_owner') |
||
77 | ->addSelect('SHARE.permissions') |
||
78 | ->where($sql->expr()->eq('SHARE.item_type', $sql->createNamedParameter('report'))); |
||
79 | $statement = $sql->executeQuery(); |
||
80 | $result = $statement->fetchAll(); |
||
81 | $statement->closeCursor(); |
||
82 | return $result; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * get all shared panoramas |
||
87 | * @return array |
||
88 | * @throws Exception |
||
89 | */ |
||
90 | public function getAllSharedPanoramas() |
||
91 | { |
||
92 | $sql = $this->db->getQueryBuilder(); |
||
93 | $sql->from(self::TABLE_NAME_PANORAMA, 'PANORAMA') |
||
94 | ->rightJoin('PANORAMA', self::TABLE_NAME, 'SHARE', $sql->expr()->eq('PANORAMA.id', 'SHARE.item_source')) |
||
95 | ->select('PANORAMA.*') |
||
96 | ->selectAlias('SHARE.id', 'shareId') |
||
97 | ->selectAlias('SHARE.type', 'shareType') |
||
98 | ->selectAlias('SHARE.uid_owner', 'shareUid_owner') |
||
99 | ->addSelect('SHARE.permissions') |
||
100 | ->where($sql->expr()->eq('SHARE.item_type', $sql->createNamedParameter('panorama'))); |
||
101 | $statement = $sql->executeQuery(); |
||
102 | $result = $statement->fetchAll(); |
||
103 | $statement->closeCursor(); |
||
104 | return $result; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Create a new share |
||
109 | * @param $item_type |
||
110 | * @param $item_source |
||
111 | * @param $type |
||
112 | * @param $uid_owner |
||
113 | * @param $token |
||
114 | * @param null $parent |
||
0 ignored issues
–
show
|
|||
115 | * @return bool |
||
116 | * @throws Exception |
||
117 | */ |
||
118 | public function createShare($item_type, $item_source, $type, $uid_owner, $token, $parent = null) |
||
119 | { |
||
120 | $sql = $this->db->getQueryBuilder(); |
||
121 | $sql->from(self::TABLE_NAME) |
||
122 | ->Select('id') |
||
123 | ->where($sql->expr()->eq('item_type', $sql->createNamedParameter($item_type))) |
||
124 | ->andWhere($sql->expr()->eq('item_source', $sql->createNamedParameter($item_source))) |
||
125 | ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter($type))) |
||
126 | ->andWhere($sql->expr()->eq('uid_owner', $sql->createNamedParameter($uid_owner))) |
||
127 | ->andWhere($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID()))); |
||
128 | $statement = $sql->executeQuery(); |
||
129 | $result = $statement->fetchAll(); |
||
130 | $statement->closeCursor(); |
||
131 | |||
132 | if ($result && ($type !== 3)) { |
||
133 | // don´t create double shares |
||
134 | // multiple link shares (3) are possible |
||
135 | return false; |
||
136 | } else { |
||
137 | $sql = $this->db->getQueryBuilder(); |
||
138 | $sql->insert(self::TABLE_NAME) |
||
139 | ->values([ |
||
140 | 'item_type' => $sql->createNamedParameter($item_type), |
||
141 | 'item_source' => $sql->createNamedParameter($item_source), |
||
142 | 'type' => $sql->createNamedParameter($type), |
||
143 | 'uid_owner' => $sql->createNamedParameter($uid_owner), |
||
144 | 'uid_initiator' => $sql->createNamedParameter($this->userSession->getUser()->getUID()), |
||
145 | 'token' => $sql->createNamedParameter($token), |
||
146 | 'parent' => $sql->createNamedParameter($parent), |
||
147 | ]); |
||
148 | $sql->executeStatement(); |
||
149 | } |
||
150 | return $sql->getLastInsertId(); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Get all shares of a report |
||
155 | * @param $item_type |
||
156 | * @param $item_source |
||
157 | * @return array |
||
158 | * @throws Exception |
||
159 | */ |
||
160 | public function getShares($item_type, $item_source) |
||
161 | { |
||
162 | $sql = $this->db->getQueryBuilder(); |
||
163 | $sql->from(self::TABLE_NAME) |
||
164 | ->select('id', 'type', 'uid_owner', 'token', 'permissions', 'domain') |
||
165 | ->selectAlias('password', 'pass') |
||
166 | ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID()))) |
||
167 | ->andWhere($sql->expr()->eq('item_type', $sql->createNamedParameter($item_type))) |
||
168 | ->andWhere($sql->expr()->eq('item_source', $sql->createNamedParameter($item_source))) |
||
169 | ->andWhere($sql->expr()->neq('type', $sql->createNamedParameter(2))) |
||
170 | ->orderBy('id', 'ASC'); |
||
171 | $statement = $sql->executeQuery(); |
||
172 | $result = $statement->fetchAll(); |
||
173 | $statement->closeCursor(); |
||
174 | return $result; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get the all receivers of shares of a report |
||
179 | * Used to derive who has to receive activities when a report changes |
||
180 | * @param $reportId |
||
181 | * @return array |
||
182 | * @throws Exception |
||
183 | */ |
||
184 | public function getSharedReceiver($item_type, $item_source) |
||
185 | { |
||
186 | $sql = $this->db->getQueryBuilder(); |
||
187 | $sql->from(self::TABLE_NAME) |
||
188 | ->select('uid_owner') |
||
189 | ->where($sql->expr()->eq('item_type', $sql->createNamedParameter($item_type))) |
||
190 | ->andWhere($sql->expr()->eq('item_source', $sql->createNamedParameter($item_source))) |
||
191 | ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter(0))); |
||
192 | $statement = $sql->executeQuery(); |
||
193 | $result = $statement->fetchAll(); |
||
194 | $statement->closeCursor(); |
||
195 | return $result; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Update the password of a share |
||
200 | * @param $shareId |
||
201 | * @param $password |
||
202 | * @return bool |
||
203 | * @throws Exception |
||
204 | */ |
||
205 | public function updateSharePassword($shareId, $password) |
||
206 | { |
||
207 | $sql = $this->db->getQueryBuilder(); |
||
208 | $sql->update(self::TABLE_NAME) |
||
209 | ->set('password', $sql->createNamedParameter($password)) |
||
210 | ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID()))) |
||
211 | ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId))); |
||
212 | $sql->executeStatement(); |
||
213 | return true; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Update the domain of a share |
||
218 | * used for injecting charts into external sites |
||
219 | * @param $shareId |
||
220 | * @param $domain |
||
221 | * @return bool |
||
222 | * @throws Exception |
||
223 | */ |
||
224 | public function updateShareDomain($shareId, $domain) |
||
225 | { |
||
226 | $sql = $this->db->getQueryBuilder(); |
||
227 | $sql->update(self::TABLE_NAME) |
||
228 | ->set('domain', $sql->createNamedParameter($domain)) |
||
229 | ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID()))) |
||
230 | ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId))); |
||
231 | $sql->executeStatement(); |
||
232 | return true; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Update the permissions of a share |
||
237 | * @param $shareId |
||
238 | * @param $permissions |
||
239 | * @return bool |
||
240 | * @throws Exception |
||
241 | */ |
||
242 | public function updateSharePermissions($shareId, $permissions) |
||
243 | { |
||
244 | // update the share itself |
||
245 | $sql = $this->db->getQueryBuilder(); |
||
246 | $sql->update(self::TABLE_NAME) |
||
247 | ->set('permissions', $sql->createNamedParameter($permissions)) |
||
248 | ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID()))) |
||
249 | ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId))); |
||
250 | $sql->executeStatement(); |
||
251 | |||
252 | return true; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Delete an own share (sharee or receiver) |
||
257 | * @param $shareId |
||
258 | * @return bool |
||
259 | * @throws Exception |
||
260 | */ |
||
261 | public function deleteShare($shareId) |
||
262 | { |
||
263 | $sql = $this->db->getQueryBuilder(); |
||
264 | $sql->delete(self::TABLE_NAME) |
||
265 | ->where($sql->expr()->eq('id', $sql->createNamedParameter($shareId))) |
||
266 | ->andWhere($sql->expr()->orX( |
||
267 | $sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())), |
||
268 | $sql->expr()->eq('uid_owner', $sql->createNamedParameter($this->userSession->getUser()->getUID())) |
||
269 | )); |
||
270 | $sql->executeStatement(); |
||
271 | return true; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Delete all shares of a report |
||
276 | * Used during report deletion |
||
277 | * @param $reportId |
||
278 | * @return bool |
||
279 | * @throws Exception |
||
280 | */ |
||
281 | public function deleteSharesByItem($item_type, $item_source) |
||
282 | { |
||
283 | $sql = $this->db->getQueryBuilder(); |
||
284 | $sql->delete(self::TABLE_NAME) |
||
285 | ->where($sql->expr()->eq('item_type', $sql->createNamedParameter($item_type))) |
||
286 | ->andWhere($sql->expr()->eq('item_source', $sql->createNamedParameter($item_source))); |
||
287 | $sql->executeStatement(); |
||
288 | return true; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * delete all shares when a share-receiving-user is deleted |
||
293 | * |
||
294 | * @param $userId |
||
295 | * @return bool |
||
296 | * @throws Exception |
||
297 | */ |
||
298 | public function deleteByUser($userId) |
||
299 | { |
||
300 | $sql = $this->db->getQueryBuilder(); |
||
301 | $sql->delete(self::TABLE_NAME) |
||
302 | ->where($sql->expr()->eq('uid_owner', $sql->createNamedParameter($userId))) |
||
303 | ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter(0))); |
||
304 | $sql->executeStatement(); |
||
305 | return true; |
||
306 | } |
||
307 | |||
308 | } |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths