Passed
Push — master ( 912bd1...0b1a33 )
by Marcel
06:53
created

ShareMapper::deleteShareByParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
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\Db;
13
14
use OCP\DB\QueryBuilder\IQueryBuilder;
15
use OCP\IDBConnection;
16
use OCP\IL10N;
17
use OCP\IUserSession;
18
use Psr\Log\LoggerInterface;
19
20
class ShareMapper
21
{
22
    /** @var IUserSession */
23
    private $userSession;
24
    private $l10n;
25
    private $db;
26
    private $logger;
27
    const TABLE_NAME = 'analytics_share';
28
    const TABLE_NAME_REPORT = 'analytics_report';
29
30
    public function __construct(
31
        IL10N $l10n,
32
        IDBConnection $db,
33
        IUserSession $userSession,
34
        LoggerInterface $logger
35
    )
36
    {
37
        $this->userSession = $userSession;
38
        $this->l10n = $l10n;
39
        $this->db = $db;
40
        $this->logger = $logger;
41
        self::TABLE_NAME;
42
        self::TABLE_NAME_REPORT;
43
    }
44
45
    /**
46
     * get all shared reports by token
47
     * uses for public pages
48
     * @param $token
49
     * @return array
50
     */
51
    public function getReportByToken($token)
52
    {
53
        $sql = $this->db->getQueryBuilder();
54
        $sql->from(self::TABLE_NAME_REPORT, 'DS')
55
            ->leftJoin('DS', self::TABLE_NAME, 'SH', $sql->expr()->eq('DS.id', 'SH.repot'))
56
            ->select('DS.*')
57
            ->addSelect('SH.permissions')
58
            ->selectAlias('SH.password', 'password')
59
            ->where($sql->expr()->eq('SH.token', $sql->createNamedParameter($token)));
60
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

60
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
61
        $result = $statement->fetch();
62
        $statement->closeCursor();
63
        return $result;
64
    }
65
66
    /**
67
     * get shared reports for current user
68
     * @return array
69
     */
70
    public function getSharedReports()
71
    {
72
        $sql = $this->db->getQueryBuilder();
73
        $sql->from(self::TABLE_NAME_REPORT, 'DS')
74
            ->leftJoin('DS', self::TABLE_NAME, 'SH', $sql->expr()->eq('DS.id', 'SH.report'))
75
            ->select('DS.id', 'DS.name')
76
            ->selectAlias('SH.id', 'shareId')
77
            ->where($sql->expr()->eq('SH.uid_owner', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
78
            ->andWhere($sql->expr()->neq('SH.type', $sql->createNamedParameter(1))); // don´t find groups with the same name
79
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

79
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
80
        $result = $statement->fetchAll();
81
        $statement->closeCursor();
82
        return $result;
83
    }
84
85
    /**
86
     * get single shared report for current user
87
     * @param $id
88
     * @return array
89
     */
90
    public function getSharedReport($id)
91
    {
92
        $sql = $this->db->getQueryBuilder();
93
        $sql->from(self::TABLE_NAME_REPORT, 'DS')
94
            ->leftJoin('DS', self::TABLE_NAME, 'SH', $sql->expr()->eq('DS.id', 'SH.report'))
95
            ->select('DS.*')
96
            ->addSelect('SH.permissions')
97
            ->selectAlias($sql->createNamedParameter(true, IQueryBuilder::PARAM_BOOL), 'isShare')
98
            ->where($sql->expr()->eq('SH.uid_owner', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
99
            ->andWhere($sql->expr()->eq('DS.id', $sql->createNamedParameter($id)));
100
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
101
        $result = $statement->fetch();
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->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

125
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
126
        $result = $statement->fetchAll();
127
        $statement->closeCursor();
128
129
        if ($result && ($type !== 3)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array<mixed,mixed> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

144
            /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
145
        }
146
        return $sql->getLastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sql->getLastInsertId() returns the type integer which is incompatible with the documented return type boolean.
Loading history...
147
    }
148
149
    /**
150
     * Get single shares metadata
151
     * @param $shareId
152
     * @return array
153
     */
154
    public function getShare($shareId)
155
    {
156
        $sql = $this->db->getQueryBuilder();
157
        $sql->from(self::TABLE_NAME)
158
            ->select('id', 'type', 'parent')
159
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($shareId)));
160
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

160
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
161
        $result = $statement->fetch();
162
        $statement->closeCursor();
163
        return $result;
164
    }
165
166
    /**
167
     * Get all shares of a report
168
     * @param $reportId
169
     * @return array
170
     */
171
    public function getShares($reportId)
172
    {
173
        $sql = $this->db->getQueryBuilder();
174
        $sql->from(self::TABLE_NAME)
175
            ->select('id', 'type', 'uid_owner', 'token', 'permissions')
176
            ->selectAlias('password', 'pass')
177
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
178
            ->andWhere($sql->expr()->eq('report', $sql->createNamedParameter($reportId)))
179
            ->andWhere($sql->expr()->neq('type', $sql->createNamedParameter(2)));
180
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

180
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
181
        $result = $statement->fetchAll();
182
        $statement->closeCursor();
183
        return $result;
184
    }
185
186
    /**
187
     * Get the all receivers of shares of a report
188
     * Used to derive who has to receive activities when a report changes
189
     * @param $reportId
190
     * @return array
191
     */
192
    public function getSharedReceiver($reportId)
193
    {
194
        $sql = $this->db->getQueryBuilder();
195
        $sql->from(self::TABLE_NAME)
196
            ->select('uid_owner')
197
            ->where($sql->expr()->eq('report', $sql->createNamedParameter($reportId)))
198
            ->andWhere($sql->expr()->eq('type', $sql->createNamedParameter(0)));
199
        $statement = $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

199
        $statement = /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
200
        $result = $statement->fetchAll();
201
        $statement->closeCursor();
202
        return $result;
203
    }
204
205
    /**
206
     * Update the password of a share
207
     * @param $shareId
208
     * @param $password
209
     * @return bool
210
     */
211
    public function updateSharePassword($shareId, $password)
212
    {
213
        $sql = $this->db->getQueryBuilder();
214
        $sql->update(self::TABLE_NAME)
215
            ->set('password', $sql->createNamedParameter($password))
216
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
217
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId)));
218
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

218
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
219
        return true;
220
    }
221
222
    /**
223
     * Update the permissions of a share
224
     * @param $shareId
225
     * @param $password
226
     * @return bool
227
     */
228
    public function updateSharePermissions($shareId, $permissions)
229
    {
230
        $sql = $this->db->getQueryBuilder();
231
        $sql->update(self::TABLE_NAME)
232
            ->set('permissions', $sql->createNamedParameter($permissions))
233
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
234
            ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($shareId)));
235
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

235
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
236
        return true;
237
    }
238
239
    /**
240
     * Delete a share
241
     * @param $shareId
242
     * @return bool
243
     * @throws \OCP\DB\Exception
244
     */
245
    public function deleteShare($shareId)
246
    {
247
        $sql = $this->db->getQueryBuilder();
248
        $sql->delete(self::TABLE_NAME)
249
            ->where($sql->expr()->eq('id', $sql->createNamedParameter($shareId)))
250
            ->andWhere($sql->expr()->orX(
251
                $sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())),
252
                $sql->expr()->eq('uid_owner', $sql->createNamedParameter($this->userSession->getUser()->getUID()))
253
            ));
254
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

254
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
255
        return true;
256
    }
257
258
    /**
259
     * Delete all shares by parent ID (users of a group share)
260
     * @param $parent
261
     * @return bool
262
     */
263
    public function deleteShareByParent($parent)
264
    {
265
        $sql = $this->db->getQueryBuilder();
266
        $sql->delete(self::TABLE_NAME)
267
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
268
            ->andWhere($sql->expr()->eq('parent', $sql->createNamedParameter($parent)));
269
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

269
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
270
        return true;
271
    }
272
273
    /**
274
     * Delete all shares of a report
275
     * Used during report deletion
276
     * @param $reportId
277
     * @return bool
278
     */
279
    public function deleteShareByReport($reportId)
280
    {
281
        $sql = $this->db->getQueryBuilder();
282
        $sql->delete(self::TABLE_NAME)
283
            ->where($sql->expr()->eq('uid_initiator', $sql->createNamedParameter($this->userSession->getUser()->getUID())))
284
            ->andWhere($sql->expr()->eq('report', $sql->createNamedParameter($reportId)));
285
        $sql->execute();
1 ignored issue
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

285
        /** @scrutinizer ignore-deprecated */ $sql->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
286
        return true;
287
    }
288
}