@@ -41,1020 +41,1020 @@ |
||
41 | 41 | |
42 | 42 | class Manager implements ICommentsManager { |
43 | 43 | |
44 | - /** @var IDBConnection */ |
|
45 | - protected $dbConn; |
|
46 | - |
|
47 | - /** @var ILogger */ |
|
48 | - protected $logger; |
|
49 | - |
|
50 | - /** @var IConfig */ |
|
51 | - protected $config; |
|
52 | - |
|
53 | - /** @var IComment[] */ |
|
54 | - protected $commentsCache = []; |
|
55 | - |
|
56 | - /** @var \Closure[] */ |
|
57 | - protected $eventHandlerClosures = []; |
|
58 | - |
|
59 | - /** @var ICommentsEventHandler[] */ |
|
60 | - protected $eventHandlers = []; |
|
61 | - |
|
62 | - /** @var \Closure[] */ |
|
63 | - protected $displayNameResolvers = []; |
|
64 | - |
|
65 | - /** |
|
66 | - * Manager constructor. |
|
67 | - * |
|
68 | - * @param IDBConnection $dbConn |
|
69 | - * @param ILogger $logger |
|
70 | - * @param IConfig $config |
|
71 | - */ |
|
72 | - public function __construct( |
|
73 | - IDBConnection $dbConn, |
|
74 | - ILogger $logger, |
|
75 | - IConfig $config |
|
76 | - ) { |
|
77 | - $this->dbConn = $dbConn; |
|
78 | - $this->logger = $logger; |
|
79 | - $this->config = $config; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * converts data base data into PHP native, proper types as defined by |
|
84 | - * IComment interface. |
|
85 | - * |
|
86 | - * @param array $data |
|
87 | - * @return array |
|
88 | - */ |
|
89 | - protected function normalizeDatabaseData(array $data) { |
|
90 | - $data['id'] = (string)$data['id']; |
|
91 | - $data['parent_id'] = (string)$data['parent_id']; |
|
92 | - $data['topmost_parent_id'] = (string)$data['topmost_parent_id']; |
|
93 | - $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
94 | - if (!is_null($data['latest_child_timestamp'])) { |
|
95 | - $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
96 | - } |
|
97 | - $data['children_count'] = (int)$data['children_count']; |
|
98 | - return $data; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * prepares a comment for an insert or update operation after making sure |
|
103 | - * all necessary fields have a value assigned. |
|
104 | - * |
|
105 | - * @param IComment $comment |
|
106 | - * @return IComment returns the same updated IComment instance as provided |
|
107 | - * by parameter for convenience |
|
108 | - * @throws \UnexpectedValueException |
|
109 | - */ |
|
110 | - protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
111 | - if (!$comment->getActorType() |
|
112 | - || !$comment->getActorId() |
|
113 | - || !$comment->getObjectType() |
|
114 | - || !$comment->getObjectId() |
|
115 | - || !$comment->getVerb() |
|
116 | - ) { |
|
117 | - throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
118 | - } |
|
119 | - |
|
120 | - if ($comment->getId() === '') { |
|
121 | - $comment->setChildrenCount(0); |
|
122 | - $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC'))); |
|
123 | - $comment->setLatestChildDateTime(null); |
|
124 | - } |
|
125 | - |
|
126 | - if (is_null($comment->getCreationDateTime())) { |
|
127 | - $comment->setCreationDateTime(new \DateTime()); |
|
128 | - } |
|
129 | - |
|
130 | - if ($comment->getParentId() !== '0') { |
|
131 | - $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId())); |
|
132 | - } else { |
|
133 | - $comment->setTopmostParentId('0'); |
|
134 | - } |
|
135 | - |
|
136 | - $this->cache($comment); |
|
137 | - |
|
138 | - return $comment; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * returns the topmost parent id of a given comment identified by ID |
|
143 | - * |
|
144 | - * @param string $id |
|
145 | - * @return string |
|
146 | - * @throws NotFoundException |
|
147 | - */ |
|
148 | - protected function determineTopmostParentId($id) { |
|
149 | - $comment = $this->get($id); |
|
150 | - if ($comment->getParentId() === '0') { |
|
151 | - return $comment->getId(); |
|
152 | - } else { |
|
153 | - return $this->determineTopmostParentId($comment->getId()); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * updates child information of a comment |
|
159 | - * |
|
160 | - * @param string $id |
|
161 | - * @param \DateTime $cDateTime the date time of the most recent child |
|
162 | - * @throws NotFoundException |
|
163 | - */ |
|
164 | - protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
|
165 | - $qb = $this->dbConn->getQueryBuilder(); |
|
166 | - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
167 | - ->from('comments') |
|
168 | - ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
|
169 | - ->setParameter('id', $id); |
|
170 | - |
|
171 | - $resultStatement = $query->execute(); |
|
172 | - $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
173 | - $resultStatement->closeCursor(); |
|
174 | - $children = (int)$data[0]; |
|
175 | - |
|
176 | - $comment = $this->get($id); |
|
177 | - $comment->setChildrenCount($children); |
|
178 | - $comment->setLatestChildDateTime($cDateTime); |
|
179 | - $this->save($comment); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Tests whether actor or object type and id parameters are acceptable. |
|
184 | - * Throws exception if not. |
|
185 | - * |
|
186 | - * @param string $role |
|
187 | - * @param string $type |
|
188 | - * @param string $id |
|
189 | - * @throws \InvalidArgumentException |
|
190 | - */ |
|
191 | - protected function checkRoleParameters($role, $type, $id) { |
|
192 | - if ( |
|
193 | - !is_string($type) || empty($type) |
|
194 | - || !is_string($id) || empty($id) |
|
195 | - ) { |
|
196 | - throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * run-time caches a comment |
|
202 | - * |
|
203 | - * @param IComment $comment |
|
204 | - */ |
|
205 | - protected function cache(IComment $comment) { |
|
206 | - $id = $comment->getId(); |
|
207 | - if (empty($id)) { |
|
208 | - return; |
|
209 | - } |
|
210 | - $this->commentsCache[(string)$id] = $comment; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * removes an entry from the comments run time cache |
|
215 | - * |
|
216 | - * @param mixed $id the comment's id |
|
217 | - */ |
|
218 | - protected function uncache($id) { |
|
219 | - $id = (string)$id; |
|
220 | - if (isset($this->commentsCache[$id])) { |
|
221 | - unset($this->commentsCache[$id]); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * returns a comment instance |
|
227 | - * |
|
228 | - * @param string $id the ID of the comment |
|
229 | - * @return IComment |
|
230 | - * @throws NotFoundException |
|
231 | - * @throws \InvalidArgumentException |
|
232 | - * @since 9.0.0 |
|
233 | - */ |
|
234 | - public function get($id) { |
|
235 | - if ((int)$id === 0) { |
|
236 | - throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
|
237 | - } |
|
238 | - |
|
239 | - if (isset($this->commentsCache[$id])) { |
|
240 | - return $this->commentsCache[$id]; |
|
241 | - } |
|
242 | - |
|
243 | - $qb = $this->dbConn->getQueryBuilder(); |
|
244 | - $resultStatement = $qb->select('*') |
|
245 | - ->from('comments') |
|
246 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
247 | - ->setParameter('id', $id, IQueryBuilder::PARAM_INT) |
|
248 | - ->execute(); |
|
249 | - |
|
250 | - $data = $resultStatement->fetch(); |
|
251 | - $resultStatement->closeCursor(); |
|
252 | - if (!$data) { |
|
253 | - throw new NotFoundException(); |
|
254 | - } |
|
255 | - |
|
256 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
257 | - $this->cache($comment); |
|
258 | - return $comment; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * returns the comment specified by the id and all it's child comments. |
|
263 | - * At this point of time, we do only support one level depth. |
|
264 | - * |
|
265 | - * @param string $id |
|
266 | - * @param int $limit max number of entries to return, 0 returns all |
|
267 | - * @param int $offset the start entry |
|
268 | - * @return array |
|
269 | - * @since 9.0.0 |
|
270 | - * |
|
271 | - * The return array looks like this |
|
272 | - * [ |
|
273 | - * 'comment' => IComment, // root comment |
|
274 | - * 'replies' => |
|
275 | - * [ |
|
276 | - * 0 => |
|
277 | - * [ |
|
278 | - * 'comment' => IComment, |
|
279 | - * 'replies' => [] |
|
280 | - * ] |
|
281 | - * 1 => |
|
282 | - * [ |
|
283 | - * 'comment' => IComment, |
|
284 | - * 'replies'=> [] |
|
285 | - * ], |
|
286 | - * … |
|
287 | - * ] |
|
288 | - * ] |
|
289 | - */ |
|
290 | - public function getTree($id, $limit = 0, $offset = 0) { |
|
291 | - $tree = []; |
|
292 | - $tree['comment'] = $this->get($id); |
|
293 | - $tree['replies'] = []; |
|
294 | - |
|
295 | - $qb = $this->dbConn->getQueryBuilder(); |
|
296 | - $query = $qb->select('*') |
|
297 | - ->from('comments') |
|
298 | - ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id'))) |
|
299 | - ->orderBy('creation_timestamp', 'DESC') |
|
300 | - ->setParameter('id', $id); |
|
301 | - |
|
302 | - if ($limit > 0) { |
|
303 | - $query->setMaxResults($limit); |
|
304 | - } |
|
305 | - if ($offset > 0) { |
|
306 | - $query->setFirstResult($offset); |
|
307 | - } |
|
308 | - |
|
309 | - $resultStatement = $query->execute(); |
|
310 | - while ($data = $resultStatement->fetch()) { |
|
311 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
312 | - $this->cache($comment); |
|
313 | - $tree['replies'][] = [ |
|
314 | - 'comment' => $comment, |
|
315 | - 'replies' => [] |
|
316 | - ]; |
|
317 | - } |
|
318 | - $resultStatement->closeCursor(); |
|
319 | - |
|
320 | - return $tree; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * returns comments for a specific object (e.g. a file). |
|
325 | - * |
|
326 | - * The sort order is always newest to oldest. |
|
327 | - * |
|
328 | - * @param string $objectType the object type, e.g. 'files' |
|
329 | - * @param string $objectId the id of the object |
|
330 | - * @param int $limit optional, number of maximum comments to be returned. if |
|
331 | - * not specified, all comments are returned. |
|
332 | - * @param int $offset optional, starting point |
|
333 | - * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
334 | - * that may be returned |
|
335 | - * @return IComment[] |
|
336 | - * @since 9.0.0 |
|
337 | - */ |
|
338 | - public function getForObject( |
|
339 | - $objectType, |
|
340 | - $objectId, |
|
341 | - $limit = 0, |
|
342 | - $offset = 0, |
|
343 | - \DateTime $notOlderThan = null |
|
344 | - ) { |
|
345 | - $comments = []; |
|
346 | - |
|
347 | - $qb = $this->dbConn->getQueryBuilder(); |
|
348 | - $query = $qb->select('*') |
|
349 | - ->from('comments') |
|
350 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
351 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
352 | - ->orderBy('creation_timestamp', 'DESC') |
|
353 | - ->setParameter('type', $objectType) |
|
354 | - ->setParameter('id', $objectId); |
|
355 | - |
|
356 | - if ($limit > 0) { |
|
357 | - $query->setMaxResults($limit); |
|
358 | - } |
|
359 | - if ($offset > 0) { |
|
360 | - $query->setFirstResult($offset); |
|
361 | - } |
|
362 | - if (!is_null($notOlderThan)) { |
|
363 | - $query |
|
364 | - ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
365 | - ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
366 | - } |
|
367 | - |
|
368 | - $resultStatement = $query->execute(); |
|
369 | - while ($data = $resultStatement->fetch()) { |
|
370 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
371 | - $this->cache($comment); |
|
372 | - $comments[] = $comment; |
|
373 | - } |
|
374 | - $resultStatement->closeCursor(); |
|
375 | - |
|
376 | - return $comments; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * @param string $objectType the object type, e.g. 'files' |
|
381 | - * @param string $objectId the id of the object |
|
382 | - * @param int $lastKnownCommentId the last known comment (will be used as offset) |
|
383 | - * @param string $sortDirection direction of the comments (`asc` or `desc`) |
|
384 | - * @param int $limit optional, number of maximum comments to be returned. if |
|
385 | - * set to 0, all comments are returned. |
|
386 | - * @return IComment[] |
|
387 | - * @return array |
|
388 | - */ |
|
389 | - public function getForObjectSince( |
|
390 | - string $objectType, |
|
391 | - string $objectId, |
|
392 | - int $lastKnownCommentId, |
|
393 | - string $sortDirection = 'asc', |
|
394 | - int $limit = 30 |
|
395 | - ): array { |
|
396 | - $comments = []; |
|
397 | - |
|
398 | - $query = $this->dbConn->getQueryBuilder(); |
|
399 | - $query->select('*') |
|
400 | - ->from('comments') |
|
401 | - ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType))) |
|
402 | - ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))) |
|
403 | - ->orderBy('creation_timestamp', $sortDirection === 'desc' ? 'DESC' : 'ASC') |
|
404 | - ->addOrderBy('id', $sortDirection === 'desc' ? 'DESC' : 'ASC'); |
|
405 | - |
|
406 | - if ($limit > 0) { |
|
407 | - $query->setMaxResults($limit); |
|
408 | - } |
|
409 | - |
|
410 | - $lastKnownComment = $lastKnownCommentId > 0 ? $this->getLastKnownComment( |
|
411 | - $objectType, |
|
412 | - $objectId, |
|
413 | - $lastKnownCommentId |
|
414 | - ) : null; |
|
415 | - if ($lastKnownComment instanceof IComment) { |
|
416 | - $lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime(); |
|
417 | - if ($sortDirection === 'desc') { |
|
418 | - $query->andWhere( |
|
419 | - $query->expr()->orX( |
|
420 | - $query->expr()->lt( |
|
421 | - 'creation_timestamp', |
|
422 | - $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
423 | - IQueryBuilder::PARAM_DATE |
|
424 | - ), |
|
425 | - $query->expr()->andX( |
|
426 | - $query->expr()->eq( |
|
427 | - 'creation_timestamp', |
|
428 | - $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
429 | - IQueryBuilder::PARAM_DATE |
|
430 | - ), |
|
431 | - $query->expr()->lt('id', $query->createNamedParameter($lastKnownCommentId)) |
|
432 | - ) |
|
433 | - ) |
|
434 | - ); |
|
435 | - } else { |
|
436 | - $query->andWhere( |
|
437 | - $query->expr()->orX( |
|
438 | - $query->expr()->gt( |
|
439 | - 'creation_timestamp', |
|
440 | - $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
441 | - IQueryBuilder::PARAM_DATE |
|
442 | - ), |
|
443 | - $query->expr()->andX( |
|
444 | - $query->expr()->eq( |
|
445 | - 'creation_timestamp', |
|
446 | - $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
447 | - IQueryBuilder::PARAM_DATE |
|
448 | - ), |
|
449 | - $query->expr()->gt('id', $query->createNamedParameter($lastKnownCommentId)) |
|
450 | - ) |
|
451 | - ) |
|
452 | - ); |
|
453 | - } |
|
454 | - } |
|
455 | - |
|
456 | - $resultStatement = $query->execute(); |
|
457 | - while ($data = $resultStatement->fetch()) { |
|
458 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
459 | - $this->cache($comment); |
|
460 | - $comments[] = $comment; |
|
461 | - } |
|
462 | - $resultStatement->closeCursor(); |
|
463 | - |
|
464 | - return $comments; |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * @param string $objectType the object type, e.g. 'files' |
|
469 | - * @param string $objectId the id of the object |
|
470 | - * @param int $id the comment to look for |
|
471 | - * @return Comment|null |
|
472 | - */ |
|
473 | - protected function getLastKnownComment(string $objectType, |
|
474 | - string $objectId, |
|
475 | - int $id) { |
|
476 | - $query = $this->dbConn->getQueryBuilder(); |
|
477 | - $query->select('*') |
|
478 | - ->from('comments') |
|
479 | - ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType))) |
|
480 | - ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))) |
|
481 | - ->andWhere($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
482 | - |
|
483 | - $result = $query->execute(); |
|
484 | - $row = $result->fetch(); |
|
485 | - $result->closeCursor(); |
|
486 | - |
|
487 | - if ($row) { |
|
488 | - $comment = new Comment($this->normalizeDatabaseData($row)); |
|
489 | - $this->cache($comment); |
|
490 | - return $comment; |
|
491 | - } |
|
492 | - |
|
493 | - return null; |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Search for comments with a given content |
|
498 | - * |
|
499 | - * @param string $search content to search for |
|
500 | - * @param string $objectType Limit the search by object type |
|
501 | - * @param string $objectId Limit the search by object id |
|
502 | - * @param string $verb Limit the verb of the comment |
|
503 | - * @param int $offset |
|
504 | - * @param int $limit |
|
505 | - * @return IComment[] |
|
506 | - */ |
|
507 | - public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { |
|
508 | - $query = $this->dbConn->getQueryBuilder(); |
|
509 | - |
|
510 | - $query->select('*') |
|
511 | - ->from('comments') |
|
512 | - ->where($query->expr()->iLike('message', $query->createNamedParameter( |
|
513 | - '%' . $this->dbConn->escapeLikeParameter($search). '%' |
|
514 | - ))) |
|
515 | - ->orderBy('creation_timestamp', 'DESC') |
|
516 | - ->addOrderBy('id', 'DESC') |
|
517 | - ->setMaxResults($limit); |
|
518 | - |
|
519 | - if ($objectType !== '') { |
|
520 | - $query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); |
|
521 | - } |
|
522 | - if ($objectId !== '') { |
|
523 | - $query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); |
|
524 | - } |
|
525 | - if ($verb !== '') { |
|
526 | - $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); |
|
527 | - } |
|
528 | - if ($offset !== 0) { |
|
529 | - $query->setFirstResult($offset); |
|
530 | - } |
|
531 | - |
|
532 | - $comments = []; |
|
533 | - $result = $query->execute(); |
|
534 | - while ($data = $result->fetch()) { |
|
535 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
536 | - $this->cache($comment); |
|
537 | - $comments[] = $comment; |
|
538 | - } |
|
539 | - $result->closeCursor(); |
|
540 | - |
|
541 | - return $comments; |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * @param $objectType string the object type, e.g. 'files' |
|
546 | - * @param $objectId string the id of the object |
|
547 | - * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
548 | - * that may be returned |
|
549 | - * @param string $verb Limit the verb of the comment - Added in 14.0.0 |
|
550 | - * @return Int |
|
551 | - * @since 9.0.0 |
|
552 | - */ |
|
553 | - public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '') { |
|
554 | - $qb = $this->dbConn->getQueryBuilder(); |
|
555 | - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
556 | - ->from('comments') |
|
557 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
558 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
559 | - ->setParameter('type', $objectType) |
|
560 | - ->setParameter('id', $objectId); |
|
561 | - |
|
562 | - if (!is_null($notOlderThan)) { |
|
563 | - $query |
|
564 | - ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
565 | - ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
566 | - } |
|
567 | - |
|
568 | - if ($verb !== '') { |
|
569 | - $query->andWhere($qb->expr()->eq('verb', $qb->createNamedParameter($verb))); |
|
570 | - } |
|
571 | - |
|
572 | - $resultStatement = $query->execute(); |
|
573 | - $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
574 | - $resultStatement->closeCursor(); |
|
575 | - return (int)$data[0]; |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * Get the number of unread comments for all files in a folder |
|
580 | - * |
|
581 | - * @param int $folderId |
|
582 | - * @param IUser $user |
|
583 | - * @return array [$fileId => $unreadCount] |
|
584 | - */ |
|
585 | - public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
|
586 | - $qb = $this->dbConn->getQueryBuilder(); |
|
587 | - $query = $qb->select('f.fileid') |
|
588 | - ->selectAlias( |
|
589 | - $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'), |
|
590 | - 'num_ids' |
|
591 | - ) |
|
592 | - ->from('comments', 'c') |
|
593 | - ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX( |
|
594 | - $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), |
|
595 | - $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)) |
|
596 | - )) |
|
597 | - ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX( |
|
598 | - $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')), |
|
599 | - $qb->expr()->eq('m.object_id', 'c.object_id'), |
|
600 | - $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())) |
|
601 | - )) |
|
602 | - ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId))) |
|
603 | - ->andWhere($qb->expr()->orX( |
|
604 | - $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'), |
|
605 | - $qb->expr()->isNull('marker_datetime') |
|
606 | - )) |
|
607 | - ->groupBy('f.fileid'); |
|
608 | - |
|
609 | - $resultStatement = $query->execute(); |
|
610 | - |
|
611 | - $results = []; |
|
612 | - while ($row = $resultStatement->fetch()) { |
|
613 | - $results[$row['fileid']] = (int) $row['num_ids']; |
|
614 | - } |
|
615 | - $resultStatement->closeCursor(); |
|
616 | - return $results; |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * creates a new comment and returns it. At this point of time, it is not |
|
621 | - * saved in the used data storage. Use save() after setting other fields |
|
622 | - * of the comment (e.g. message or verb). |
|
623 | - * |
|
624 | - * @param string $actorType the actor type (e.g. 'users') |
|
625 | - * @param string $actorId a user id |
|
626 | - * @param string $objectType the object type the comment is attached to |
|
627 | - * @param string $objectId the object id the comment is attached to |
|
628 | - * @return IComment |
|
629 | - * @since 9.0.0 |
|
630 | - */ |
|
631 | - public function create($actorType, $actorId, $objectType, $objectId) { |
|
632 | - $comment = new Comment(); |
|
633 | - $comment |
|
634 | - ->setActor($actorType, $actorId) |
|
635 | - ->setObject($objectType, $objectId); |
|
636 | - return $comment; |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * permanently deletes the comment specified by the ID |
|
641 | - * |
|
642 | - * When the comment has child comments, their parent ID will be changed to |
|
643 | - * the parent ID of the item that is to be deleted. |
|
644 | - * |
|
645 | - * @param string $id |
|
646 | - * @return bool |
|
647 | - * @throws \InvalidArgumentException |
|
648 | - * @since 9.0.0 |
|
649 | - */ |
|
650 | - public function delete($id) { |
|
651 | - if (!is_string($id)) { |
|
652 | - throw new \InvalidArgumentException('Parameter must be string'); |
|
653 | - } |
|
654 | - |
|
655 | - try { |
|
656 | - $comment = $this->get($id); |
|
657 | - } catch (\Exception $e) { |
|
658 | - // Ignore exceptions, we just don't fire a hook then |
|
659 | - $comment = null; |
|
660 | - } |
|
661 | - |
|
662 | - $qb = $this->dbConn->getQueryBuilder(); |
|
663 | - $query = $qb->delete('comments') |
|
664 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
665 | - ->setParameter('id', $id); |
|
666 | - |
|
667 | - try { |
|
668 | - $affectedRows = $query->execute(); |
|
669 | - $this->uncache($id); |
|
670 | - } catch (DriverException $e) { |
|
671 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
672 | - return false; |
|
673 | - } |
|
674 | - |
|
675 | - if ($affectedRows > 0 && $comment instanceof IComment) { |
|
676 | - $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment); |
|
677 | - } |
|
678 | - |
|
679 | - return ($affectedRows > 0); |
|
680 | - } |
|
681 | - |
|
682 | - /** |
|
683 | - * saves the comment permanently |
|
684 | - * |
|
685 | - * if the supplied comment has an empty ID, a new entry comment will be |
|
686 | - * saved and the instance updated with the new ID. |
|
687 | - * |
|
688 | - * Otherwise, an existing comment will be updated. |
|
689 | - * |
|
690 | - * Throws NotFoundException when a comment that is to be updated does not |
|
691 | - * exist anymore at this point of time. |
|
692 | - * |
|
693 | - * @param IComment $comment |
|
694 | - * @return bool |
|
695 | - * @throws NotFoundException |
|
696 | - * @since 9.0.0 |
|
697 | - */ |
|
698 | - public function save(IComment $comment) { |
|
699 | - if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') { |
|
700 | - $result = $this->insert($comment); |
|
701 | - } else { |
|
702 | - $result = $this->update($comment); |
|
703 | - } |
|
704 | - |
|
705 | - if ($result && !!$comment->getParentId()) { |
|
706 | - $this->updateChildrenInformation( |
|
707 | - $comment->getParentId(), |
|
708 | - $comment->getCreationDateTime() |
|
709 | - ); |
|
710 | - $this->cache($comment); |
|
711 | - } |
|
712 | - |
|
713 | - return $result; |
|
714 | - } |
|
715 | - |
|
716 | - /** |
|
717 | - * inserts the provided comment in the database |
|
718 | - * |
|
719 | - * @param IComment $comment |
|
720 | - * @return bool |
|
721 | - */ |
|
722 | - protected function insert(IComment &$comment) { |
|
723 | - $qb = $this->dbConn->getQueryBuilder(); |
|
724 | - $affectedRows = $qb |
|
725 | - ->insert('comments') |
|
726 | - ->values([ |
|
727 | - 'parent_id' => $qb->createNamedParameter($comment->getParentId()), |
|
728 | - 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), |
|
729 | - 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), |
|
730 | - 'actor_type' => $qb->createNamedParameter($comment->getActorType()), |
|
731 | - 'actor_id' => $qb->createNamedParameter($comment->getActorId()), |
|
732 | - 'message' => $qb->createNamedParameter($comment->getMessage()), |
|
733 | - 'verb' => $qb->createNamedParameter($comment->getVerb()), |
|
734 | - 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), |
|
735 | - 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), |
|
736 | - 'object_type' => $qb->createNamedParameter($comment->getObjectType()), |
|
737 | - 'object_id' => $qb->createNamedParameter($comment->getObjectId()), |
|
738 | - ]) |
|
739 | - ->execute(); |
|
740 | - |
|
741 | - if ($affectedRows > 0) { |
|
742 | - $comment->setId((string)$qb->getLastInsertId()); |
|
743 | - $this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
|
744 | - } |
|
745 | - |
|
746 | - return $affectedRows > 0; |
|
747 | - } |
|
748 | - |
|
749 | - /** |
|
750 | - * updates a Comment data row |
|
751 | - * |
|
752 | - * @param IComment $comment |
|
753 | - * @return bool |
|
754 | - * @throws NotFoundException |
|
755 | - */ |
|
756 | - protected function update(IComment $comment) { |
|
757 | - // for properly working preUpdate Events we need the old comments as is |
|
758 | - // in the DB and overcome caching. Also avoid that outdated information stays. |
|
759 | - $this->uncache($comment->getId()); |
|
760 | - $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); |
|
761 | - $this->uncache($comment->getId()); |
|
762 | - |
|
763 | - $qb = $this->dbConn->getQueryBuilder(); |
|
764 | - $affectedRows = $qb |
|
765 | - ->update('comments') |
|
766 | - ->set('parent_id', $qb->createNamedParameter($comment->getParentId())) |
|
767 | - ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) |
|
768 | - ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount())) |
|
769 | - ->set('actor_type', $qb->createNamedParameter($comment->getActorType())) |
|
770 | - ->set('actor_id', $qb->createNamedParameter($comment->getActorId())) |
|
771 | - ->set('message', $qb->createNamedParameter($comment->getMessage())) |
|
772 | - ->set('verb', $qb->createNamedParameter($comment->getVerb())) |
|
773 | - ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) |
|
774 | - ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) |
|
775 | - ->set('object_type', $qb->createNamedParameter($comment->getObjectType())) |
|
776 | - ->set('object_id', $qb->createNamedParameter($comment->getObjectId())) |
|
777 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
778 | - ->setParameter('id', $comment->getId()) |
|
779 | - ->execute(); |
|
780 | - |
|
781 | - if ($affectedRows === 0) { |
|
782 | - throw new NotFoundException('Comment to update does ceased to exist'); |
|
783 | - } |
|
784 | - |
|
785 | - $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); |
|
786 | - |
|
787 | - return $affectedRows > 0; |
|
788 | - } |
|
789 | - |
|
790 | - /** |
|
791 | - * removes references to specific actor (e.g. on user delete) of a comment. |
|
792 | - * The comment itself must not get lost/deleted. |
|
793 | - * |
|
794 | - * @param string $actorType the actor type (e.g. 'users') |
|
795 | - * @param string $actorId a user id |
|
796 | - * @return boolean |
|
797 | - * @since 9.0.0 |
|
798 | - */ |
|
799 | - public function deleteReferencesOfActor($actorType, $actorId) { |
|
800 | - $this->checkRoleParameters('Actor', $actorType, $actorId); |
|
801 | - |
|
802 | - $qb = $this->dbConn->getQueryBuilder(); |
|
803 | - $affectedRows = $qb |
|
804 | - ->update('comments') |
|
805 | - ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
806 | - ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
807 | - ->where($qb->expr()->eq('actor_type', $qb->createParameter('type'))) |
|
808 | - ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id'))) |
|
809 | - ->setParameter('type', $actorType) |
|
810 | - ->setParameter('id', $actorId) |
|
811 | - ->execute(); |
|
812 | - |
|
813 | - $this->commentsCache = []; |
|
814 | - |
|
815 | - return is_int($affectedRows); |
|
816 | - } |
|
817 | - |
|
818 | - /** |
|
819 | - * deletes all comments made of a specific object (e.g. on file delete) |
|
820 | - * |
|
821 | - * @param string $objectType the object type (e.g. 'files') |
|
822 | - * @param string $objectId e.g. the file id |
|
823 | - * @return boolean |
|
824 | - * @since 9.0.0 |
|
825 | - */ |
|
826 | - public function deleteCommentsAtObject($objectType, $objectId) { |
|
827 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
828 | - |
|
829 | - $qb = $this->dbConn->getQueryBuilder(); |
|
830 | - $affectedRows = $qb |
|
831 | - ->delete('comments') |
|
832 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
833 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
834 | - ->setParameter('type', $objectType) |
|
835 | - ->setParameter('id', $objectId) |
|
836 | - ->execute(); |
|
837 | - |
|
838 | - $this->commentsCache = []; |
|
839 | - |
|
840 | - return is_int($affectedRows); |
|
841 | - } |
|
842 | - |
|
843 | - /** |
|
844 | - * deletes the read markers for the specified user |
|
845 | - * |
|
846 | - * @param \OCP\IUser $user |
|
847 | - * @return bool |
|
848 | - * @since 9.0.0 |
|
849 | - */ |
|
850 | - public function deleteReadMarksFromUser(IUser $user) { |
|
851 | - $qb = $this->dbConn->getQueryBuilder(); |
|
852 | - $query = $qb->delete('comments_read_markers') |
|
853 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
854 | - ->setParameter('user_id', $user->getUID()); |
|
855 | - |
|
856 | - try { |
|
857 | - $affectedRows = $query->execute(); |
|
858 | - } catch (DriverException $e) { |
|
859 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
860 | - return false; |
|
861 | - } |
|
862 | - return ($affectedRows > 0); |
|
863 | - } |
|
864 | - |
|
865 | - /** |
|
866 | - * sets the read marker for a given file to the specified date for the |
|
867 | - * provided user |
|
868 | - * |
|
869 | - * @param string $objectType |
|
870 | - * @param string $objectId |
|
871 | - * @param \DateTime $dateTime |
|
872 | - * @param IUser $user |
|
873 | - * @since 9.0.0 |
|
874 | - * @suppress SqlInjectionChecker |
|
875 | - */ |
|
876 | - public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
|
877 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
878 | - |
|
879 | - $qb = $this->dbConn->getQueryBuilder(); |
|
880 | - $values = [ |
|
881 | - 'user_id' => $qb->createNamedParameter($user->getUID()), |
|
882 | - 'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'), |
|
883 | - 'object_type' => $qb->createNamedParameter($objectType), |
|
884 | - 'object_id' => $qb->createNamedParameter($objectId), |
|
885 | - ]; |
|
886 | - |
|
887 | - // Strategy: try to update, if this does not return affected rows, do an insert. |
|
888 | - $affectedRows = $qb |
|
889 | - ->update('comments_read_markers') |
|
890 | - ->set('user_id', $values['user_id']) |
|
891 | - ->set('marker_datetime', $values['marker_datetime']) |
|
892 | - ->set('object_type', $values['object_type']) |
|
893 | - ->set('object_id', $values['object_id']) |
|
894 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
895 | - ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
896 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
897 | - ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
898 | - ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
899 | - ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
900 | - ->execute(); |
|
901 | - |
|
902 | - if ($affectedRows > 0) { |
|
903 | - return; |
|
904 | - } |
|
905 | - |
|
906 | - $qb->insert('comments_read_markers') |
|
907 | - ->values($values) |
|
908 | - ->execute(); |
|
909 | - } |
|
910 | - |
|
911 | - /** |
|
912 | - * returns the read marker for a given file to the specified date for the |
|
913 | - * provided user. It returns null, when the marker is not present, i.e. |
|
914 | - * no comments were marked as read. |
|
915 | - * |
|
916 | - * @param string $objectType |
|
917 | - * @param string $objectId |
|
918 | - * @param IUser $user |
|
919 | - * @return \DateTime|null |
|
920 | - * @since 9.0.0 |
|
921 | - */ |
|
922 | - public function getReadMark($objectType, $objectId, IUser $user) { |
|
923 | - $qb = $this->dbConn->getQueryBuilder(); |
|
924 | - $resultStatement = $qb->select('marker_datetime') |
|
925 | - ->from('comments_read_markers') |
|
926 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
927 | - ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
928 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
929 | - ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
930 | - ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
931 | - ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
932 | - ->execute(); |
|
933 | - |
|
934 | - $data = $resultStatement->fetch(); |
|
935 | - $resultStatement->closeCursor(); |
|
936 | - if (!$data || is_null($data['marker_datetime'])) { |
|
937 | - return null; |
|
938 | - } |
|
939 | - |
|
940 | - return new \DateTime($data['marker_datetime']); |
|
941 | - } |
|
942 | - |
|
943 | - /** |
|
944 | - * deletes the read markers on the specified object |
|
945 | - * |
|
946 | - * @param string $objectType |
|
947 | - * @param string $objectId |
|
948 | - * @return bool |
|
949 | - * @since 9.0.0 |
|
950 | - */ |
|
951 | - public function deleteReadMarksOnObject($objectType, $objectId) { |
|
952 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
953 | - |
|
954 | - $qb = $this->dbConn->getQueryBuilder(); |
|
955 | - $query = $qb->delete('comments_read_markers') |
|
956 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
957 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
958 | - ->setParameter('object_type', $objectType) |
|
959 | - ->setParameter('object_id', $objectId); |
|
960 | - |
|
961 | - try { |
|
962 | - $affectedRows = $query->execute(); |
|
963 | - } catch (DriverException $e) { |
|
964 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
965 | - return false; |
|
966 | - } |
|
967 | - return ($affectedRows > 0); |
|
968 | - } |
|
969 | - |
|
970 | - /** |
|
971 | - * registers an Entity to the manager, so event notifications can be send |
|
972 | - * to consumers of the comments infrastructure |
|
973 | - * |
|
974 | - * @param \Closure $closure |
|
975 | - */ |
|
976 | - public function registerEventHandler(\Closure $closure) { |
|
977 | - $this->eventHandlerClosures[] = $closure; |
|
978 | - $this->eventHandlers = []; |
|
979 | - } |
|
980 | - |
|
981 | - /** |
|
982 | - * registers a method that resolves an ID to a display name for a given type |
|
983 | - * |
|
984 | - * @param string $type |
|
985 | - * @param \Closure $closure |
|
986 | - * @throws \OutOfBoundsException |
|
987 | - * @since 11.0.0 |
|
988 | - * |
|
989 | - * Only one resolver shall be registered per type. Otherwise a |
|
990 | - * \OutOfBoundsException has to thrown. |
|
991 | - */ |
|
992 | - public function registerDisplayNameResolver($type, \Closure $closure) { |
|
993 | - if (!is_string($type)) { |
|
994 | - throw new \InvalidArgumentException('String expected.'); |
|
995 | - } |
|
996 | - if (isset($this->displayNameResolvers[$type])) { |
|
997 | - throw new \OutOfBoundsException('Displayname resolver for this type already registered'); |
|
998 | - } |
|
999 | - $this->displayNameResolvers[$type] = $closure; |
|
1000 | - } |
|
1001 | - |
|
1002 | - /** |
|
1003 | - * resolves a given ID of a given Type to a display name. |
|
1004 | - * |
|
1005 | - * @param string $type |
|
1006 | - * @param string $id |
|
1007 | - * @return string |
|
1008 | - * @throws \OutOfBoundsException |
|
1009 | - * @since 11.0.0 |
|
1010 | - * |
|
1011 | - * If a provided type was not registered, an \OutOfBoundsException shall |
|
1012 | - * be thrown. It is upon the resolver discretion what to return of the |
|
1013 | - * provided ID is unknown. It must be ensured that a string is returned. |
|
1014 | - */ |
|
1015 | - public function resolveDisplayName($type, $id) { |
|
1016 | - if (!is_string($type)) { |
|
1017 | - throw new \InvalidArgumentException('String expected.'); |
|
1018 | - } |
|
1019 | - if (!isset($this->displayNameResolvers[$type])) { |
|
1020 | - throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
|
1021 | - } |
|
1022 | - return (string)$this->displayNameResolvers[$type]($id); |
|
1023 | - } |
|
1024 | - |
|
1025 | - /** |
|
1026 | - * returns valid, registered entities |
|
1027 | - * |
|
1028 | - * @return \OCP\Comments\ICommentsEventHandler[] |
|
1029 | - */ |
|
1030 | - private function getEventHandlers() { |
|
1031 | - if (!empty($this->eventHandlers)) { |
|
1032 | - return $this->eventHandlers; |
|
1033 | - } |
|
1034 | - |
|
1035 | - $this->eventHandlers = []; |
|
1036 | - foreach ($this->eventHandlerClosures as $name => $closure) { |
|
1037 | - $entity = $closure(); |
|
1038 | - if (!($entity instanceof ICommentsEventHandler)) { |
|
1039 | - throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface'); |
|
1040 | - } |
|
1041 | - $this->eventHandlers[$name] = $entity; |
|
1042 | - } |
|
1043 | - |
|
1044 | - return $this->eventHandlers; |
|
1045 | - } |
|
1046 | - |
|
1047 | - /** |
|
1048 | - * sends notifications to the registered entities |
|
1049 | - * |
|
1050 | - * @param $eventType |
|
1051 | - * @param IComment $comment |
|
1052 | - */ |
|
1053 | - private function sendEvent($eventType, IComment $comment) { |
|
1054 | - $entities = $this->getEventHandlers(); |
|
1055 | - $event = new CommentsEvent($eventType, $comment); |
|
1056 | - foreach ($entities as $entity) { |
|
1057 | - $entity->handle($event); |
|
1058 | - } |
|
1059 | - } |
|
44 | + /** @var IDBConnection */ |
|
45 | + protected $dbConn; |
|
46 | + |
|
47 | + /** @var ILogger */ |
|
48 | + protected $logger; |
|
49 | + |
|
50 | + /** @var IConfig */ |
|
51 | + protected $config; |
|
52 | + |
|
53 | + /** @var IComment[] */ |
|
54 | + protected $commentsCache = []; |
|
55 | + |
|
56 | + /** @var \Closure[] */ |
|
57 | + protected $eventHandlerClosures = []; |
|
58 | + |
|
59 | + /** @var ICommentsEventHandler[] */ |
|
60 | + protected $eventHandlers = []; |
|
61 | + |
|
62 | + /** @var \Closure[] */ |
|
63 | + protected $displayNameResolvers = []; |
|
64 | + |
|
65 | + /** |
|
66 | + * Manager constructor. |
|
67 | + * |
|
68 | + * @param IDBConnection $dbConn |
|
69 | + * @param ILogger $logger |
|
70 | + * @param IConfig $config |
|
71 | + */ |
|
72 | + public function __construct( |
|
73 | + IDBConnection $dbConn, |
|
74 | + ILogger $logger, |
|
75 | + IConfig $config |
|
76 | + ) { |
|
77 | + $this->dbConn = $dbConn; |
|
78 | + $this->logger = $logger; |
|
79 | + $this->config = $config; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * converts data base data into PHP native, proper types as defined by |
|
84 | + * IComment interface. |
|
85 | + * |
|
86 | + * @param array $data |
|
87 | + * @return array |
|
88 | + */ |
|
89 | + protected function normalizeDatabaseData(array $data) { |
|
90 | + $data['id'] = (string)$data['id']; |
|
91 | + $data['parent_id'] = (string)$data['parent_id']; |
|
92 | + $data['topmost_parent_id'] = (string)$data['topmost_parent_id']; |
|
93 | + $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
94 | + if (!is_null($data['latest_child_timestamp'])) { |
|
95 | + $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
96 | + } |
|
97 | + $data['children_count'] = (int)$data['children_count']; |
|
98 | + return $data; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * prepares a comment for an insert or update operation after making sure |
|
103 | + * all necessary fields have a value assigned. |
|
104 | + * |
|
105 | + * @param IComment $comment |
|
106 | + * @return IComment returns the same updated IComment instance as provided |
|
107 | + * by parameter for convenience |
|
108 | + * @throws \UnexpectedValueException |
|
109 | + */ |
|
110 | + protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
111 | + if (!$comment->getActorType() |
|
112 | + || !$comment->getActorId() |
|
113 | + || !$comment->getObjectType() |
|
114 | + || !$comment->getObjectId() |
|
115 | + || !$comment->getVerb() |
|
116 | + ) { |
|
117 | + throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
118 | + } |
|
119 | + |
|
120 | + if ($comment->getId() === '') { |
|
121 | + $comment->setChildrenCount(0); |
|
122 | + $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC'))); |
|
123 | + $comment->setLatestChildDateTime(null); |
|
124 | + } |
|
125 | + |
|
126 | + if (is_null($comment->getCreationDateTime())) { |
|
127 | + $comment->setCreationDateTime(new \DateTime()); |
|
128 | + } |
|
129 | + |
|
130 | + if ($comment->getParentId() !== '0') { |
|
131 | + $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId())); |
|
132 | + } else { |
|
133 | + $comment->setTopmostParentId('0'); |
|
134 | + } |
|
135 | + |
|
136 | + $this->cache($comment); |
|
137 | + |
|
138 | + return $comment; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * returns the topmost parent id of a given comment identified by ID |
|
143 | + * |
|
144 | + * @param string $id |
|
145 | + * @return string |
|
146 | + * @throws NotFoundException |
|
147 | + */ |
|
148 | + protected function determineTopmostParentId($id) { |
|
149 | + $comment = $this->get($id); |
|
150 | + if ($comment->getParentId() === '0') { |
|
151 | + return $comment->getId(); |
|
152 | + } else { |
|
153 | + return $this->determineTopmostParentId($comment->getId()); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * updates child information of a comment |
|
159 | + * |
|
160 | + * @param string $id |
|
161 | + * @param \DateTime $cDateTime the date time of the most recent child |
|
162 | + * @throws NotFoundException |
|
163 | + */ |
|
164 | + protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
|
165 | + $qb = $this->dbConn->getQueryBuilder(); |
|
166 | + $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
167 | + ->from('comments') |
|
168 | + ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
|
169 | + ->setParameter('id', $id); |
|
170 | + |
|
171 | + $resultStatement = $query->execute(); |
|
172 | + $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
173 | + $resultStatement->closeCursor(); |
|
174 | + $children = (int)$data[0]; |
|
175 | + |
|
176 | + $comment = $this->get($id); |
|
177 | + $comment->setChildrenCount($children); |
|
178 | + $comment->setLatestChildDateTime($cDateTime); |
|
179 | + $this->save($comment); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Tests whether actor or object type and id parameters are acceptable. |
|
184 | + * Throws exception if not. |
|
185 | + * |
|
186 | + * @param string $role |
|
187 | + * @param string $type |
|
188 | + * @param string $id |
|
189 | + * @throws \InvalidArgumentException |
|
190 | + */ |
|
191 | + protected function checkRoleParameters($role, $type, $id) { |
|
192 | + if ( |
|
193 | + !is_string($type) || empty($type) |
|
194 | + || !is_string($id) || empty($id) |
|
195 | + ) { |
|
196 | + throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * run-time caches a comment |
|
202 | + * |
|
203 | + * @param IComment $comment |
|
204 | + */ |
|
205 | + protected function cache(IComment $comment) { |
|
206 | + $id = $comment->getId(); |
|
207 | + if (empty($id)) { |
|
208 | + return; |
|
209 | + } |
|
210 | + $this->commentsCache[(string)$id] = $comment; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * removes an entry from the comments run time cache |
|
215 | + * |
|
216 | + * @param mixed $id the comment's id |
|
217 | + */ |
|
218 | + protected function uncache($id) { |
|
219 | + $id = (string)$id; |
|
220 | + if (isset($this->commentsCache[$id])) { |
|
221 | + unset($this->commentsCache[$id]); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * returns a comment instance |
|
227 | + * |
|
228 | + * @param string $id the ID of the comment |
|
229 | + * @return IComment |
|
230 | + * @throws NotFoundException |
|
231 | + * @throws \InvalidArgumentException |
|
232 | + * @since 9.0.0 |
|
233 | + */ |
|
234 | + public function get($id) { |
|
235 | + if ((int)$id === 0) { |
|
236 | + throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
|
237 | + } |
|
238 | + |
|
239 | + if (isset($this->commentsCache[$id])) { |
|
240 | + return $this->commentsCache[$id]; |
|
241 | + } |
|
242 | + |
|
243 | + $qb = $this->dbConn->getQueryBuilder(); |
|
244 | + $resultStatement = $qb->select('*') |
|
245 | + ->from('comments') |
|
246 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
247 | + ->setParameter('id', $id, IQueryBuilder::PARAM_INT) |
|
248 | + ->execute(); |
|
249 | + |
|
250 | + $data = $resultStatement->fetch(); |
|
251 | + $resultStatement->closeCursor(); |
|
252 | + if (!$data) { |
|
253 | + throw new NotFoundException(); |
|
254 | + } |
|
255 | + |
|
256 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
257 | + $this->cache($comment); |
|
258 | + return $comment; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * returns the comment specified by the id and all it's child comments. |
|
263 | + * At this point of time, we do only support one level depth. |
|
264 | + * |
|
265 | + * @param string $id |
|
266 | + * @param int $limit max number of entries to return, 0 returns all |
|
267 | + * @param int $offset the start entry |
|
268 | + * @return array |
|
269 | + * @since 9.0.0 |
|
270 | + * |
|
271 | + * The return array looks like this |
|
272 | + * [ |
|
273 | + * 'comment' => IComment, // root comment |
|
274 | + * 'replies' => |
|
275 | + * [ |
|
276 | + * 0 => |
|
277 | + * [ |
|
278 | + * 'comment' => IComment, |
|
279 | + * 'replies' => [] |
|
280 | + * ] |
|
281 | + * 1 => |
|
282 | + * [ |
|
283 | + * 'comment' => IComment, |
|
284 | + * 'replies'=> [] |
|
285 | + * ], |
|
286 | + * … |
|
287 | + * ] |
|
288 | + * ] |
|
289 | + */ |
|
290 | + public function getTree($id, $limit = 0, $offset = 0) { |
|
291 | + $tree = []; |
|
292 | + $tree['comment'] = $this->get($id); |
|
293 | + $tree['replies'] = []; |
|
294 | + |
|
295 | + $qb = $this->dbConn->getQueryBuilder(); |
|
296 | + $query = $qb->select('*') |
|
297 | + ->from('comments') |
|
298 | + ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id'))) |
|
299 | + ->orderBy('creation_timestamp', 'DESC') |
|
300 | + ->setParameter('id', $id); |
|
301 | + |
|
302 | + if ($limit > 0) { |
|
303 | + $query->setMaxResults($limit); |
|
304 | + } |
|
305 | + if ($offset > 0) { |
|
306 | + $query->setFirstResult($offset); |
|
307 | + } |
|
308 | + |
|
309 | + $resultStatement = $query->execute(); |
|
310 | + while ($data = $resultStatement->fetch()) { |
|
311 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
312 | + $this->cache($comment); |
|
313 | + $tree['replies'][] = [ |
|
314 | + 'comment' => $comment, |
|
315 | + 'replies' => [] |
|
316 | + ]; |
|
317 | + } |
|
318 | + $resultStatement->closeCursor(); |
|
319 | + |
|
320 | + return $tree; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * returns comments for a specific object (e.g. a file). |
|
325 | + * |
|
326 | + * The sort order is always newest to oldest. |
|
327 | + * |
|
328 | + * @param string $objectType the object type, e.g. 'files' |
|
329 | + * @param string $objectId the id of the object |
|
330 | + * @param int $limit optional, number of maximum comments to be returned. if |
|
331 | + * not specified, all comments are returned. |
|
332 | + * @param int $offset optional, starting point |
|
333 | + * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
334 | + * that may be returned |
|
335 | + * @return IComment[] |
|
336 | + * @since 9.0.0 |
|
337 | + */ |
|
338 | + public function getForObject( |
|
339 | + $objectType, |
|
340 | + $objectId, |
|
341 | + $limit = 0, |
|
342 | + $offset = 0, |
|
343 | + \DateTime $notOlderThan = null |
|
344 | + ) { |
|
345 | + $comments = []; |
|
346 | + |
|
347 | + $qb = $this->dbConn->getQueryBuilder(); |
|
348 | + $query = $qb->select('*') |
|
349 | + ->from('comments') |
|
350 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
351 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
352 | + ->orderBy('creation_timestamp', 'DESC') |
|
353 | + ->setParameter('type', $objectType) |
|
354 | + ->setParameter('id', $objectId); |
|
355 | + |
|
356 | + if ($limit > 0) { |
|
357 | + $query->setMaxResults($limit); |
|
358 | + } |
|
359 | + if ($offset > 0) { |
|
360 | + $query->setFirstResult($offset); |
|
361 | + } |
|
362 | + if (!is_null($notOlderThan)) { |
|
363 | + $query |
|
364 | + ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
365 | + ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
366 | + } |
|
367 | + |
|
368 | + $resultStatement = $query->execute(); |
|
369 | + while ($data = $resultStatement->fetch()) { |
|
370 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
371 | + $this->cache($comment); |
|
372 | + $comments[] = $comment; |
|
373 | + } |
|
374 | + $resultStatement->closeCursor(); |
|
375 | + |
|
376 | + return $comments; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * @param string $objectType the object type, e.g. 'files' |
|
381 | + * @param string $objectId the id of the object |
|
382 | + * @param int $lastKnownCommentId the last known comment (will be used as offset) |
|
383 | + * @param string $sortDirection direction of the comments (`asc` or `desc`) |
|
384 | + * @param int $limit optional, number of maximum comments to be returned. if |
|
385 | + * set to 0, all comments are returned. |
|
386 | + * @return IComment[] |
|
387 | + * @return array |
|
388 | + */ |
|
389 | + public function getForObjectSince( |
|
390 | + string $objectType, |
|
391 | + string $objectId, |
|
392 | + int $lastKnownCommentId, |
|
393 | + string $sortDirection = 'asc', |
|
394 | + int $limit = 30 |
|
395 | + ): array { |
|
396 | + $comments = []; |
|
397 | + |
|
398 | + $query = $this->dbConn->getQueryBuilder(); |
|
399 | + $query->select('*') |
|
400 | + ->from('comments') |
|
401 | + ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType))) |
|
402 | + ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))) |
|
403 | + ->orderBy('creation_timestamp', $sortDirection === 'desc' ? 'DESC' : 'ASC') |
|
404 | + ->addOrderBy('id', $sortDirection === 'desc' ? 'DESC' : 'ASC'); |
|
405 | + |
|
406 | + if ($limit > 0) { |
|
407 | + $query->setMaxResults($limit); |
|
408 | + } |
|
409 | + |
|
410 | + $lastKnownComment = $lastKnownCommentId > 0 ? $this->getLastKnownComment( |
|
411 | + $objectType, |
|
412 | + $objectId, |
|
413 | + $lastKnownCommentId |
|
414 | + ) : null; |
|
415 | + if ($lastKnownComment instanceof IComment) { |
|
416 | + $lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime(); |
|
417 | + if ($sortDirection === 'desc') { |
|
418 | + $query->andWhere( |
|
419 | + $query->expr()->orX( |
|
420 | + $query->expr()->lt( |
|
421 | + 'creation_timestamp', |
|
422 | + $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
423 | + IQueryBuilder::PARAM_DATE |
|
424 | + ), |
|
425 | + $query->expr()->andX( |
|
426 | + $query->expr()->eq( |
|
427 | + 'creation_timestamp', |
|
428 | + $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
429 | + IQueryBuilder::PARAM_DATE |
|
430 | + ), |
|
431 | + $query->expr()->lt('id', $query->createNamedParameter($lastKnownCommentId)) |
|
432 | + ) |
|
433 | + ) |
|
434 | + ); |
|
435 | + } else { |
|
436 | + $query->andWhere( |
|
437 | + $query->expr()->orX( |
|
438 | + $query->expr()->gt( |
|
439 | + 'creation_timestamp', |
|
440 | + $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
441 | + IQueryBuilder::PARAM_DATE |
|
442 | + ), |
|
443 | + $query->expr()->andX( |
|
444 | + $query->expr()->eq( |
|
445 | + 'creation_timestamp', |
|
446 | + $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), |
|
447 | + IQueryBuilder::PARAM_DATE |
|
448 | + ), |
|
449 | + $query->expr()->gt('id', $query->createNamedParameter($lastKnownCommentId)) |
|
450 | + ) |
|
451 | + ) |
|
452 | + ); |
|
453 | + } |
|
454 | + } |
|
455 | + |
|
456 | + $resultStatement = $query->execute(); |
|
457 | + while ($data = $resultStatement->fetch()) { |
|
458 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
459 | + $this->cache($comment); |
|
460 | + $comments[] = $comment; |
|
461 | + } |
|
462 | + $resultStatement->closeCursor(); |
|
463 | + |
|
464 | + return $comments; |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * @param string $objectType the object type, e.g. 'files' |
|
469 | + * @param string $objectId the id of the object |
|
470 | + * @param int $id the comment to look for |
|
471 | + * @return Comment|null |
|
472 | + */ |
|
473 | + protected function getLastKnownComment(string $objectType, |
|
474 | + string $objectId, |
|
475 | + int $id) { |
|
476 | + $query = $this->dbConn->getQueryBuilder(); |
|
477 | + $query->select('*') |
|
478 | + ->from('comments') |
|
479 | + ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType))) |
|
480 | + ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))) |
|
481 | + ->andWhere($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
482 | + |
|
483 | + $result = $query->execute(); |
|
484 | + $row = $result->fetch(); |
|
485 | + $result->closeCursor(); |
|
486 | + |
|
487 | + if ($row) { |
|
488 | + $comment = new Comment($this->normalizeDatabaseData($row)); |
|
489 | + $this->cache($comment); |
|
490 | + return $comment; |
|
491 | + } |
|
492 | + |
|
493 | + return null; |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Search for comments with a given content |
|
498 | + * |
|
499 | + * @param string $search content to search for |
|
500 | + * @param string $objectType Limit the search by object type |
|
501 | + * @param string $objectId Limit the search by object id |
|
502 | + * @param string $verb Limit the verb of the comment |
|
503 | + * @param int $offset |
|
504 | + * @param int $limit |
|
505 | + * @return IComment[] |
|
506 | + */ |
|
507 | + public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { |
|
508 | + $query = $this->dbConn->getQueryBuilder(); |
|
509 | + |
|
510 | + $query->select('*') |
|
511 | + ->from('comments') |
|
512 | + ->where($query->expr()->iLike('message', $query->createNamedParameter( |
|
513 | + '%' . $this->dbConn->escapeLikeParameter($search). '%' |
|
514 | + ))) |
|
515 | + ->orderBy('creation_timestamp', 'DESC') |
|
516 | + ->addOrderBy('id', 'DESC') |
|
517 | + ->setMaxResults($limit); |
|
518 | + |
|
519 | + if ($objectType !== '') { |
|
520 | + $query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); |
|
521 | + } |
|
522 | + if ($objectId !== '') { |
|
523 | + $query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); |
|
524 | + } |
|
525 | + if ($verb !== '') { |
|
526 | + $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); |
|
527 | + } |
|
528 | + if ($offset !== 0) { |
|
529 | + $query->setFirstResult($offset); |
|
530 | + } |
|
531 | + |
|
532 | + $comments = []; |
|
533 | + $result = $query->execute(); |
|
534 | + while ($data = $result->fetch()) { |
|
535 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
536 | + $this->cache($comment); |
|
537 | + $comments[] = $comment; |
|
538 | + } |
|
539 | + $result->closeCursor(); |
|
540 | + |
|
541 | + return $comments; |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * @param $objectType string the object type, e.g. 'files' |
|
546 | + * @param $objectId string the id of the object |
|
547 | + * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
548 | + * that may be returned |
|
549 | + * @param string $verb Limit the verb of the comment - Added in 14.0.0 |
|
550 | + * @return Int |
|
551 | + * @since 9.0.0 |
|
552 | + */ |
|
553 | + public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '') { |
|
554 | + $qb = $this->dbConn->getQueryBuilder(); |
|
555 | + $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
556 | + ->from('comments') |
|
557 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
558 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
559 | + ->setParameter('type', $objectType) |
|
560 | + ->setParameter('id', $objectId); |
|
561 | + |
|
562 | + if (!is_null($notOlderThan)) { |
|
563 | + $query |
|
564 | + ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
565 | + ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
566 | + } |
|
567 | + |
|
568 | + if ($verb !== '') { |
|
569 | + $query->andWhere($qb->expr()->eq('verb', $qb->createNamedParameter($verb))); |
|
570 | + } |
|
571 | + |
|
572 | + $resultStatement = $query->execute(); |
|
573 | + $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
574 | + $resultStatement->closeCursor(); |
|
575 | + return (int)$data[0]; |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * Get the number of unread comments for all files in a folder |
|
580 | + * |
|
581 | + * @param int $folderId |
|
582 | + * @param IUser $user |
|
583 | + * @return array [$fileId => $unreadCount] |
|
584 | + */ |
|
585 | + public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
|
586 | + $qb = $this->dbConn->getQueryBuilder(); |
|
587 | + $query = $qb->select('f.fileid') |
|
588 | + ->selectAlias( |
|
589 | + $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'), |
|
590 | + 'num_ids' |
|
591 | + ) |
|
592 | + ->from('comments', 'c') |
|
593 | + ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX( |
|
594 | + $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), |
|
595 | + $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)) |
|
596 | + )) |
|
597 | + ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX( |
|
598 | + $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')), |
|
599 | + $qb->expr()->eq('m.object_id', 'c.object_id'), |
|
600 | + $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())) |
|
601 | + )) |
|
602 | + ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId))) |
|
603 | + ->andWhere($qb->expr()->orX( |
|
604 | + $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'), |
|
605 | + $qb->expr()->isNull('marker_datetime') |
|
606 | + )) |
|
607 | + ->groupBy('f.fileid'); |
|
608 | + |
|
609 | + $resultStatement = $query->execute(); |
|
610 | + |
|
611 | + $results = []; |
|
612 | + while ($row = $resultStatement->fetch()) { |
|
613 | + $results[$row['fileid']] = (int) $row['num_ids']; |
|
614 | + } |
|
615 | + $resultStatement->closeCursor(); |
|
616 | + return $results; |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * creates a new comment and returns it. At this point of time, it is not |
|
621 | + * saved in the used data storage. Use save() after setting other fields |
|
622 | + * of the comment (e.g. message or verb). |
|
623 | + * |
|
624 | + * @param string $actorType the actor type (e.g. 'users') |
|
625 | + * @param string $actorId a user id |
|
626 | + * @param string $objectType the object type the comment is attached to |
|
627 | + * @param string $objectId the object id the comment is attached to |
|
628 | + * @return IComment |
|
629 | + * @since 9.0.0 |
|
630 | + */ |
|
631 | + public function create($actorType, $actorId, $objectType, $objectId) { |
|
632 | + $comment = new Comment(); |
|
633 | + $comment |
|
634 | + ->setActor($actorType, $actorId) |
|
635 | + ->setObject($objectType, $objectId); |
|
636 | + return $comment; |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * permanently deletes the comment specified by the ID |
|
641 | + * |
|
642 | + * When the comment has child comments, their parent ID will be changed to |
|
643 | + * the parent ID of the item that is to be deleted. |
|
644 | + * |
|
645 | + * @param string $id |
|
646 | + * @return bool |
|
647 | + * @throws \InvalidArgumentException |
|
648 | + * @since 9.0.0 |
|
649 | + */ |
|
650 | + public function delete($id) { |
|
651 | + if (!is_string($id)) { |
|
652 | + throw new \InvalidArgumentException('Parameter must be string'); |
|
653 | + } |
|
654 | + |
|
655 | + try { |
|
656 | + $comment = $this->get($id); |
|
657 | + } catch (\Exception $e) { |
|
658 | + // Ignore exceptions, we just don't fire a hook then |
|
659 | + $comment = null; |
|
660 | + } |
|
661 | + |
|
662 | + $qb = $this->dbConn->getQueryBuilder(); |
|
663 | + $query = $qb->delete('comments') |
|
664 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
665 | + ->setParameter('id', $id); |
|
666 | + |
|
667 | + try { |
|
668 | + $affectedRows = $query->execute(); |
|
669 | + $this->uncache($id); |
|
670 | + } catch (DriverException $e) { |
|
671 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
672 | + return false; |
|
673 | + } |
|
674 | + |
|
675 | + if ($affectedRows > 0 && $comment instanceof IComment) { |
|
676 | + $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment); |
|
677 | + } |
|
678 | + |
|
679 | + return ($affectedRows > 0); |
|
680 | + } |
|
681 | + |
|
682 | + /** |
|
683 | + * saves the comment permanently |
|
684 | + * |
|
685 | + * if the supplied comment has an empty ID, a new entry comment will be |
|
686 | + * saved and the instance updated with the new ID. |
|
687 | + * |
|
688 | + * Otherwise, an existing comment will be updated. |
|
689 | + * |
|
690 | + * Throws NotFoundException when a comment that is to be updated does not |
|
691 | + * exist anymore at this point of time. |
|
692 | + * |
|
693 | + * @param IComment $comment |
|
694 | + * @return bool |
|
695 | + * @throws NotFoundException |
|
696 | + * @since 9.0.0 |
|
697 | + */ |
|
698 | + public function save(IComment $comment) { |
|
699 | + if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') { |
|
700 | + $result = $this->insert($comment); |
|
701 | + } else { |
|
702 | + $result = $this->update($comment); |
|
703 | + } |
|
704 | + |
|
705 | + if ($result && !!$comment->getParentId()) { |
|
706 | + $this->updateChildrenInformation( |
|
707 | + $comment->getParentId(), |
|
708 | + $comment->getCreationDateTime() |
|
709 | + ); |
|
710 | + $this->cache($comment); |
|
711 | + } |
|
712 | + |
|
713 | + return $result; |
|
714 | + } |
|
715 | + |
|
716 | + /** |
|
717 | + * inserts the provided comment in the database |
|
718 | + * |
|
719 | + * @param IComment $comment |
|
720 | + * @return bool |
|
721 | + */ |
|
722 | + protected function insert(IComment &$comment) { |
|
723 | + $qb = $this->dbConn->getQueryBuilder(); |
|
724 | + $affectedRows = $qb |
|
725 | + ->insert('comments') |
|
726 | + ->values([ |
|
727 | + 'parent_id' => $qb->createNamedParameter($comment->getParentId()), |
|
728 | + 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), |
|
729 | + 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), |
|
730 | + 'actor_type' => $qb->createNamedParameter($comment->getActorType()), |
|
731 | + 'actor_id' => $qb->createNamedParameter($comment->getActorId()), |
|
732 | + 'message' => $qb->createNamedParameter($comment->getMessage()), |
|
733 | + 'verb' => $qb->createNamedParameter($comment->getVerb()), |
|
734 | + 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), |
|
735 | + 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), |
|
736 | + 'object_type' => $qb->createNamedParameter($comment->getObjectType()), |
|
737 | + 'object_id' => $qb->createNamedParameter($comment->getObjectId()), |
|
738 | + ]) |
|
739 | + ->execute(); |
|
740 | + |
|
741 | + if ($affectedRows > 0) { |
|
742 | + $comment->setId((string)$qb->getLastInsertId()); |
|
743 | + $this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
|
744 | + } |
|
745 | + |
|
746 | + return $affectedRows > 0; |
|
747 | + } |
|
748 | + |
|
749 | + /** |
|
750 | + * updates a Comment data row |
|
751 | + * |
|
752 | + * @param IComment $comment |
|
753 | + * @return bool |
|
754 | + * @throws NotFoundException |
|
755 | + */ |
|
756 | + protected function update(IComment $comment) { |
|
757 | + // for properly working preUpdate Events we need the old comments as is |
|
758 | + // in the DB and overcome caching. Also avoid that outdated information stays. |
|
759 | + $this->uncache($comment->getId()); |
|
760 | + $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); |
|
761 | + $this->uncache($comment->getId()); |
|
762 | + |
|
763 | + $qb = $this->dbConn->getQueryBuilder(); |
|
764 | + $affectedRows = $qb |
|
765 | + ->update('comments') |
|
766 | + ->set('parent_id', $qb->createNamedParameter($comment->getParentId())) |
|
767 | + ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) |
|
768 | + ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount())) |
|
769 | + ->set('actor_type', $qb->createNamedParameter($comment->getActorType())) |
|
770 | + ->set('actor_id', $qb->createNamedParameter($comment->getActorId())) |
|
771 | + ->set('message', $qb->createNamedParameter($comment->getMessage())) |
|
772 | + ->set('verb', $qb->createNamedParameter($comment->getVerb())) |
|
773 | + ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) |
|
774 | + ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) |
|
775 | + ->set('object_type', $qb->createNamedParameter($comment->getObjectType())) |
|
776 | + ->set('object_id', $qb->createNamedParameter($comment->getObjectId())) |
|
777 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
778 | + ->setParameter('id', $comment->getId()) |
|
779 | + ->execute(); |
|
780 | + |
|
781 | + if ($affectedRows === 0) { |
|
782 | + throw new NotFoundException('Comment to update does ceased to exist'); |
|
783 | + } |
|
784 | + |
|
785 | + $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); |
|
786 | + |
|
787 | + return $affectedRows > 0; |
|
788 | + } |
|
789 | + |
|
790 | + /** |
|
791 | + * removes references to specific actor (e.g. on user delete) of a comment. |
|
792 | + * The comment itself must not get lost/deleted. |
|
793 | + * |
|
794 | + * @param string $actorType the actor type (e.g. 'users') |
|
795 | + * @param string $actorId a user id |
|
796 | + * @return boolean |
|
797 | + * @since 9.0.0 |
|
798 | + */ |
|
799 | + public function deleteReferencesOfActor($actorType, $actorId) { |
|
800 | + $this->checkRoleParameters('Actor', $actorType, $actorId); |
|
801 | + |
|
802 | + $qb = $this->dbConn->getQueryBuilder(); |
|
803 | + $affectedRows = $qb |
|
804 | + ->update('comments') |
|
805 | + ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
806 | + ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
807 | + ->where($qb->expr()->eq('actor_type', $qb->createParameter('type'))) |
|
808 | + ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id'))) |
|
809 | + ->setParameter('type', $actorType) |
|
810 | + ->setParameter('id', $actorId) |
|
811 | + ->execute(); |
|
812 | + |
|
813 | + $this->commentsCache = []; |
|
814 | + |
|
815 | + return is_int($affectedRows); |
|
816 | + } |
|
817 | + |
|
818 | + /** |
|
819 | + * deletes all comments made of a specific object (e.g. on file delete) |
|
820 | + * |
|
821 | + * @param string $objectType the object type (e.g. 'files') |
|
822 | + * @param string $objectId e.g. the file id |
|
823 | + * @return boolean |
|
824 | + * @since 9.0.0 |
|
825 | + */ |
|
826 | + public function deleteCommentsAtObject($objectType, $objectId) { |
|
827 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
828 | + |
|
829 | + $qb = $this->dbConn->getQueryBuilder(); |
|
830 | + $affectedRows = $qb |
|
831 | + ->delete('comments') |
|
832 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
833 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
834 | + ->setParameter('type', $objectType) |
|
835 | + ->setParameter('id', $objectId) |
|
836 | + ->execute(); |
|
837 | + |
|
838 | + $this->commentsCache = []; |
|
839 | + |
|
840 | + return is_int($affectedRows); |
|
841 | + } |
|
842 | + |
|
843 | + /** |
|
844 | + * deletes the read markers for the specified user |
|
845 | + * |
|
846 | + * @param \OCP\IUser $user |
|
847 | + * @return bool |
|
848 | + * @since 9.0.0 |
|
849 | + */ |
|
850 | + public function deleteReadMarksFromUser(IUser $user) { |
|
851 | + $qb = $this->dbConn->getQueryBuilder(); |
|
852 | + $query = $qb->delete('comments_read_markers') |
|
853 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
854 | + ->setParameter('user_id', $user->getUID()); |
|
855 | + |
|
856 | + try { |
|
857 | + $affectedRows = $query->execute(); |
|
858 | + } catch (DriverException $e) { |
|
859 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
860 | + return false; |
|
861 | + } |
|
862 | + return ($affectedRows > 0); |
|
863 | + } |
|
864 | + |
|
865 | + /** |
|
866 | + * sets the read marker for a given file to the specified date for the |
|
867 | + * provided user |
|
868 | + * |
|
869 | + * @param string $objectType |
|
870 | + * @param string $objectId |
|
871 | + * @param \DateTime $dateTime |
|
872 | + * @param IUser $user |
|
873 | + * @since 9.0.0 |
|
874 | + * @suppress SqlInjectionChecker |
|
875 | + */ |
|
876 | + public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
|
877 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
878 | + |
|
879 | + $qb = $this->dbConn->getQueryBuilder(); |
|
880 | + $values = [ |
|
881 | + 'user_id' => $qb->createNamedParameter($user->getUID()), |
|
882 | + 'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'), |
|
883 | + 'object_type' => $qb->createNamedParameter($objectType), |
|
884 | + 'object_id' => $qb->createNamedParameter($objectId), |
|
885 | + ]; |
|
886 | + |
|
887 | + // Strategy: try to update, if this does not return affected rows, do an insert. |
|
888 | + $affectedRows = $qb |
|
889 | + ->update('comments_read_markers') |
|
890 | + ->set('user_id', $values['user_id']) |
|
891 | + ->set('marker_datetime', $values['marker_datetime']) |
|
892 | + ->set('object_type', $values['object_type']) |
|
893 | + ->set('object_id', $values['object_id']) |
|
894 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
895 | + ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
896 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
897 | + ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
898 | + ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
899 | + ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
900 | + ->execute(); |
|
901 | + |
|
902 | + if ($affectedRows > 0) { |
|
903 | + return; |
|
904 | + } |
|
905 | + |
|
906 | + $qb->insert('comments_read_markers') |
|
907 | + ->values($values) |
|
908 | + ->execute(); |
|
909 | + } |
|
910 | + |
|
911 | + /** |
|
912 | + * returns the read marker for a given file to the specified date for the |
|
913 | + * provided user. It returns null, when the marker is not present, i.e. |
|
914 | + * no comments were marked as read. |
|
915 | + * |
|
916 | + * @param string $objectType |
|
917 | + * @param string $objectId |
|
918 | + * @param IUser $user |
|
919 | + * @return \DateTime|null |
|
920 | + * @since 9.0.0 |
|
921 | + */ |
|
922 | + public function getReadMark($objectType, $objectId, IUser $user) { |
|
923 | + $qb = $this->dbConn->getQueryBuilder(); |
|
924 | + $resultStatement = $qb->select('marker_datetime') |
|
925 | + ->from('comments_read_markers') |
|
926 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
927 | + ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
928 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
929 | + ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
930 | + ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
931 | + ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
932 | + ->execute(); |
|
933 | + |
|
934 | + $data = $resultStatement->fetch(); |
|
935 | + $resultStatement->closeCursor(); |
|
936 | + if (!$data || is_null($data['marker_datetime'])) { |
|
937 | + return null; |
|
938 | + } |
|
939 | + |
|
940 | + return new \DateTime($data['marker_datetime']); |
|
941 | + } |
|
942 | + |
|
943 | + /** |
|
944 | + * deletes the read markers on the specified object |
|
945 | + * |
|
946 | + * @param string $objectType |
|
947 | + * @param string $objectId |
|
948 | + * @return bool |
|
949 | + * @since 9.0.0 |
|
950 | + */ |
|
951 | + public function deleteReadMarksOnObject($objectType, $objectId) { |
|
952 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
953 | + |
|
954 | + $qb = $this->dbConn->getQueryBuilder(); |
|
955 | + $query = $qb->delete('comments_read_markers') |
|
956 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
957 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
958 | + ->setParameter('object_type', $objectType) |
|
959 | + ->setParameter('object_id', $objectId); |
|
960 | + |
|
961 | + try { |
|
962 | + $affectedRows = $query->execute(); |
|
963 | + } catch (DriverException $e) { |
|
964 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
965 | + return false; |
|
966 | + } |
|
967 | + return ($affectedRows > 0); |
|
968 | + } |
|
969 | + |
|
970 | + /** |
|
971 | + * registers an Entity to the manager, so event notifications can be send |
|
972 | + * to consumers of the comments infrastructure |
|
973 | + * |
|
974 | + * @param \Closure $closure |
|
975 | + */ |
|
976 | + public function registerEventHandler(\Closure $closure) { |
|
977 | + $this->eventHandlerClosures[] = $closure; |
|
978 | + $this->eventHandlers = []; |
|
979 | + } |
|
980 | + |
|
981 | + /** |
|
982 | + * registers a method that resolves an ID to a display name for a given type |
|
983 | + * |
|
984 | + * @param string $type |
|
985 | + * @param \Closure $closure |
|
986 | + * @throws \OutOfBoundsException |
|
987 | + * @since 11.0.0 |
|
988 | + * |
|
989 | + * Only one resolver shall be registered per type. Otherwise a |
|
990 | + * \OutOfBoundsException has to thrown. |
|
991 | + */ |
|
992 | + public function registerDisplayNameResolver($type, \Closure $closure) { |
|
993 | + if (!is_string($type)) { |
|
994 | + throw new \InvalidArgumentException('String expected.'); |
|
995 | + } |
|
996 | + if (isset($this->displayNameResolvers[$type])) { |
|
997 | + throw new \OutOfBoundsException('Displayname resolver for this type already registered'); |
|
998 | + } |
|
999 | + $this->displayNameResolvers[$type] = $closure; |
|
1000 | + } |
|
1001 | + |
|
1002 | + /** |
|
1003 | + * resolves a given ID of a given Type to a display name. |
|
1004 | + * |
|
1005 | + * @param string $type |
|
1006 | + * @param string $id |
|
1007 | + * @return string |
|
1008 | + * @throws \OutOfBoundsException |
|
1009 | + * @since 11.0.0 |
|
1010 | + * |
|
1011 | + * If a provided type was not registered, an \OutOfBoundsException shall |
|
1012 | + * be thrown. It is upon the resolver discretion what to return of the |
|
1013 | + * provided ID is unknown. It must be ensured that a string is returned. |
|
1014 | + */ |
|
1015 | + public function resolveDisplayName($type, $id) { |
|
1016 | + if (!is_string($type)) { |
|
1017 | + throw new \InvalidArgumentException('String expected.'); |
|
1018 | + } |
|
1019 | + if (!isset($this->displayNameResolvers[$type])) { |
|
1020 | + throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
|
1021 | + } |
|
1022 | + return (string)$this->displayNameResolvers[$type]($id); |
|
1023 | + } |
|
1024 | + |
|
1025 | + /** |
|
1026 | + * returns valid, registered entities |
|
1027 | + * |
|
1028 | + * @return \OCP\Comments\ICommentsEventHandler[] |
|
1029 | + */ |
|
1030 | + private function getEventHandlers() { |
|
1031 | + if (!empty($this->eventHandlers)) { |
|
1032 | + return $this->eventHandlers; |
|
1033 | + } |
|
1034 | + |
|
1035 | + $this->eventHandlers = []; |
|
1036 | + foreach ($this->eventHandlerClosures as $name => $closure) { |
|
1037 | + $entity = $closure(); |
|
1038 | + if (!($entity instanceof ICommentsEventHandler)) { |
|
1039 | + throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface'); |
|
1040 | + } |
|
1041 | + $this->eventHandlers[$name] = $entity; |
|
1042 | + } |
|
1043 | + |
|
1044 | + return $this->eventHandlers; |
|
1045 | + } |
|
1046 | + |
|
1047 | + /** |
|
1048 | + * sends notifications to the registered entities |
|
1049 | + * |
|
1050 | + * @param $eventType |
|
1051 | + * @param IComment $comment |
|
1052 | + */ |
|
1053 | + private function sendEvent($eventType, IComment $comment) { |
|
1054 | + $entities = $this->getEventHandlers(); |
|
1055 | + $event = new CommentsEvent($eventType, $comment); |
|
1056 | + foreach ($entities as $entity) { |
|
1057 | + $entity->handle($event); |
|
1058 | + } |
|
1059 | + } |
|
1060 | 1060 | } |
@@ -87,14 +87,14 @@ discard block |
||
87 | 87 | * @return array |
88 | 88 | */ |
89 | 89 | protected function normalizeDatabaseData(array $data) { |
90 | - $data['id'] = (string)$data['id']; |
|
91 | - $data['parent_id'] = (string)$data['parent_id']; |
|
92 | - $data['topmost_parent_id'] = (string)$data['topmost_parent_id']; |
|
90 | + $data['id'] = (string) $data['id']; |
|
91 | + $data['parent_id'] = (string) $data['parent_id']; |
|
92 | + $data['topmost_parent_id'] = (string) $data['topmost_parent_id']; |
|
93 | 93 | $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
94 | 94 | if (!is_null($data['latest_child_timestamp'])) { |
95 | 95 | $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
96 | 96 | } |
97 | - $data['children_count'] = (int)$data['children_count']; |
|
97 | + $data['children_count'] = (int) $data['children_count']; |
|
98 | 98 | return $data; |
99 | 99 | } |
100 | 100 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | */ |
164 | 164 | protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
165 | 165 | $qb = $this->dbConn->getQueryBuilder(); |
166 | - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
166 | + $query = $qb->select($qb->createFunction('COUNT('.$qb->getColumnName('id').')')) |
|
167 | 167 | ->from('comments') |
168 | 168 | ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
169 | 169 | ->setParameter('id', $id); |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | $resultStatement = $query->execute(); |
172 | 172 | $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
173 | 173 | $resultStatement->closeCursor(); |
174 | - $children = (int)$data[0]; |
|
174 | + $children = (int) $data[0]; |
|
175 | 175 | |
176 | 176 | $comment = $this->get($id); |
177 | 177 | $comment->setChildrenCount($children); |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | !is_string($type) || empty($type) |
194 | 194 | || !is_string($id) || empty($id) |
195 | 195 | ) { |
196 | - throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
196 | + throw new \InvalidArgumentException($role.' parameters must be string and not empty'); |
|
197 | 197 | } |
198 | 198 | } |
199 | 199 | |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | if (empty($id)) { |
208 | 208 | return; |
209 | 209 | } |
210 | - $this->commentsCache[(string)$id] = $comment; |
|
210 | + $this->commentsCache[(string) $id] = $comment; |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | * @param mixed $id the comment's id |
217 | 217 | */ |
218 | 218 | protected function uncache($id) { |
219 | - $id = (string)$id; |
|
219 | + $id = (string) $id; |
|
220 | 220 | if (isset($this->commentsCache[$id])) { |
221 | 221 | unset($this->commentsCache[$id]); |
222 | 222 | } |
@@ -232,7 +232,7 @@ discard block |
||
232 | 232 | * @since 9.0.0 |
233 | 233 | */ |
234 | 234 | public function get($id) { |
235 | - if ((int)$id === 0) { |
|
235 | + if ((int) $id === 0) { |
|
236 | 236 | throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
237 | 237 | } |
238 | 238 | |
@@ -510,7 +510,7 @@ discard block |
||
510 | 510 | $query->select('*') |
511 | 511 | ->from('comments') |
512 | 512 | ->where($query->expr()->iLike('message', $query->createNamedParameter( |
513 | - '%' . $this->dbConn->escapeLikeParameter($search). '%' |
|
513 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
514 | 514 | ))) |
515 | 515 | ->orderBy('creation_timestamp', 'DESC') |
516 | 516 | ->addOrderBy('id', 'DESC') |
@@ -552,7 +552,7 @@ discard block |
||
552 | 552 | */ |
553 | 553 | public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '') { |
554 | 554 | $qb = $this->dbConn->getQueryBuilder(); |
555 | - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
555 | + $query = $qb->select($qb->createFunction('COUNT('.$qb->getColumnName('id').')')) |
|
556 | 556 | ->from('comments') |
557 | 557 | ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
558 | 558 | ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
@@ -572,7 +572,7 @@ discard block |
||
572 | 572 | $resultStatement = $query->execute(); |
573 | 573 | $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
574 | 574 | $resultStatement->closeCursor(); |
575 | - return (int)$data[0]; |
|
575 | + return (int) $data[0]; |
|
576 | 576 | } |
577 | 577 | |
578 | 578 | /** |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | $qb = $this->dbConn->getQueryBuilder(); |
587 | 587 | $query = $qb->select('f.fileid') |
588 | 588 | ->selectAlias( |
589 | - $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'), |
|
589 | + $qb->createFunction('COUNT('.$qb->getColumnName('c.id').')'), |
|
590 | 590 | 'num_ids' |
591 | 591 | ) |
592 | 592 | ->from('comments', 'c') |
@@ -739,7 +739,7 @@ discard block |
||
739 | 739 | ->execute(); |
740 | 740 | |
741 | 741 | if ($affectedRows > 0) { |
742 | - $comment->setId((string)$qb->getLastInsertId()); |
|
742 | + $comment->setId((string) $qb->getLastInsertId()); |
|
743 | 743 | $this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
744 | 744 | } |
745 | 745 | |
@@ -1019,7 +1019,7 @@ discard block |
||
1019 | 1019 | if (!isset($this->displayNameResolvers[$type])) { |
1020 | 1020 | throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
1021 | 1021 | } |
1022 | - return (string)$this->displayNameResolvers[$type]($id); |
|
1022 | + return (string) $this->displayNameResolvers[$type]($id); |
|
1023 | 1023 | } |
1024 | 1024 | |
1025 | 1025 | /** |
@@ -58,545 +58,545 @@ |
||
58 | 58 | * @package OC\User |
59 | 59 | */ |
60 | 60 | class Manager extends PublicEmitter implements IUserManager { |
61 | - /** |
|
62 | - * @var \OCP\UserInterface[] $backends |
|
63 | - */ |
|
64 | - private $backends = array(); |
|
65 | - |
|
66 | - /** |
|
67 | - * @var \OC\User\User[] $cachedUsers |
|
68 | - */ |
|
69 | - private $cachedUsers = array(); |
|
70 | - |
|
71 | - /** |
|
72 | - * @var \OCP\IConfig $config |
|
73 | - */ |
|
74 | - private $config; |
|
75 | - |
|
76 | - /** |
|
77 | - * @param \OCP\IConfig $config |
|
78 | - */ |
|
79 | - public function __construct(IConfig $config) { |
|
80 | - $this->config = $config; |
|
81 | - $cachedUsers = &$this->cachedUsers; |
|
82 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
83 | - /** @var \OC\User\User $user */ |
|
84 | - unset($cachedUsers[$user->getUID()]); |
|
85 | - }); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get the active backends |
|
90 | - * @return \OCP\UserInterface[] |
|
91 | - */ |
|
92 | - public function getBackends() { |
|
93 | - return $this->backends; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * register a user backend |
|
98 | - * |
|
99 | - * @param \OCP\UserInterface $backend |
|
100 | - */ |
|
101 | - public function registerBackend($backend) { |
|
102 | - $this->backends[] = $backend; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * remove a user backend |
|
107 | - * |
|
108 | - * @param \OCP\UserInterface $backend |
|
109 | - */ |
|
110 | - public function removeBackend($backend) { |
|
111 | - $this->cachedUsers = array(); |
|
112 | - if (($i = array_search($backend, $this->backends)) !== false) { |
|
113 | - unset($this->backends[$i]); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * remove all user backends |
|
119 | - */ |
|
120 | - public function clearBackends() { |
|
121 | - $this->cachedUsers = array(); |
|
122 | - $this->backends = array(); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * get a user by user id |
|
127 | - * |
|
128 | - * @param string $uid |
|
129 | - * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
130 | - */ |
|
131 | - public function get($uid) { |
|
132 | - if (is_null($uid) || $uid === '' || $uid === false) { |
|
133 | - return null; |
|
134 | - } |
|
135 | - if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
136 | - return $this->cachedUsers[$uid]; |
|
137 | - } |
|
138 | - foreach ($this->backends as $backend) { |
|
139 | - if ($backend->userExists($uid)) { |
|
140 | - return $this->getUserObject($uid, $backend); |
|
141 | - } |
|
142 | - } |
|
143 | - return null; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * get or construct the user object |
|
148 | - * |
|
149 | - * @param string $uid |
|
150 | - * @param \OCP\UserInterface $backend |
|
151 | - * @param bool $cacheUser If false the newly created user object will not be cached |
|
152 | - * @return \OC\User\User |
|
153 | - */ |
|
154 | - protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
155 | - if (isset($this->cachedUsers[$uid])) { |
|
156 | - return $this->cachedUsers[$uid]; |
|
157 | - } |
|
158 | - |
|
159 | - $user = new User($uid, $backend, $this, $this->config); |
|
160 | - if ($cacheUser) { |
|
161 | - $this->cachedUsers[$uid] = $user; |
|
162 | - } |
|
163 | - return $user; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * check if a user exists |
|
168 | - * |
|
169 | - * @param string $uid |
|
170 | - * @return bool |
|
171 | - */ |
|
172 | - public function userExists($uid) { |
|
173 | - $user = $this->get($uid); |
|
174 | - return ($user !== null); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Check if the password is valid for the user |
|
179 | - * |
|
180 | - * @param string $loginName |
|
181 | - * @param string $password |
|
182 | - * @return mixed the User object on success, false otherwise |
|
183 | - */ |
|
184 | - public function checkPassword($loginName, $password) { |
|
185 | - $result = $this->checkPasswordNoLogging($loginName, $password); |
|
186 | - |
|
187 | - if ($result === false) { |
|
188 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
189 | - } |
|
190 | - |
|
191 | - return $result; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Check if the password is valid for the user |
|
196 | - * |
|
197 | - * @internal |
|
198 | - * @param string $loginName |
|
199 | - * @param string $password |
|
200 | - * @return mixed the User object on success, false otherwise |
|
201 | - */ |
|
202 | - public function checkPasswordNoLogging($loginName, $password) { |
|
203 | - $loginName = str_replace("\0", '', $loginName); |
|
204 | - $password = str_replace("\0", '', $password); |
|
205 | - |
|
206 | - foreach ($this->backends as $backend) { |
|
207 | - if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
208 | - $uid = $backend->checkPassword($loginName, $password); |
|
209 | - if ($uid !== false) { |
|
210 | - return $this->getUserObject($uid, $backend); |
|
211 | - } |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - return false; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * search by user id |
|
220 | - * |
|
221 | - * @param string $pattern |
|
222 | - * @param int $limit |
|
223 | - * @param int $offset |
|
224 | - * @return \OC\User\User[] |
|
225 | - */ |
|
226 | - public function search($pattern, $limit = null, $offset = null) { |
|
227 | - $users = array(); |
|
228 | - foreach ($this->backends as $backend) { |
|
229 | - $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
230 | - if (is_array($backendUsers)) { |
|
231 | - foreach ($backendUsers as $uid) { |
|
232 | - $users[$uid] = $this->getUserObject($uid, $backend); |
|
233 | - } |
|
234 | - } |
|
235 | - } |
|
236 | - |
|
237 | - uasort($users, function ($a, $b) { |
|
238 | - /** |
|
239 | - * @var \OC\User\User $a |
|
240 | - * @var \OC\User\User $b |
|
241 | - */ |
|
242 | - return strcasecmp($a->getUID(), $b->getUID()); |
|
243 | - }); |
|
244 | - return $users; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * search by displayName |
|
249 | - * |
|
250 | - * @param string $pattern |
|
251 | - * @param int $limit |
|
252 | - * @param int $offset |
|
253 | - * @return \OC\User\User[] |
|
254 | - */ |
|
255 | - public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
256 | - $users = array(); |
|
257 | - foreach ($this->backends as $backend) { |
|
258 | - $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
259 | - if (is_array($backendUsers)) { |
|
260 | - foreach ($backendUsers as $uid => $displayName) { |
|
261 | - $users[] = $this->getUserObject($uid, $backend); |
|
262 | - } |
|
263 | - } |
|
264 | - } |
|
265 | - |
|
266 | - usort($users, function ($a, $b) { |
|
267 | - /** |
|
268 | - * @var \OC\User\User $a |
|
269 | - * @var \OC\User\User $b |
|
270 | - */ |
|
271 | - return strcasecmp($a->getDisplayName(), $b->getDisplayName()); |
|
272 | - }); |
|
273 | - return $users; |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * @param string $uid |
|
278 | - * @param string $password |
|
279 | - * @throws \InvalidArgumentException |
|
280 | - * @return bool|IUser the created user or false |
|
281 | - */ |
|
282 | - public function createUser($uid, $password) { |
|
283 | - $localBackends = []; |
|
284 | - foreach ($this->backends as $backend) { |
|
285 | - if ($backend instanceof Database) { |
|
286 | - // First check if there is another user backend |
|
287 | - $localBackends[] = $backend; |
|
288 | - continue; |
|
289 | - } |
|
290 | - |
|
291 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
292 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - foreach ($localBackends as $backend) { |
|
297 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
298 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - return false; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @param string $uid |
|
307 | - * @param string $password |
|
308 | - * @param UserInterface $backend |
|
309 | - * @return IUser|null |
|
310 | - * @throws \InvalidArgumentException |
|
311 | - */ |
|
312 | - public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
313 | - $l = \OC::$server->getL10N('lib'); |
|
314 | - |
|
315 | - // Check the name for bad characters |
|
316 | - // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
317 | - if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
318 | - throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
319 | - . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
320 | - } |
|
321 | - // No empty username |
|
322 | - if (trim($uid) === '') { |
|
323 | - throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
324 | - } |
|
325 | - // No whitespace at the beginning or at the end |
|
326 | - if (trim($uid) !== $uid) { |
|
327 | - throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
328 | - } |
|
329 | - // Username only consists of 1 or 2 dots (directory traversal) |
|
330 | - if ($uid === '.' || $uid === '..') { |
|
331 | - throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
332 | - } |
|
333 | - // No empty password |
|
334 | - if (trim($password) === '') { |
|
335 | - throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
336 | - } |
|
337 | - |
|
338 | - // Check if user already exists |
|
339 | - if ($this->userExists($uid)) { |
|
340 | - throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
341 | - } |
|
342 | - |
|
343 | - $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
344 | - $state = $backend->createUser($uid, $password); |
|
345 | - if($state === false) { |
|
346 | - throw new \InvalidArgumentException($l->t('Could not create user')); |
|
347 | - } |
|
348 | - $user = $this->getUserObject($uid, $backend); |
|
349 | - if ($user instanceof IUser) { |
|
350 | - $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
351 | - } |
|
352 | - return $user; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * returns how many users per backend exist (if supported by backend) |
|
357 | - * |
|
358 | - * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
359 | - * entry in the preferences table will be affected |
|
360 | - * @return array|int an array of backend class as key and count number as value |
|
361 | - * if $hasLoggedIn is true only an int is returned |
|
362 | - */ |
|
363 | - public function countUsers($hasLoggedIn = false) { |
|
364 | - if ($hasLoggedIn) { |
|
365 | - return $this->countSeenUsers(); |
|
366 | - } |
|
367 | - $userCountStatistics = []; |
|
368 | - foreach ($this->backends as $backend) { |
|
369 | - if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
370 | - $backendUsers = $backend->countUsers(); |
|
371 | - if($backendUsers !== false) { |
|
372 | - if($backend instanceof IUserBackend) { |
|
373 | - $name = $backend->getBackendName(); |
|
374 | - } else { |
|
375 | - $name = get_class($backend); |
|
376 | - } |
|
377 | - if(isset($userCountStatistics[$name])) { |
|
378 | - $userCountStatistics[$name] += $backendUsers; |
|
379 | - } else { |
|
380 | - $userCountStatistics[$name] = $backendUsers; |
|
381 | - } |
|
382 | - } |
|
383 | - } |
|
384 | - } |
|
385 | - return $userCountStatistics; |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * returns how many users per backend exist in the requested groups (if supported by backend) |
|
390 | - * |
|
391 | - * @param IGroup[] $groups an array of gid to search in |
|
392 | - * @return array|int an array of backend class as key and count number as value |
|
393 | - * if $hasLoggedIn is true only an int is returned |
|
394 | - */ |
|
395 | - public function countUsersOfGroups(array $groups) { |
|
396 | - $users = []; |
|
397 | - foreach($groups as $group) { |
|
398 | - $usersIds = array_map(function($user) { |
|
399 | - return $user->getUID(); |
|
400 | - }, $group->getUsers()); |
|
401 | - $users = array_merge($users, $usersIds); |
|
402 | - } |
|
403 | - return count(array_unique($users)); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * The callback is executed for each user on each backend. |
|
408 | - * If the callback returns false no further users will be retrieved. |
|
409 | - * |
|
410 | - * @param \Closure $callback |
|
411 | - * @param string $search |
|
412 | - * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
413 | - * in the preferences table will be affected |
|
414 | - * @since 9.0.0 |
|
415 | - */ |
|
416 | - public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
417 | - if ($onlySeen) { |
|
418 | - $this->callForSeenUsers($callback); |
|
419 | - } else { |
|
420 | - foreach ($this->getBackends() as $backend) { |
|
421 | - $limit = 500; |
|
422 | - $offset = 0; |
|
423 | - do { |
|
424 | - $users = $backend->getUsers($search, $limit, $offset); |
|
425 | - foreach ($users as $uid) { |
|
426 | - if (!$backend->userExists($uid)) { |
|
427 | - continue; |
|
428 | - } |
|
429 | - $user = $this->getUserObject($uid, $backend, false); |
|
430 | - $return = $callback($user); |
|
431 | - if ($return === false) { |
|
432 | - break; |
|
433 | - } |
|
434 | - } |
|
435 | - $offset += $limit; |
|
436 | - } while (count($users) >= $limit); |
|
437 | - } |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * returns how many users are disabled |
|
443 | - * |
|
444 | - * @return int |
|
445 | - * @since 12.0.0 |
|
446 | - */ |
|
447 | - public function countDisabledUsers(): int { |
|
448 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
449 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
450 | - ->from('preferences') |
|
451 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
452 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
453 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
61 | + /** |
|
62 | + * @var \OCP\UserInterface[] $backends |
|
63 | + */ |
|
64 | + private $backends = array(); |
|
65 | + |
|
66 | + /** |
|
67 | + * @var \OC\User\User[] $cachedUsers |
|
68 | + */ |
|
69 | + private $cachedUsers = array(); |
|
70 | + |
|
71 | + /** |
|
72 | + * @var \OCP\IConfig $config |
|
73 | + */ |
|
74 | + private $config; |
|
75 | + |
|
76 | + /** |
|
77 | + * @param \OCP\IConfig $config |
|
78 | + */ |
|
79 | + public function __construct(IConfig $config) { |
|
80 | + $this->config = $config; |
|
81 | + $cachedUsers = &$this->cachedUsers; |
|
82 | + $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
83 | + /** @var \OC\User\User $user */ |
|
84 | + unset($cachedUsers[$user->getUID()]); |
|
85 | + }); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get the active backends |
|
90 | + * @return \OCP\UserInterface[] |
|
91 | + */ |
|
92 | + public function getBackends() { |
|
93 | + return $this->backends; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * register a user backend |
|
98 | + * |
|
99 | + * @param \OCP\UserInterface $backend |
|
100 | + */ |
|
101 | + public function registerBackend($backend) { |
|
102 | + $this->backends[] = $backend; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * remove a user backend |
|
107 | + * |
|
108 | + * @param \OCP\UserInterface $backend |
|
109 | + */ |
|
110 | + public function removeBackend($backend) { |
|
111 | + $this->cachedUsers = array(); |
|
112 | + if (($i = array_search($backend, $this->backends)) !== false) { |
|
113 | + unset($this->backends[$i]); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * remove all user backends |
|
119 | + */ |
|
120 | + public function clearBackends() { |
|
121 | + $this->cachedUsers = array(); |
|
122 | + $this->backends = array(); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * get a user by user id |
|
127 | + * |
|
128 | + * @param string $uid |
|
129 | + * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
130 | + */ |
|
131 | + public function get($uid) { |
|
132 | + if (is_null($uid) || $uid === '' || $uid === false) { |
|
133 | + return null; |
|
134 | + } |
|
135 | + if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
136 | + return $this->cachedUsers[$uid]; |
|
137 | + } |
|
138 | + foreach ($this->backends as $backend) { |
|
139 | + if ($backend->userExists($uid)) { |
|
140 | + return $this->getUserObject($uid, $backend); |
|
141 | + } |
|
142 | + } |
|
143 | + return null; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * get or construct the user object |
|
148 | + * |
|
149 | + * @param string $uid |
|
150 | + * @param \OCP\UserInterface $backend |
|
151 | + * @param bool $cacheUser If false the newly created user object will not be cached |
|
152 | + * @return \OC\User\User |
|
153 | + */ |
|
154 | + protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
155 | + if (isset($this->cachedUsers[$uid])) { |
|
156 | + return $this->cachedUsers[$uid]; |
|
157 | + } |
|
158 | + |
|
159 | + $user = new User($uid, $backend, $this, $this->config); |
|
160 | + if ($cacheUser) { |
|
161 | + $this->cachedUsers[$uid] = $user; |
|
162 | + } |
|
163 | + return $user; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * check if a user exists |
|
168 | + * |
|
169 | + * @param string $uid |
|
170 | + * @return bool |
|
171 | + */ |
|
172 | + public function userExists($uid) { |
|
173 | + $user = $this->get($uid); |
|
174 | + return ($user !== null); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Check if the password is valid for the user |
|
179 | + * |
|
180 | + * @param string $loginName |
|
181 | + * @param string $password |
|
182 | + * @return mixed the User object on success, false otherwise |
|
183 | + */ |
|
184 | + public function checkPassword($loginName, $password) { |
|
185 | + $result = $this->checkPasswordNoLogging($loginName, $password); |
|
186 | + |
|
187 | + if ($result === false) { |
|
188 | + \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
189 | + } |
|
190 | + |
|
191 | + return $result; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Check if the password is valid for the user |
|
196 | + * |
|
197 | + * @internal |
|
198 | + * @param string $loginName |
|
199 | + * @param string $password |
|
200 | + * @return mixed the User object on success, false otherwise |
|
201 | + */ |
|
202 | + public function checkPasswordNoLogging($loginName, $password) { |
|
203 | + $loginName = str_replace("\0", '', $loginName); |
|
204 | + $password = str_replace("\0", '', $password); |
|
205 | + |
|
206 | + foreach ($this->backends as $backend) { |
|
207 | + if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
208 | + $uid = $backend->checkPassword($loginName, $password); |
|
209 | + if ($uid !== false) { |
|
210 | + return $this->getUserObject($uid, $backend); |
|
211 | + } |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + return false; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * search by user id |
|
220 | + * |
|
221 | + * @param string $pattern |
|
222 | + * @param int $limit |
|
223 | + * @param int $offset |
|
224 | + * @return \OC\User\User[] |
|
225 | + */ |
|
226 | + public function search($pattern, $limit = null, $offset = null) { |
|
227 | + $users = array(); |
|
228 | + foreach ($this->backends as $backend) { |
|
229 | + $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
230 | + if (is_array($backendUsers)) { |
|
231 | + foreach ($backendUsers as $uid) { |
|
232 | + $users[$uid] = $this->getUserObject($uid, $backend); |
|
233 | + } |
|
234 | + } |
|
235 | + } |
|
236 | + |
|
237 | + uasort($users, function ($a, $b) { |
|
238 | + /** |
|
239 | + * @var \OC\User\User $a |
|
240 | + * @var \OC\User\User $b |
|
241 | + */ |
|
242 | + return strcasecmp($a->getUID(), $b->getUID()); |
|
243 | + }); |
|
244 | + return $users; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * search by displayName |
|
249 | + * |
|
250 | + * @param string $pattern |
|
251 | + * @param int $limit |
|
252 | + * @param int $offset |
|
253 | + * @return \OC\User\User[] |
|
254 | + */ |
|
255 | + public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
256 | + $users = array(); |
|
257 | + foreach ($this->backends as $backend) { |
|
258 | + $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
259 | + if (is_array($backendUsers)) { |
|
260 | + foreach ($backendUsers as $uid => $displayName) { |
|
261 | + $users[] = $this->getUserObject($uid, $backend); |
|
262 | + } |
|
263 | + } |
|
264 | + } |
|
265 | + |
|
266 | + usort($users, function ($a, $b) { |
|
267 | + /** |
|
268 | + * @var \OC\User\User $a |
|
269 | + * @var \OC\User\User $b |
|
270 | + */ |
|
271 | + return strcasecmp($a->getDisplayName(), $b->getDisplayName()); |
|
272 | + }); |
|
273 | + return $users; |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * @param string $uid |
|
278 | + * @param string $password |
|
279 | + * @throws \InvalidArgumentException |
|
280 | + * @return bool|IUser the created user or false |
|
281 | + */ |
|
282 | + public function createUser($uid, $password) { |
|
283 | + $localBackends = []; |
|
284 | + foreach ($this->backends as $backend) { |
|
285 | + if ($backend instanceof Database) { |
|
286 | + // First check if there is another user backend |
|
287 | + $localBackends[] = $backend; |
|
288 | + continue; |
|
289 | + } |
|
290 | + |
|
291 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
292 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + foreach ($localBackends as $backend) { |
|
297 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
298 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + return false; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @param string $uid |
|
307 | + * @param string $password |
|
308 | + * @param UserInterface $backend |
|
309 | + * @return IUser|null |
|
310 | + * @throws \InvalidArgumentException |
|
311 | + */ |
|
312 | + public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
313 | + $l = \OC::$server->getL10N('lib'); |
|
314 | + |
|
315 | + // Check the name for bad characters |
|
316 | + // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
317 | + if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
318 | + throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
319 | + . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
320 | + } |
|
321 | + // No empty username |
|
322 | + if (trim($uid) === '') { |
|
323 | + throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
324 | + } |
|
325 | + // No whitespace at the beginning or at the end |
|
326 | + if (trim($uid) !== $uid) { |
|
327 | + throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
328 | + } |
|
329 | + // Username only consists of 1 or 2 dots (directory traversal) |
|
330 | + if ($uid === '.' || $uid === '..') { |
|
331 | + throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
332 | + } |
|
333 | + // No empty password |
|
334 | + if (trim($password) === '') { |
|
335 | + throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
336 | + } |
|
337 | + |
|
338 | + // Check if user already exists |
|
339 | + if ($this->userExists($uid)) { |
|
340 | + throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
341 | + } |
|
342 | + |
|
343 | + $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
344 | + $state = $backend->createUser($uid, $password); |
|
345 | + if($state === false) { |
|
346 | + throw new \InvalidArgumentException($l->t('Could not create user')); |
|
347 | + } |
|
348 | + $user = $this->getUserObject($uid, $backend); |
|
349 | + if ($user instanceof IUser) { |
|
350 | + $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
351 | + } |
|
352 | + return $user; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * returns how many users per backend exist (if supported by backend) |
|
357 | + * |
|
358 | + * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
359 | + * entry in the preferences table will be affected |
|
360 | + * @return array|int an array of backend class as key and count number as value |
|
361 | + * if $hasLoggedIn is true only an int is returned |
|
362 | + */ |
|
363 | + public function countUsers($hasLoggedIn = false) { |
|
364 | + if ($hasLoggedIn) { |
|
365 | + return $this->countSeenUsers(); |
|
366 | + } |
|
367 | + $userCountStatistics = []; |
|
368 | + foreach ($this->backends as $backend) { |
|
369 | + if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
370 | + $backendUsers = $backend->countUsers(); |
|
371 | + if($backendUsers !== false) { |
|
372 | + if($backend instanceof IUserBackend) { |
|
373 | + $name = $backend->getBackendName(); |
|
374 | + } else { |
|
375 | + $name = get_class($backend); |
|
376 | + } |
|
377 | + if(isset($userCountStatistics[$name])) { |
|
378 | + $userCountStatistics[$name] += $backendUsers; |
|
379 | + } else { |
|
380 | + $userCountStatistics[$name] = $backendUsers; |
|
381 | + } |
|
382 | + } |
|
383 | + } |
|
384 | + } |
|
385 | + return $userCountStatistics; |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * returns how many users per backend exist in the requested groups (if supported by backend) |
|
390 | + * |
|
391 | + * @param IGroup[] $groups an array of gid to search in |
|
392 | + * @return array|int an array of backend class as key and count number as value |
|
393 | + * if $hasLoggedIn is true only an int is returned |
|
394 | + */ |
|
395 | + public function countUsersOfGroups(array $groups) { |
|
396 | + $users = []; |
|
397 | + foreach($groups as $group) { |
|
398 | + $usersIds = array_map(function($user) { |
|
399 | + return $user->getUID(); |
|
400 | + }, $group->getUsers()); |
|
401 | + $users = array_merge($users, $usersIds); |
|
402 | + } |
|
403 | + return count(array_unique($users)); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * The callback is executed for each user on each backend. |
|
408 | + * If the callback returns false no further users will be retrieved. |
|
409 | + * |
|
410 | + * @param \Closure $callback |
|
411 | + * @param string $search |
|
412 | + * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
413 | + * in the preferences table will be affected |
|
414 | + * @since 9.0.0 |
|
415 | + */ |
|
416 | + public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
417 | + if ($onlySeen) { |
|
418 | + $this->callForSeenUsers($callback); |
|
419 | + } else { |
|
420 | + foreach ($this->getBackends() as $backend) { |
|
421 | + $limit = 500; |
|
422 | + $offset = 0; |
|
423 | + do { |
|
424 | + $users = $backend->getUsers($search, $limit, $offset); |
|
425 | + foreach ($users as $uid) { |
|
426 | + if (!$backend->userExists($uid)) { |
|
427 | + continue; |
|
428 | + } |
|
429 | + $user = $this->getUserObject($uid, $backend, false); |
|
430 | + $return = $callback($user); |
|
431 | + if ($return === false) { |
|
432 | + break; |
|
433 | + } |
|
434 | + } |
|
435 | + $offset += $limit; |
|
436 | + } while (count($users) >= $limit); |
|
437 | + } |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * returns how many users are disabled |
|
443 | + * |
|
444 | + * @return int |
|
445 | + * @since 12.0.0 |
|
446 | + */ |
|
447 | + public function countDisabledUsers(): int { |
|
448 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
449 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
450 | + ->from('preferences') |
|
451 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
452 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
453 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
454 | 454 | |
455 | 455 | |
456 | - $result = $queryBuilder->execute(); |
|
457 | - $count = $result->fetchColumn(); |
|
458 | - $result->closeCursor(); |
|
456 | + $result = $queryBuilder->execute(); |
|
457 | + $count = $result->fetchColumn(); |
|
458 | + $result->closeCursor(); |
|
459 | 459 | |
460 | - if ($count !== false) { |
|
461 | - $count = (int)$count; |
|
462 | - } else { |
|
463 | - $count = 0; |
|
464 | - } |
|
465 | - |
|
466 | - return $count; |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * returns how many users are disabled in the requested groups |
|
471 | - * |
|
472 | - * @param array $groups groupids to search |
|
473 | - * @return int |
|
474 | - * @since 14.0.0 |
|
475 | - */ |
|
476 | - public function countDisabledUsersOfGroups(array $groups): int { |
|
477 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
478 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(DISTINCT ' . $queryBuilder->getColumnName('uid') . ')')) |
|
479 | - ->from('preferences', 'p') |
|
480 | - ->innerJoin('p', 'group_user', 'g', $queryBuilder->expr()->eq('p.userid', 'g.uid')) |
|
481 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
482 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
483 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
484 | - ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY))); |
|
485 | - |
|
486 | - $result = $queryBuilder->execute(); |
|
487 | - $count = $result->fetchColumn(); |
|
488 | - $result->closeCursor(); |
|
460 | + if ($count !== false) { |
|
461 | + $count = (int)$count; |
|
462 | + } else { |
|
463 | + $count = 0; |
|
464 | + } |
|
465 | + |
|
466 | + return $count; |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * returns how many users are disabled in the requested groups |
|
471 | + * |
|
472 | + * @param array $groups groupids to search |
|
473 | + * @return int |
|
474 | + * @since 14.0.0 |
|
475 | + */ |
|
476 | + public function countDisabledUsersOfGroups(array $groups): int { |
|
477 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
478 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(DISTINCT ' . $queryBuilder->getColumnName('uid') . ')')) |
|
479 | + ->from('preferences', 'p') |
|
480 | + ->innerJoin('p', 'group_user', 'g', $queryBuilder->expr()->eq('p.userid', 'g.uid')) |
|
481 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
482 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
483 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
484 | + ->andWhere($queryBuilder->expr()->in('gid', $queryBuilder->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY))); |
|
485 | + |
|
486 | + $result = $queryBuilder->execute(); |
|
487 | + $count = $result->fetchColumn(); |
|
488 | + $result->closeCursor(); |
|
489 | 489 | |
490 | - if ($count !== false) { |
|
491 | - $count = (int)$count; |
|
492 | - } else { |
|
493 | - $count = 0; |
|
494 | - } |
|
495 | - |
|
496 | - return $count; |
|
497 | - } |
|
498 | - |
|
499 | - /** |
|
500 | - * returns how many users have logged in once |
|
501 | - * |
|
502 | - * @return int |
|
503 | - * @since 11.0.0 |
|
504 | - */ |
|
505 | - public function countSeenUsers() { |
|
506 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
507 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
508 | - ->from('preferences') |
|
509 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
510 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
511 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
512 | - |
|
513 | - $query = $queryBuilder->execute(); |
|
514 | - |
|
515 | - $result = (int)$query->fetchColumn(); |
|
516 | - $query->closeCursor(); |
|
517 | - |
|
518 | - return $result; |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * @param \Closure $callback |
|
523 | - * @since 11.0.0 |
|
524 | - */ |
|
525 | - public function callForSeenUsers(\Closure $callback) { |
|
526 | - $limit = 1000; |
|
527 | - $offset = 0; |
|
528 | - do { |
|
529 | - $userIds = $this->getSeenUserIds($limit, $offset); |
|
530 | - $offset += $limit; |
|
531 | - foreach ($userIds as $userId) { |
|
532 | - foreach ($this->backends as $backend) { |
|
533 | - if ($backend->userExists($userId)) { |
|
534 | - $user = $this->getUserObject($userId, $backend, false); |
|
535 | - $return = $callback($user); |
|
536 | - if ($return === false) { |
|
537 | - return; |
|
538 | - } |
|
539 | - break; |
|
540 | - } |
|
541 | - } |
|
542 | - } |
|
543 | - } while (count($userIds) >= $limit); |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Getting all userIds that have a listLogin value requires checking the |
|
548 | - * value in php because on oracle you cannot use a clob in a where clause, |
|
549 | - * preventing us from doing a not null or length(value) > 0 check. |
|
550 | - * |
|
551 | - * @param int $limit |
|
552 | - * @param int $offset |
|
553 | - * @return string[] with user ids |
|
554 | - */ |
|
555 | - private function getSeenUserIds($limit = null, $offset = null) { |
|
556 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
557 | - $queryBuilder->select(['userid']) |
|
558 | - ->from('preferences') |
|
559 | - ->where($queryBuilder->expr()->eq( |
|
560 | - 'appid', $queryBuilder->createNamedParameter('login')) |
|
561 | - ) |
|
562 | - ->andWhere($queryBuilder->expr()->eq( |
|
563 | - 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
564 | - ) |
|
565 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
566 | - ); |
|
567 | - |
|
568 | - if ($limit !== null) { |
|
569 | - $queryBuilder->setMaxResults($limit); |
|
570 | - } |
|
571 | - if ($offset !== null) { |
|
572 | - $queryBuilder->setFirstResult($offset); |
|
573 | - } |
|
574 | - $query = $queryBuilder->execute(); |
|
575 | - $result = []; |
|
576 | - |
|
577 | - while ($row = $query->fetch()) { |
|
578 | - $result[] = $row['userid']; |
|
579 | - } |
|
580 | - |
|
581 | - $query->closeCursor(); |
|
582 | - |
|
583 | - return $result; |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * @param string $email |
|
588 | - * @return IUser[] |
|
589 | - * @since 9.1.0 |
|
590 | - */ |
|
591 | - public function getByEmail($email) { |
|
592 | - $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
593 | - |
|
594 | - $users = array_map(function($uid) { |
|
595 | - return $this->get($uid); |
|
596 | - }, $userIds); |
|
597 | - |
|
598 | - return array_values(array_filter($users, function($u) { |
|
599 | - return ($u instanceof IUser); |
|
600 | - })); |
|
601 | - } |
|
490 | + if ($count !== false) { |
|
491 | + $count = (int)$count; |
|
492 | + } else { |
|
493 | + $count = 0; |
|
494 | + } |
|
495 | + |
|
496 | + return $count; |
|
497 | + } |
|
498 | + |
|
499 | + /** |
|
500 | + * returns how many users have logged in once |
|
501 | + * |
|
502 | + * @return int |
|
503 | + * @since 11.0.0 |
|
504 | + */ |
|
505 | + public function countSeenUsers() { |
|
506 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
507 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
508 | + ->from('preferences') |
|
509 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
510 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
511 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
512 | + |
|
513 | + $query = $queryBuilder->execute(); |
|
514 | + |
|
515 | + $result = (int)$query->fetchColumn(); |
|
516 | + $query->closeCursor(); |
|
517 | + |
|
518 | + return $result; |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * @param \Closure $callback |
|
523 | + * @since 11.0.0 |
|
524 | + */ |
|
525 | + public function callForSeenUsers(\Closure $callback) { |
|
526 | + $limit = 1000; |
|
527 | + $offset = 0; |
|
528 | + do { |
|
529 | + $userIds = $this->getSeenUserIds($limit, $offset); |
|
530 | + $offset += $limit; |
|
531 | + foreach ($userIds as $userId) { |
|
532 | + foreach ($this->backends as $backend) { |
|
533 | + if ($backend->userExists($userId)) { |
|
534 | + $user = $this->getUserObject($userId, $backend, false); |
|
535 | + $return = $callback($user); |
|
536 | + if ($return === false) { |
|
537 | + return; |
|
538 | + } |
|
539 | + break; |
|
540 | + } |
|
541 | + } |
|
542 | + } |
|
543 | + } while (count($userIds) >= $limit); |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Getting all userIds that have a listLogin value requires checking the |
|
548 | + * value in php because on oracle you cannot use a clob in a where clause, |
|
549 | + * preventing us from doing a not null or length(value) > 0 check. |
|
550 | + * |
|
551 | + * @param int $limit |
|
552 | + * @param int $offset |
|
553 | + * @return string[] with user ids |
|
554 | + */ |
|
555 | + private function getSeenUserIds($limit = null, $offset = null) { |
|
556 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
557 | + $queryBuilder->select(['userid']) |
|
558 | + ->from('preferences') |
|
559 | + ->where($queryBuilder->expr()->eq( |
|
560 | + 'appid', $queryBuilder->createNamedParameter('login')) |
|
561 | + ) |
|
562 | + ->andWhere($queryBuilder->expr()->eq( |
|
563 | + 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
564 | + ) |
|
565 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
566 | + ); |
|
567 | + |
|
568 | + if ($limit !== null) { |
|
569 | + $queryBuilder->setMaxResults($limit); |
|
570 | + } |
|
571 | + if ($offset !== null) { |
|
572 | + $queryBuilder->setFirstResult($offset); |
|
573 | + } |
|
574 | + $query = $queryBuilder->execute(); |
|
575 | + $result = []; |
|
576 | + |
|
577 | + while ($row = $query->fetch()) { |
|
578 | + $result[] = $row['userid']; |
|
579 | + } |
|
580 | + |
|
581 | + $query->closeCursor(); |
|
582 | + |
|
583 | + return $result; |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * @param string $email |
|
588 | + * @return IUser[] |
|
589 | + * @since 9.1.0 |
|
590 | + */ |
|
591 | + public function getByEmail($email) { |
|
592 | + $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
593 | + |
|
594 | + $users = array_map(function($uid) { |
|
595 | + return $this->get($uid); |
|
596 | + }, $userIds); |
|
597 | + |
|
598 | + return array_values(array_filter($users, function($u) { |
|
599 | + return ($u instanceof IUser); |
|
600 | + })); |
|
601 | + } |
|
602 | 602 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | public function __construct(IConfig $config) { |
80 | 80 | $this->config = $config; |
81 | 81 | $cachedUsers = &$this->cachedUsers; |
82 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
82 | + $this->listen('\OC\User', 'postDelete', function($user) use (&$cachedUsers) { |
|
83 | 83 | /** @var \OC\User\User $user */ |
84 | 84 | unset($cachedUsers[$user->getUID()]); |
85 | 85 | }); |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | $result = $this->checkPasswordNoLogging($loginName, $password); |
186 | 186 | |
187 | 187 | if ($result === false) { |
188 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
188 | + \OC::$server->getLogger()->warning('Login failed: \''.$loginName.'\' (Remote IP: \''.\OC::$server->getRequest()->getRemoteAddress().'\')', ['app' => 'core']); |
|
189 | 189 | } |
190 | 190 | |
191 | 191 | return $result; |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | } |
235 | 235 | } |
236 | 236 | |
237 | - uasort($users, function ($a, $b) { |
|
237 | + uasort($users, function($a, $b) { |
|
238 | 238 | /** |
239 | 239 | * @var \OC\User\User $a |
240 | 240 | * @var \OC\User\User $b |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | } |
264 | 264 | } |
265 | 265 | |
266 | - usort($users, function ($a, $b) { |
|
266 | + usort($users, function($a, $b) { |
|
267 | 267 | /** |
268 | 268 | * @var \OC\User\User $a |
269 | 269 | * @var \OC\User\User $b |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | |
343 | 343 | $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
344 | 344 | $state = $backend->createUser($uid, $password); |
345 | - if($state === false) { |
|
345 | + if ($state === false) { |
|
346 | 346 | throw new \InvalidArgumentException($l->t('Could not create user')); |
347 | 347 | } |
348 | 348 | $user = $this->getUserObject($uid, $backend); |
@@ -368,13 +368,13 @@ discard block |
||
368 | 368 | foreach ($this->backends as $backend) { |
369 | 369 | if ($backend->implementsActions(Backend::COUNT_USERS)) { |
370 | 370 | $backendUsers = $backend->countUsers(); |
371 | - if($backendUsers !== false) { |
|
372 | - if($backend instanceof IUserBackend) { |
|
371 | + if ($backendUsers !== false) { |
|
372 | + if ($backend instanceof IUserBackend) { |
|
373 | 373 | $name = $backend->getBackendName(); |
374 | 374 | } else { |
375 | 375 | $name = get_class($backend); |
376 | 376 | } |
377 | - if(isset($userCountStatistics[$name])) { |
|
377 | + if (isset($userCountStatistics[$name])) { |
|
378 | 378 | $userCountStatistics[$name] += $backendUsers; |
379 | 379 | } else { |
380 | 380 | $userCountStatistics[$name] = $backendUsers; |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | */ |
395 | 395 | public function countUsersOfGroups(array $groups) { |
396 | 396 | $users = []; |
397 | - foreach($groups as $group) { |
|
397 | + foreach ($groups as $group) { |
|
398 | 398 | $usersIds = array_map(function($user) { |
399 | 399 | return $user->getUID(); |
400 | 400 | }, $group->getUsers()); |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | $result->closeCursor(); |
459 | 459 | |
460 | 460 | if ($count !== false) { |
461 | - $count = (int)$count; |
|
461 | + $count = (int) $count; |
|
462 | 462 | } else { |
463 | 463 | $count = 0; |
464 | 464 | } |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | */ |
476 | 476 | public function countDisabledUsersOfGroups(array $groups): int { |
477 | 477 | $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
478 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(DISTINCT ' . $queryBuilder->getColumnName('uid') . ')')) |
|
478 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(DISTINCT '.$queryBuilder->getColumnName('uid').')')) |
|
479 | 479 | ->from('preferences', 'p') |
480 | 480 | ->innerJoin('p', 'group_user', 'g', $queryBuilder->expr()->eq('p.userid', 'g.uid')) |
481 | 481 | ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
@@ -488,7 +488,7 @@ discard block |
||
488 | 488 | $result->closeCursor(); |
489 | 489 | |
490 | 490 | if ($count !== false) { |
491 | - $count = (int)$count; |
|
491 | + $count = (int) $count; |
|
492 | 492 | } else { |
493 | 493 | $count = 0; |
494 | 494 | } |
@@ -512,7 +512,7 @@ discard block |
||
512 | 512 | |
513 | 513 | $query = $queryBuilder->execute(); |
514 | 514 | |
515 | - $result = (int)$query->fetchColumn(); |
|
515 | + $result = (int) $query->fetchColumn(); |
|
516 | 516 | $query->closeCursor(); |
517 | 517 | |
518 | 518 | return $result; |
@@ -55,357 +55,357 @@ |
||
55 | 55 | * Class for group management in a SQL Database (e.g. MySQL, SQLite) |
56 | 56 | */ |
57 | 57 | class Database extends ABackend |
58 | - implements IAddToGroupBackend, |
|
59 | - ICountDisabledInGroup, |
|
60 | - ICountUsersBackend, |
|
61 | - ICreateGroupBackend, |
|
62 | - IDeleteGroupBackend, |
|
63 | - IRemoveFromGroupBackend { |
|
64 | - |
|
65 | - /** @var string[] */ |
|
66 | - private $groupCache = []; |
|
67 | - |
|
68 | - /** @var IDBConnection */ |
|
69 | - private $dbConn; |
|
70 | - |
|
71 | - /** |
|
72 | - * \OC\Group\Database constructor. |
|
73 | - * |
|
74 | - * @param IDBConnection|null $dbConn |
|
75 | - */ |
|
76 | - public function __construct(IDBConnection $dbConn = null) { |
|
77 | - $this->dbConn = $dbConn; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * FIXME: This function should not be required! |
|
82 | - */ |
|
83 | - private function fixDI() { |
|
84 | - if ($this->dbConn === null) { |
|
85 | - $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Try to create a new group |
|
91 | - * @param string $gid The name of the group to create |
|
92 | - * @return bool |
|
93 | - * |
|
94 | - * Tries to create a new group. If the group name already exists, false will |
|
95 | - * be returned. |
|
96 | - */ |
|
97 | - public function createGroup(string $gid): bool { |
|
98 | - $this->fixDI(); |
|
99 | - |
|
100 | - // Add group |
|
101 | - $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
102 | - 'gid' => $gid, |
|
103 | - ]); |
|
104 | - |
|
105 | - // Add to cache |
|
106 | - $this->groupCache[$gid] = $gid; |
|
107 | - |
|
108 | - return $result === 1; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * delete a group |
|
113 | - * @param string $gid gid of the group to delete |
|
114 | - * @return bool |
|
115 | - * |
|
116 | - * Deletes a group and removes it from the group_user-table |
|
117 | - */ |
|
118 | - public function deleteGroup(string $gid): bool { |
|
119 | - $this->fixDI(); |
|
120 | - |
|
121 | - // Delete the group |
|
122 | - $qb = $this->dbConn->getQueryBuilder(); |
|
123 | - $qb->delete('groups') |
|
124 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
125 | - ->execute(); |
|
126 | - |
|
127 | - // Delete the group-user relation |
|
128 | - $qb = $this->dbConn->getQueryBuilder(); |
|
129 | - $qb->delete('group_user') |
|
130 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
131 | - ->execute(); |
|
132 | - |
|
133 | - // Delete the group-groupadmin relation |
|
134 | - $qb = $this->dbConn->getQueryBuilder(); |
|
135 | - $qb->delete('group_admin') |
|
136 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
137 | - ->execute(); |
|
138 | - |
|
139 | - // Delete from cache |
|
140 | - unset($this->groupCache[$gid]); |
|
141 | - |
|
142 | - return true; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * is user in group? |
|
147 | - * @param string $uid uid of the user |
|
148 | - * @param string $gid gid of the group |
|
149 | - * @return bool |
|
150 | - * |
|
151 | - * Checks whether the user is member of a group or not. |
|
152 | - */ |
|
153 | - public function inGroup( $uid, $gid ) { |
|
154 | - $this->fixDI(); |
|
155 | - |
|
156 | - // check |
|
157 | - $qb = $this->dbConn->getQueryBuilder(); |
|
158 | - $cursor = $qb->select('uid') |
|
159 | - ->from('group_user') |
|
160 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
161 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
162 | - ->execute(); |
|
163 | - |
|
164 | - $result = $cursor->fetch(); |
|
165 | - $cursor->closeCursor(); |
|
166 | - |
|
167 | - return $result ? true : false; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Add a user to a group |
|
172 | - * @param string $uid Name of the user to add to group |
|
173 | - * @param string $gid Name of the group in which add the user |
|
174 | - * @return bool |
|
175 | - * |
|
176 | - * Adds a user to a group. |
|
177 | - */ |
|
178 | - public function addToGroup(string $uid, string $gid): bool { |
|
179 | - $this->fixDI(); |
|
180 | - |
|
181 | - // No duplicate entries! |
|
182 | - if( !$this->inGroup( $uid, $gid )) { |
|
183 | - $qb = $this->dbConn->getQueryBuilder(); |
|
184 | - $qb->insert('group_user') |
|
185 | - ->setValue('uid', $qb->createNamedParameter($uid)) |
|
186 | - ->setValue('gid', $qb->createNamedParameter($gid)) |
|
187 | - ->execute(); |
|
188 | - return true; |
|
189 | - }else{ |
|
190 | - return false; |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Removes a user from a group |
|
196 | - * @param string $uid Name of the user to remove from group |
|
197 | - * @param string $gid Name of the group from which remove the user |
|
198 | - * @return bool |
|
199 | - * |
|
200 | - * removes the user from a group. |
|
201 | - */ |
|
202 | - public function removeFromGroup(string $uid, string $gid): bool { |
|
203 | - $this->fixDI(); |
|
204 | - |
|
205 | - $qb = $this->dbConn->getQueryBuilder(); |
|
206 | - $qb->delete('group_user') |
|
207 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
208 | - ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
209 | - ->execute(); |
|
210 | - |
|
211 | - return true; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Get all groups a user belongs to |
|
216 | - * @param string $uid Name of the user |
|
217 | - * @return array an array of group names |
|
218 | - * |
|
219 | - * This function fetches all groups a user belongs to. It does not check |
|
220 | - * if the user exists at all. |
|
221 | - */ |
|
222 | - public function getUserGroups( $uid ) { |
|
223 | - //guests has empty or null $uid |
|
224 | - if ($uid === null || $uid === '') { |
|
225 | - return []; |
|
226 | - } |
|
227 | - |
|
228 | - $this->fixDI(); |
|
229 | - |
|
230 | - // No magic! |
|
231 | - $qb = $this->dbConn->getQueryBuilder(); |
|
232 | - $cursor = $qb->select('gid') |
|
233 | - ->from('group_user') |
|
234 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
235 | - ->execute(); |
|
236 | - |
|
237 | - $groups = []; |
|
238 | - while( $row = $cursor->fetch()) { |
|
239 | - $groups[] = $row['gid']; |
|
240 | - $this->groupCache[$row['gid']] = $row['gid']; |
|
241 | - } |
|
242 | - $cursor->closeCursor(); |
|
243 | - |
|
244 | - return $groups; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * get a list of all groups |
|
249 | - * @param string $search |
|
250 | - * @param int $limit |
|
251 | - * @param int $offset |
|
252 | - * @return array an array of group names |
|
253 | - * |
|
254 | - * Returns a list with all groups |
|
255 | - */ |
|
256 | - public function getGroups($search = '', $limit = null, $offset = null) { |
|
257 | - $this->fixDI(); |
|
258 | - |
|
259 | - $query = $this->dbConn->getQueryBuilder(); |
|
260 | - $query->select('gid') |
|
261 | - ->from('groups') |
|
262 | - ->orderBy('gid', 'ASC'); |
|
263 | - |
|
264 | - if ($search !== '') { |
|
265 | - $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
266 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
267 | - ))); |
|
268 | - } |
|
269 | - |
|
270 | - $query->setMaxResults($limit) |
|
271 | - ->setFirstResult($offset); |
|
272 | - $result = $query->execute(); |
|
273 | - |
|
274 | - $groups = []; |
|
275 | - while ($row = $result->fetch()) { |
|
276 | - $groups[] = $row['gid']; |
|
277 | - } |
|
278 | - $result->closeCursor(); |
|
279 | - |
|
280 | - return $groups; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * check if a group exists |
|
285 | - * @param string $gid |
|
286 | - * @return bool |
|
287 | - */ |
|
288 | - public function groupExists($gid) { |
|
289 | - $this->fixDI(); |
|
290 | - |
|
291 | - // Check cache first |
|
292 | - if (isset($this->groupCache[$gid])) { |
|
293 | - return true; |
|
294 | - } |
|
295 | - |
|
296 | - $qb = $this->dbConn->getQueryBuilder(); |
|
297 | - $cursor = $qb->select('gid') |
|
298 | - ->from('groups') |
|
299 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
300 | - ->execute(); |
|
301 | - $result = $cursor->fetch(); |
|
302 | - $cursor->closeCursor(); |
|
303 | - |
|
304 | - if ($result !== false) { |
|
305 | - $this->groupCache[$gid] = $gid; |
|
306 | - return true; |
|
307 | - } |
|
308 | - return false; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * get a list of all users in a group |
|
313 | - * @param string $gid |
|
314 | - * @param string $search |
|
315 | - * @param int $limit |
|
316 | - * @param int $offset |
|
317 | - * @return array an array of user ids |
|
318 | - */ |
|
319 | - public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
320 | - $this->fixDI(); |
|
321 | - |
|
322 | - $query = $this->dbConn->getQueryBuilder(); |
|
323 | - $query->select('uid') |
|
324 | - ->from('group_user') |
|
325 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
326 | - ->orderBy('uid', 'ASC'); |
|
327 | - |
|
328 | - if ($search !== '') { |
|
329 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
330 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
331 | - ))); |
|
332 | - } |
|
333 | - |
|
334 | - $query->setMaxResults($limit) |
|
335 | - ->setFirstResult($offset); |
|
336 | - $result = $query->execute(); |
|
337 | - |
|
338 | - $users = []; |
|
339 | - while ($row = $result->fetch()) { |
|
340 | - $users[] = $row['uid']; |
|
341 | - } |
|
342 | - $result->closeCursor(); |
|
343 | - |
|
344 | - return $users; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * get the number of all users matching the search string in a group |
|
349 | - * @param string $gid |
|
350 | - * @param string $search |
|
351 | - * @return int |
|
352 | - */ |
|
353 | - public function countUsersInGroup(string $gid, string $search = ''): int { |
|
354 | - $this->fixDI(); |
|
355 | - |
|
356 | - $query = $this->dbConn->getQueryBuilder(); |
|
357 | - $query->selectAlias($query->createFunction('COUNT(*)'), 'num_users') |
|
358 | - ->from('group_user') |
|
359 | - ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
360 | - |
|
361 | - if ($search !== '') { |
|
362 | - $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
363 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
364 | - ))); |
|
365 | - } |
|
366 | - |
|
367 | - $result = $query->execute(); |
|
368 | - $count = $result->fetchColumn(); |
|
369 | - $result->closeCursor(); |
|
370 | - |
|
371 | - if ($count !== false) { |
|
372 | - $count = (int)$count; |
|
373 | - } else { |
|
374 | - $count = 0; |
|
375 | - } |
|
376 | - |
|
377 | - return $count; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * get the number of disabled users in a group |
|
382 | - * |
|
383 | - * @param string $search |
|
384 | - * @return int|bool |
|
385 | - */ |
|
386 | - public function countDisabledInGroup(string $gid): int { |
|
387 | - $this->fixDI(); |
|
58 | + implements IAddToGroupBackend, |
|
59 | + ICountDisabledInGroup, |
|
60 | + ICountUsersBackend, |
|
61 | + ICreateGroupBackend, |
|
62 | + IDeleteGroupBackend, |
|
63 | + IRemoveFromGroupBackend { |
|
64 | + |
|
65 | + /** @var string[] */ |
|
66 | + private $groupCache = []; |
|
67 | + |
|
68 | + /** @var IDBConnection */ |
|
69 | + private $dbConn; |
|
70 | + |
|
71 | + /** |
|
72 | + * \OC\Group\Database constructor. |
|
73 | + * |
|
74 | + * @param IDBConnection|null $dbConn |
|
75 | + */ |
|
76 | + public function __construct(IDBConnection $dbConn = null) { |
|
77 | + $this->dbConn = $dbConn; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * FIXME: This function should not be required! |
|
82 | + */ |
|
83 | + private function fixDI() { |
|
84 | + if ($this->dbConn === null) { |
|
85 | + $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Try to create a new group |
|
91 | + * @param string $gid The name of the group to create |
|
92 | + * @return bool |
|
93 | + * |
|
94 | + * Tries to create a new group. If the group name already exists, false will |
|
95 | + * be returned. |
|
96 | + */ |
|
97 | + public function createGroup(string $gid): bool { |
|
98 | + $this->fixDI(); |
|
99 | + |
|
100 | + // Add group |
|
101 | + $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
102 | + 'gid' => $gid, |
|
103 | + ]); |
|
104 | + |
|
105 | + // Add to cache |
|
106 | + $this->groupCache[$gid] = $gid; |
|
107 | + |
|
108 | + return $result === 1; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * delete a group |
|
113 | + * @param string $gid gid of the group to delete |
|
114 | + * @return bool |
|
115 | + * |
|
116 | + * Deletes a group and removes it from the group_user-table |
|
117 | + */ |
|
118 | + public function deleteGroup(string $gid): bool { |
|
119 | + $this->fixDI(); |
|
120 | + |
|
121 | + // Delete the group |
|
122 | + $qb = $this->dbConn->getQueryBuilder(); |
|
123 | + $qb->delete('groups') |
|
124 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
125 | + ->execute(); |
|
126 | + |
|
127 | + // Delete the group-user relation |
|
128 | + $qb = $this->dbConn->getQueryBuilder(); |
|
129 | + $qb->delete('group_user') |
|
130 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
131 | + ->execute(); |
|
132 | + |
|
133 | + // Delete the group-groupadmin relation |
|
134 | + $qb = $this->dbConn->getQueryBuilder(); |
|
135 | + $qb->delete('group_admin') |
|
136 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
137 | + ->execute(); |
|
138 | + |
|
139 | + // Delete from cache |
|
140 | + unset($this->groupCache[$gid]); |
|
141 | + |
|
142 | + return true; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * is user in group? |
|
147 | + * @param string $uid uid of the user |
|
148 | + * @param string $gid gid of the group |
|
149 | + * @return bool |
|
150 | + * |
|
151 | + * Checks whether the user is member of a group or not. |
|
152 | + */ |
|
153 | + public function inGroup( $uid, $gid ) { |
|
154 | + $this->fixDI(); |
|
155 | + |
|
156 | + // check |
|
157 | + $qb = $this->dbConn->getQueryBuilder(); |
|
158 | + $cursor = $qb->select('uid') |
|
159 | + ->from('group_user') |
|
160 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
161 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
162 | + ->execute(); |
|
163 | + |
|
164 | + $result = $cursor->fetch(); |
|
165 | + $cursor->closeCursor(); |
|
166 | + |
|
167 | + return $result ? true : false; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Add a user to a group |
|
172 | + * @param string $uid Name of the user to add to group |
|
173 | + * @param string $gid Name of the group in which add the user |
|
174 | + * @return bool |
|
175 | + * |
|
176 | + * Adds a user to a group. |
|
177 | + */ |
|
178 | + public function addToGroup(string $uid, string $gid): bool { |
|
179 | + $this->fixDI(); |
|
180 | + |
|
181 | + // No duplicate entries! |
|
182 | + if( !$this->inGroup( $uid, $gid )) { |
|
183 | + $qb = $this->dbConn->getQueryBuilder(); |
|
184 | + $qb->insert('group_user') |
|
185 | + ->setValue('uid', $qb->createNamedParameter($uid)) |
|
186 | + ->setValue('gid', $qb->createNamedParameter($gid)) |
|
187 | + ->execute(); |
|
188 | + return true; |
|
189 | + }else{ |
|
190 | + return false; |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Removes a user from a group |
|
196 | + * @param string $uid Name of the user to remove from group |
|
197 | + * @param string $gid Name of the group from which remove the user |
|
198 | + * @return bool |
|
199 | + * |
|
200 | + * removes the user from a group. |
|
201 | + */ |
|
202 | + public function removeFromGroup(string $uid, string $gid): bool { |
|
203 | + $this->fixDI(); |
|
204 | + |
|
205 | + $qb = $this->dbConn->getQueryBuilder(); |
|
206 | + $qb->delete('group_user') |
|
207 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
208 | + ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
209 | + ->execute(); |
|
210 | + |
|
211 | + return true; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Get all groups a user belongs to |
|
216 | + * @param string $uid Name of the user |
|
217 | + * @return array an array of group names |
|
218 | + * |
|
219 | + * This function fetches all groups a user belongs to. It does not check |
|
220 | + * if the user exists at all. |
|
221 | + */ |
|
222 | + public function getUserGroups( $uid ) { |
|
223 | + //guests has empty or null $uid |
|
224 | + if ($uid === null || $uid === '') { |
|
225 | + return []; |
|
226 | + } |
|
227 | + |
|
228 | + $this->fixDI(); |
|
229 | + |
|
230 | + // No magic! |
|
231 | + $qb = $this->dbConn->getQueryBuilder(); |
|
232 | + $cursor = $qb->select('gid') |
|
233 | + ->from('group_user') |
|
234 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
235 | + ->execute(); |
|
236 | + |
|
237 | + $groups = []; |
|
238 | + while( $row = $cursor->fetch()) { |
|
239 | + $groups[] = $row['gid']; |
|
240 | + $this->groupCache[$row['gid']] = $row['gid']; |
|
241 | + } |
|
242 | + $cursor->closeCursor(); |
|
243 | + |
|
244 | + return $groups; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * get a list of all groups |
|
249 | + * @param string $search |
|
250 | + * @param int $limit |
|
251 | + * @param int $offset |
|
252 | + * @return array an array of group names |
|
253 | + * |
|
254 | + * Returns a list with all groups |
|
255 | + */ |
|
256 | + public function getGroups($search = '', $limit = null, $offset = null) { |
|
257 | + $this->fixDI(); |
|
258 | + |
|
259 | + $query = $this->dbConn->getQueryBuilder(); |
|
260 | + $query->select('gid') |
|
261 | + ->from('groups') |
|
262 | + ->orderBy('gid', 'ASC'); |
|
263 | + |
|
264 | + if ($search !== '') { |
|
265 | + $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
|
266 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
267 | + ))); |
|
268 | + } |
|
269 | + |
|
270 | + $query->setMaxResults($limit) |
|
271 | + ->setFirstResult($offset); |
|
272 | + $result = $query->execute(); |
|
273 | + |
|
274 | + $groups = []; |
|
275 | + while ($row = $result->fetch()) { |
|
276 | + $groups[] = $row['gid']; |
|
277 | + } |
|
278 | + $result->closeCursor(); |
|
279 | + |
|
280 | + return $groups; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * check if a group exists |
|
285 | + * @param string $gid |
|
286 | + * @return bool |
|
287 | + */ |
|
288 | + public function groupExists($gid) { |
|
289 | + $this->fixDI(); |
|
290 | + |
|
291 | + // Check cache first |
|
292 | + if (isset($this->groupCache[$gid])) { |
|
293 | + return true; |
|
294 | + } |
|
295 | + |
|
296 | + $qb = $this->dbConn->getQueryBuilder(); |
|
297 | + $cursor = $qb->select('gid') |
|
298 | + ->from('groups') |
|
299 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
300 | + ->execute(); |
|
301 | + $result = $cursor->fetch(); |
|
302 | + $cursor->closeCursor(); |
|
303 | + |
|
304 | + if ($result !== false) { |
|
305 | + $this->groupCache[$gid] = $gid; |
|
306 | + return true; |
|
307 | + } |
|
308 | + return false; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * get a list of all users in a group |
|
313 | + * @param string $gid |
|
314 | + * @param string $search |
|
315 | + * @param int $limit |
|
316 | + * @param int $offset |
|
317 | + * @return array an array of user ids |
|
318 | + */ |
|
319 | + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
320 | + $this->fixDI(); |
|
321 | + |
|
322 | + $query = $this->dbConn->getQueryBuilder(); |
|
323 | + $query->select('uid') |
|
324 | + ->from('group_user') |
|
325 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))) |
|
326 | + ->orderBy('uid', 'ASC'); |
|
327 | + |
|
328 | + if ($search !== '') { |
|
329 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
330 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
331 | + ))); |
|
332 | + } |
|
333 | + |
|
334 | + $query->setMaxResults($limit) |
|
335 | + ->setFirstResult($offset); |
|
336 | + $result = $query->execute(); |
|
337 | + |
|
338 | + $users = []; |
|
339 | + while ($row = $result->fetch()) { |
|
340 | + $users[] = $row['uid']; |
|
341 | + } |
|
342 | + $result->closeCursor(); |
|
343 | + |
|
344 | + return $users; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * get the number of all users matching the search string in a group |
|
349 | + * @param string $gid |
|
350 | + * @param string $search |
|
351 | + * @return int |
|
352 | + */ |
|
353 | + public function countUsersInGroup(string $gid, string $search = ''): int { |
|
354 | + $this->fixDI(); |
|
355 | + |
|
356 | + $query = $this->dbConn->getQueryBuilder(); |
|
357 | + $query->selectAlias($query->createFunction('COUNT(*)'), 'num_users') |
|
358 | + ->from('group_user') |
|
359 | + ->where($query->expr()->eq('gid', $query->createNamedParameter($gid))); |
|
360 | + |
|
361 | + if ($search !== '') { |
|
362 | + $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
|
363 | + '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
364 | + ))); |
|
365 | + } |
|
366 | + |
|
367 | + $result = $query->execute(); |
|
368 | + $count = $result->fetchColumn(); |
|
369 | + $result->closeCursor(); |
|
370 | + |
|
371 | + if ($count !== false) { |
|
372 | + $count = (int)$count; |
|
373 | + } else { |
|
374 | + $count = 0; |
|
375 | + } |
|
376 | + |
|
377 | + return $count; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * get the number of disabled users in a group |
|
382 | + * |
|
383 | + * @param string $search |
|
384 | + * @return int|bool |
|
385 | + */ |
|
386 | + public function countDisabledInGroup(string $gid): int { |
|
387 | + $this->fixDI(); |
|
388 | 388 | |
389 | - $query = $this->dbConn->getQueryBuilder(); |
|
390 | - $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
391 | - ->from('preferences', 'p') |
|
392 | - ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
|
393 | - ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
394 | - ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
395 | - ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
396 | - ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
389 | + $query = $this->dbConn->getQueryBuilder(); |
|
390 | + $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
391 | + ->from('preferences', 'p') |
|
392 | + ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
|
393 | + ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
|
394 | + ->andWhere($query->expr()->eq('configkey', $query->createNamedParameter('enabled'))) |
|
395 | + ->andWhere($query->expr()->eq('configvalue', $query->createNamedParameter('false'), IQueryBuilder::PARAM_STR)) |
|
396 | + ->andWhere($query->expr()->eq('gid', $query->createNamedParameter($gid), IQueryBuilder::PARAM_STR)); |
|
397 | 397 | |
398 | - $result = $query->execute(); |
|
399 | - $count = $result->fetchColumn(); |
|
400 | - $result->closeCursor(); |
|
398 | + $result = $query->execute(); |
|
399 | + $count = $result->fetchColumn(); |
|
400 | + $result->closeCursor(); |
|
401 | 401 | |
402 | - if ($count !== false) { |
|
403 | - $count = (int)$count; |
|
404 | - } else { |
|
405 | - $count = 0; |
|
406 | - } |
|
407 | - |
|
408 | - return $count; |
|
409 | - } |
|
402 | + if ($count !== false) { |
|
403 | + $count = (int)$count; |
|
404 | + } else { |
|
405 | + $count = 0; |
|
406 | + } |
|
407 | + |
|
408 | + return $count; |
|
409 | + } |
|
410 | 410 | |
411 | 411 | } |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | * |
151 | 151 | * Checks whether the user is member of a group or not. |
152 | 152 | */ |
153 | - public function inGroup( $uid, $gid ) { |
|
153 | + public function inGroup($uid, $gid) { |
|
154 | 154 | $this->fixDI(); |
155 | 155 | |
156 | 156 | // check |
@@ -179,14 +179,14 @@ discard block |
||
179 | 179 | $this->fixDI(); |
180 | 180 | |
181 | 181 | // No duplicate entries! |
182 | - if( !$this->inGroup( $uid, $gid )) { |
|
182 | + if (!$this->inGroup($uid, $gid)) { |
|
183 | 183 | $qb = $this->dbConn->getQueryBuilder(); |
184 | 184 | $qb->insert('group_user') |
185 | 185 | ->setValue('uid', $qb->createNamedParameter($uid)) |
186 | 186 | ->setValue('gid', $qb->createNamedParameter($gid)) |
187 | 187 | ->execute(); |
188 | 188 | return true; |
189 | - }else{ |
|
189 | + } else { |
|
190 | 190 | return false; |
191 | 191 | } |
192 | 192 | } |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | * This function fetches all groups a user belongs to. It does not check |
220 | 220 | * if the user exists at all. |
221 | 221 | */ |
222 | - public function getUserGroups( $uid ) { |
|
222 | + public function getUserGroups($uid) { |
|
223 | 223 | //guests has empty or null $uid |
224 | 224 | if ($uid === null || $uid === '') { |
225 | 225 | return []; |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | ->execute(); |
236 | 236 | |
237 | 237 | $groups = []; |
238 | - while( $row = $cursor->fetch()) { |
|
238 | + while ($row = $cursor->fetch()) { |
|
239 | 239 | $groups[] = $row['gid']; |
240 | 240 | $this->groupCache[$row['gid']] = $row['gid']; |
241 | 241 | } |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | |
264 | 264 | if ($search !== '') { |
265 | 265 | $query->where($query->expr()->iLike('gid', $query->createNamedParameter( |
266 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
266 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
267 | 267 | ))); |
268 | 268 | } |
269 | 269 | |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | |
328 | 328 | if ($search !== '') { |
329 | 329 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
330 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
330 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
331 | 331 | ))); |
332 | 332 | } |
333 | 333 | |
@@ -360,7 +360,7 @@ discard block |
||
360 | 360 | |
361 | 361 | if ($search !== '') { |
362 | 362 | $query->andWhere($query->expr()->like('uid', $query->createNamedParameter( |
363 | - '%' . $this->dbConn->escapeLikeParameter($search) . '%' |
|
363 | + '%'.$this->dbConn->escapeLikeParameter($search).'%' |
|
364 | 364 | ))); |
365 | 365 | } |
366 | 366 | |
@@ -369,7 +369,7 @@ discard block |
||
369 | 369 | $result->closeCursor(); |
370 | 370 | |
371 | 371 | if ($count !== false) { |
372 | - $count = (int)$count; |
|
372 | + $count = (int) $count; |
|
373 | 373 | } else { |
374 | 374 | $count = 0; |
375 | 375 | } |
@@ -387,7 +387,7 @@ discard block |
||
387 | 387 | $this->fixDI(); |
388 | 388 | |
389 | 389 | $query = $this->dbConn->getQueryBuilder(); |
390 | - $query->select($query->createFunction('COUNT(DISTINCT ' . $query->getColumnName('uid') . ')')) |
|
390 | + $query->select($query->createFunction('COUNT(DISTINCT '.$query->getColumnName('uid').')')) |
|
391 | 391 | ->from('preferences', 'p') |
392 | 392 | ->innerJoin('p', 'group_user', 'g', $query->expr()->eq('p.userid', 'g.uid')) |
393 | 393 | ->where($query->expr()->eq('appid', $query->createNamedParameter('core'))) |
@@ -400,7 +400,7 @@ discard block |
||
400 | 400 | $result->closeCursor(); |
401 | 401 | |
402 | 402 | if ($count !== false) { |
403 | - $count = (int)$count; |
|
403 | + $count = (int) $count; |
|
404 | 404 | } else { |
405 | 405 | $count = 0; |
406 | 406 | } |
@@ -30,57 +30,57 @@ |
||
30 | 30 | |
31 | 31 | class BuildCalendarSearchIndex implements IRepairStep { |
32 | 32 | |
33 | - /** @var IDBConnection */ |
|
34 | - private $db; |
|
33 | + /** @var IDBConnection */ |
|
34 | + private $db; |
|
35 | 35 | |
36 | - /** @var IJobList */ |
|
37 | - private $jobList; |
|
36 | + /** @var IJobList */ |
|
37 | + private $jobList; |
|
38 | 38 | |
39 | - /** @var IConfig */ |
|
40 | - private $config; |
|
39 | + /** @var IConfig */ |
|
40 | + private $config; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @param IDBConnection $db |
|
44 | - * @param IJobList $jobList |
|
45 | - * @param IConfig $config |
|
46 | - */ |
|
47 | - public function __construct(IDBConnection $db, |
|
48 | - IJobList $jobList, |
|
49 | - IConfig $config) { |
|
50 | - $this->db = $db; |
|
51 | - $this->jobList = $jobList; |
|
52 | - $this->config = $config; |
|
53 | - } |
|
42 | + /** |
|
43 | + * @param IDBConnection $db |
|
44 | + * @param IJobList $jobList |
|
45 | + * @param IConfig $config |
|
46 | + */ |
|
47 | + public function __construct(IDBConnection $db, |
|
48 | + IJobList $jobList, |
|
49 | + IConfig $config) { |
|
50 | + $this->db = $db; |
|
51 | + $this->jobList = $jobList; |
|
52 | + $this->config = $config; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function getName() { |
|
59 | - return 'Registering building of calendar search index as background job'; |
|
60 | - } |
|
55 | + /** |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function getName() { |
|
59 | + return 'Registering building of calendar search index as background job'; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param IOutput $output |
|
64 | - */ |
|
65 | - public function run(IOutput $output) { |
|
66 | - // only run once |
|
67 | - if ($this->config->getAppValue('dav', 'buildCalendarSearchIndex') === 'yes') { |
|
68 | - $output->info('Repair step already executed'); |
|
69 | - return; |
|
70 | - } |
|
62 | + /** |
|
63 | + * @param IOutput $output |
|
64 | + */ |
|
65 | + public function run(IOutput $output) { |
|
66 | + // only run once |
|
67 | + if ($this->config->getAppValue('dav', 'buildCalendarSearchIndex') === 'yes') { |
|
68 | + $output->info('Repair step already executed'); |
|
69 | + return; |
|
70 | + } |
|
71 | 71 | |
72 | - $query = $this->db->getQueryBuilder(); |
|
73 | - $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')')) |
|
74 | - ->from('calendarobjects'); |
|
75 | - $maxId = (int)$query->execute()->fetchColumn(); |
|
72 | + $query = $this->db->getQueryBuilder(); |
|
73 | + $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')')) |
|
74 | + ->from('calendarobjects'); |
|
75 | + $maxId = (int)$query->execute()->fetchColumn(); |
|
76 | 76 | |
77 | - $output->info('Add background job'); |
|
78 | - $this->jobList->add(BuildCalendarSearchIndexBackgroundJob::class, [ |
|
79 | - 'offset' => 0, |
|
80 | - 'stopAt' => $maxId |
|
81 | - ]); |
|
77 | + $output->info('Add background job'); |
|
78 | + $this->jobList->add(BuildCalendarSearchIndexBackgroundJob::class, [ |
|
79 | + 'offset' => 0, |
|
80 | + 'stopAt' => $maxId |
|
81 | + ]); |
|
82 | 82 | |
83 | - // if all were done, no need to redo the repair during next upgrade |
|
84 | - $this->config->setAppValue('dav', 'buildCalendarSearchIndex', 'yes'); |
|
85 | - } |
|
83 | + // if all were done, no need to redo the repair during next upgrade |
|
84 | + $this->config->setAppValue('dav', 'buildCalendarSearchIndex', 'yes'); |
|
85 | + } |
|
86 | 86 | } |
87 | 87 | \ No newline at end of file |
@@ -70,9 +70,9 @@ |
||
70 | 70 | } |
71 | 71 | |
72 | 72 | $query = $this->db->getQueryBuilder(); |
73 | - $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')')) |
|
73 | + $query->select($query->createFunction('MAX('.$query->getColumnName('id').')')) |
|
74 | 74 | ->from('calendarobjects'); |
75 | - $maxId = (int)$query->execute()->fetchColumn(); |
|
75 | + $maxId = (int) $query->execute()->fetchColumn(); |
|
76 | 76 | |
77 | 77 | $output->info('Add background job'); |
78 | 78 | $this->jobList->add(BuildCalendarSearchIndexBackgroundJob::class, [ |
@@ -29,293 +29,293 @@ |
||
29 | 29 | * @package OCA\User_LDAP\Mapping |
30 | 30 | */ |
31 | 31 | abstract class AbstractMapping { |
32 | - /** |
|
33 | - * @var \OCP\IDBConnection $dbc |
|
34 | - */ |
|
35 | - protected $dbc; |
|
32 | + /** |
|
33 | + * @var \OCP\IDBConnection $dbc |
|
34 | + */ |
|
35 | + protected $dbc; |
|
36 | 36 | |
37 | - /** |
|
38 | - * returns the DB table name which holds the mappings |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - abstract protected function getTableName(); |
|
37 | + /** |
|
38 | + * returns the DB table name which holds the mappings |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + abstract protected function getTableName(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param \OCP\IDBConnection $dbc |
|
45 | - */ |
|
46 | - public function __construct(\OCP\IDBConnection $dbc) { |
|
47 | - $this->dbc = $dbc; |
|
48 | - } |
|
43 | + /** |
|
44 | + * @param \OCP\IDBConnection $dbc |
|
45 | + */ |
|
46 | + public function __construct(\OCP\IDBConnection $dbc) { |
|
47 | + $this->dbc = $dbc; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * checks whether a provided string represents an existing table col |
|
52 | - * @param string $col |
|
53 | - * @return bool |
|
54 | - */ |
|
55 | - public function isColNameValid($col) { |
|
56 | - switch($col) { |
|
57 | - case 'ldap_dn': |
|
58 | - case 'owncloud_name': |
|
59 | - case 'directory_uuid': |
|
60 | - return true; |
|
61 | - default: |
|
62 | - return false; |
|
63 | - } |
|
64 | - } |
|
50 | + /** |
|
51 | + * checks whether a provided string represents an existing table col |
|
52 | + * @param string $col |
|
53 | + * @return bool |
|
54 | + */ |
|
55 | + public function isColNameValid($col) { |
|
56 | + switch($col) { |
|
57 | + case 'ldap_dn': |
|
58 | + case 'owncloud_name': |
|
59 | + case 'directory_uuid': |
|
60 | + return true; |
|
61 | + default: |
|
62 | + return false; |
|
63 | + } |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Gets the value of one column based on a provided value of another column |
|
68 | - * @param string $fetchCol |
|
69 | - * @param string $compareCol |
|
70 | - * @param string $search |
|
71 | - * @throws \Exception |
|
72 | - * @return string|false |
|
73 | - */ |
|
74 | - protected function getXbyY($fetchCol, $compareCol, $search) { |
|
75 | - if(!$this->isColNameValid($fetchCol)) { |
|
76 | - //this is used internally only, but we don't want to risk |
|
77 | - //having SQL injection at all. |
|
78 | - throw new \Exception('Invalid Column Name'); |
|
79 | - } |
|
80 | - $query = $this->dbc->prepare(' |
|
66 | + /** |
|
67 | + * Gets the value of one column based on a provided value of another column |
|
68 | + * @param string $fetchCol |
|
69 | + * @param string $compareCol |
|
70 | + * @param string $search |
|
71 | + * @throws \Exception |
|
72 | + * @return string|false |
|
73 | + */ |
|
74 | + protected function getXbyY($fetchCol, $compareCol, $search) { |
|
75 | + if(!$this->isColNameValid($fetchCol)) { |
|
76 | + //this is used internally only, but we don't want to risk |
|
77 | + //having SQL injection at all. |
|
78 | + throw new \Exception('Invalid Column Name'); |
|
79 | + } |
|
80 | + $query = $this->dbc->prepare(' |
|
81 | 81 | SELECT `' . $fetchCol . '` |
82 | 82 | FROM `'. $this->getTableName() .'` |
83 | 83 | WHERE `' . $compareCol . '` = ? |
84 | 84 | '); |
85 | 85 | |
86 | - $res = $query->execute(array($search)); |
|
87 | - if($res !== false) { |
|
88 | - return $query->fetchColumn(); |
|
89 | - } |
|
86 | + $res = $query->execute(array($search)); |
|
87 | + if($res !== false) { |
|
88 | + return $query->fetchColumn(); |
|
89 | + } |
|
90 | 90 | |
91 | - return false; |
|
92 | - } |
|
91 | + return false; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Performs a DELETE or UPDATE query to the database. |
|
96 | - * @param \Doctrine\DBAL\Driver\Statement $query |
|
97 | - * @param array $parameters |
|
98 | - * @return bool true if at least one row was modified, false otherwise |
|
99 | - */ |
|
100 | - protected function modify($query, $parameters) { |
|
101 | - $result = $query->execute($parameters); |
|
102 | - return ($result === true && $query->rowCount() > 0); |
|
103 | - } |
|
94 | + /** |
|
95 | + * Performs a DELETE or UPDATE query to the database. |
|
96 | + * @param \Doctrine\DBAL\Driver\Statement $query |
|
97 | + * @param array $parameters |
|
98 | + * @return bool true if at least one row was modified, false otherwise |
|
99 | + */ |
|
100 | + protected function modify($query, $parameters) { |
|
101 | + $result = $query->execute($parameters); |
|
102 | + return ($result === true && $query->rowCount() > 0); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Gets the LDAP DN based on the provided name. |
|
107 | - * Replaces Access::ocname2dn |
|
108 | - * @param string $name |
|
109 | - * @return string|false |
|
110 | - */ |
|
111 | - public function getDNByName($name) { |
|
112 | - return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
113 | - } |
|
105 | + /** |
|
106 | + * Gets the LDAP DN based on the provided name. |
|
107 | + * Replaces Access::ocname2dn |
|
108 | + * @param string $name |
|
109 | + * @return string|false |
|
110 | + */ |
|
111 | + public function getDNByName($name) { |
|
112 | + return $this->getXbyY('ldap_dn', 'owncloud_name', $name); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Updates the DN based on the given UUID |
|
117 | - * @param string $fdn |
|
118 | - * @param string $uuid |
|
119 | - * @return bool |
|
120 | - */ |
|
121 | - public function setDNbyUUID($fdn, $uuid) { |
|
122 | - $query = $this->dbc->prepare(' |
|
115 | + /** |
|
116 | + * Updates the DN based on the given UUID |
|
117 | + * @param string $fdn |
|
118 | + * @param string $uuid |
|
119 | + * @return bool |
|
120 | + */ |
|
121 | + public function setDNbyUUID($fdn, $uuid) { |
|
122 | + $query = $this->dbc->prepare(' |
|
123 | 123 | UPDATE `' . $this->getTableName() . '` |
124 | 124 | SET `ldap_dn` = ? |
125 | 125 | WHERE `directory_uuid` = ? |
126 | 126 | '); |
127 | 127 | |
128 | - return $this->modify($query, array($fdn, $uuid)); |
|
129 | - } |
|
128 | + return $this->modify($query, array($fdn, $uuid)); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Updates the UUID based on the given DN |
|
133 | - * |
|
134 | - * required by Migration/UUIDFix |
|
135 | - * |
|
136 | - * @param $uuid |
|
137 | - * @param $fdn |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function setUUIDbyDN($uuid, $fdn) { |
|
141 | - $query = $this->dbc->prepare(' |
|
131 | + /** |
|
132 | + * Updates the UUID based on the given DN |
|
133 | + * |
|
134 | + * required by Migration/UUIDFix |
|
135 | + * |
|
136 | + * @param $uuid |
|
137 | + * @param $fdn |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function setUUIDbyDN($uuid, $fdn) { |
|
141 | + $query = $this->dbc->prepare(' |
|
142 | 142 | UPDATE `' . $this->getTableName() . '` |
143 | 143 | SET `directory_uuid` = ? |
144 | 144 | WHERE `ldap_dn` = ? |
145 | 145 | '); |
146 | 146 | |
147 | - return $this->modify($query, [$uuid, $fdn]); |
|
148 | - } |
|
147 | + return $this->modify($query, [$uuid, $fdn]); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Gets the name based on the provided LDAP DN. |
|
152 | - * @param string $fdn |
|
153 | - * @return string|false |
|
154 | - */ |
|
155 | - public function getNameByDN($fdn) { |
|
156 | - return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
157 | - } |
|
150 | + /** |
|
151 | + * Gets the name based on the provided LDAP DN. |
|
152 | + * @param string $fdn |
|
153 | + * @return string|false |
|
154 | + */ |
|
155 | + public function getNameByDN($fdn) { |
|
156 | + return $this->getXbyY('owncloud_name', 'ldap_dn', $fdn); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Searches mapped names by the giving string in the name column |
|
161 | - * @param string $search |
|
162 | - * @param string $prefixMatch |
|
163 | - * @param string $postfixMatch |
|
164 | - * @return string[] |
|
165 | - */ |
|
166 | - public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
167 | - $query = $this->dbc->prepare(' |
|
159 | + /** |
|
160 | + * Searches mapped names by the giving string in the name column |
|
161 | + * @param string $search |
|
162 | + * @param string $prefixMatch |
|
163 | + * @param string $postfixMatch |
|
164 | + * @return string[] |
|
165 | + */ |
|
166 | + public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
|
167 | + $query = $this->dbc->prepare(' |
|
168 | 168 | SELECT `owncloud_name` |
169 | 169 | FROM `'. $this->getTableName() .'` |
170 | 170 | WHERE `owncloud_name` LIKE ? |
171 | 171 | '); |
172 | 172 | |
173 | - $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch)); |
|
174 | - $names = array(); |
|
175 | - if($res !== false) { |
|
176 | - while($row = $query->fetch()) { |
|
177 | - $names[] = $row['owncloud_name']; |
|
178 | - } |
|
179 | - } |
|
180 | - return $names; |
|
181 | - } |
|
173 | + $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch)); |
|
174 | + $names = array(); |
|
175 | + if($res !== false) { |
|
176 | + while($row = $query->fetch()) { |
|
177 | + $names[] = $row['owncloud_name']; |
|
178 | + } |
|
179 | + } |
|
180 | + return $names; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Gets the name based on the provided LDAP UUID. |
|
185 | - * @param string $uuid |
|
186 | - * @return string|false |
|
187 | - */ |
|
188 | - public function getNameByUUID($uuid) { |
|
189 | - return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
190 | - } |
|
183 | + /** |
|
184 | + * Gets the name based on the provided LDAP UUID. |
|
185 | + * @param string $uuid |
|
186 | + * @return string|false |
|
187 | + */ |
|
188 | + public function getNameByUUID($uuid) { |
|
189 | + return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid); |
|
190 | + } |
|
191 | 191 | |
192 | - /** |
|
193 | - * Gets the UUID based on the provided LDAP DN |
|
194 | - * @param string $dn |
|
195 | - * @return false|string |
|
196 | - * @throws \Exception |
|
197 | - */ |
|
198 | - public function getUUIDByDN($dn) { |
|
199 | - return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
200 | - } |
|
192 | + /** |
|
193 | + * Gets the UUID based on the provided LDAP DN |
|
194 | + * @param string $dn |
|
195 | + * @return false|string |
|
196 | + * @throws \Exception |
|
197 | + */ |
|
198 | + public function getUUIDByDN($dn) { |
|
199 | + return $this->getXbyY('directory_uuid', 'ldap_dn', $dn); |
|
200 | + } |
|
201 | 201 | |
202 | - /** |
|
203 | - * gets a piece of the mapping list |
|
204 | - * @param int $offset |
|
205 | - * @param int $limit |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - public function getList($offset = null, $limit = null) { |
|
209 | - $query = $this->dbc->prepare(' |
|
202 | + /** |
|
203 | + * gets a piece of the mapping list |
|
204 | + * @param int $offset |
|
205 | + * @param int $limit |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + public function getList($offset = null, $limit = null) { |
|
209 | + $query = $this->dbc->prepare(' |
|
210 | 210 | SELECT |
211 | 211 | `ldap_dn` AS `dn`, |
212 | 212 | `owncloud_name` AS `name`, |
213 | 213 | `directory_uuid` AS `uuid` |
214 | 214 | FROM `' . $this->getTableName() . '`', |
215 | - $limit, |
|
216 | - $offset |
|
217 | - ); |
|
215 | + $limit, |
|
216 | + $offset |
|
217 | + ); |
|
218 | 218 | |
219 | - $query->execute(); |
|
220 | - return $query->fetchAll(); |
|
221 | - } |
|
219 | + $query->execute(); |
|
220 | + return $query->fetchAll(); |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * attempts to map the given entry |
|
225 | - * @param string $fdn fully distinguished name (from LDAP) |
|
226 | - * @param string $name |
|
227 | - * @param string $uuid a unique identifier as used in LDAP |
|
228 | - * @return bool |
|
229 | - */ |
|
230 | - public function map($fdn, $name, $uuid) { |
|
231 | - if(mb_strlen($fdn) > 255) { |
|
232 | - \OC::$server->getLogger()->error( |
|
233 | - 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
234 | - [ |
|
235 | - 'app' => 'user_ldap', |
|
236 | - 'dn' => $fdn, |
|
237 | - ] |
|
238 | - ); |
|
239 | - return false; |
|
240 | - } |
|
223 | + /** |
|
224 | + * attempts to map the given entry |
|
225 | + * @param string $fdn fully distinguished name (from LDAP) |
|
226 | + * @param string $name |
|
227 | + * @param string $uuid a unique identifier as used in LDAP |
|
228 | + * @return bool |
|
229 | + */ |
|
230 | + public function map($fdn, $name, $uuid) { |
|
231 | + if(mb_strlen($fdn) > 255) { |
|
232 | + \OC::$server->getLogger()->error( |
|
233 | + 'Cannot map, because the DN exceeds 255 characters: {dn}', |
|
234 | + [ |
|
235 | + 'app' => 'user_ldap', |
|
236 | + 'dn' => $fdn, |
|
237 | + ] |
|
238 | + ); |
|
239 | + return false; |
|
240 | + } |
|
241 | 241 | |
242 | - $row = array( |
|
243 | - 'ldap_dn' => $fdn, |
|
244 | - 'owncloud_name' => $name, |
|
245 | - 'directory_uuid' => $uuid |
|
246 | - ); |
|
242 | + $row = array( |
|
243 | + 'ldap_dn' => $fdn, |
|
244 | + 'owncloud_name' => $name, |
|
245 | + 'directory_uuid' => $uuid |
|
246 | + ); |
|
247 | 247 | |
248 | - try { |
|
249 | - $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
250 | - // insertIfNotExist returns values as int |
|
251 | - return (bool)$result; |
|
252 | - } catch (\Exception $e) { |
|
253 | - return false; |
|
254 | - } |
|
255 | - } |
|
248 | + try { |
|
249 | + $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
|
250 | + // insertIfNotExist returns values as int |
|
251 | + return (bool)$result; |
|
252 | + } catch (\Exception $e) { |
|
253 | + return false; |
|
254 | + } |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * removes a mapping based on the owncloud_name of the entry |
|
259 | - * @param string $name |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - public function unmap($name) { |
|
263 | - $query = $this->dbc->prepare(' |
|
257 | + /** |
|
258 | + * removes a mapping based on the owncloud_name of the entry |
|
259 | + * @param string $name |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + public function unmap($name) { |
|
263 | + $query = $this->dbc->prepare(' |
|
264 | 264 | DELETE FROM `'. $this->getTableName() .'` |
265 | 265 | WHERE `owncloud_name` = ?'); |
266 | 266 | |
267 | - return $this->modify($query, array($name)); |
|
268 | - } |
|
267 | + return $this->modify($query, array($name)); |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Truncate's the mapping table |
|
272 | - * @return bool |
|
273 | - */ |
|
274 | - public function clear() { |
|
275 | - $sql = $this->dbc |
|
276 | - ->getDatabasePlatform() |
|
277 | - ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
278 | - return $this->dbc->prepare($sql)->execute(); |
|
279 | - } |
|
270 | + /** |
|
271 | + * Truncate's the mapping table |
|
272 | + * @return bool |
|
273 | + */ |
|
274 | + public function clear() { |
|
275 | + $sql = $this->dbc |
|
276 | + ->getDatabasePlatform() |
|
277 | + ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
278 | + return $this->dbc->prepare($sql)->execute(); |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * clears the mapping table one by one and executing a callback with |
|
283 | - * each row's id (=owncloud_name col) |
|
284 | - * |
|
285 | - * @param callable $preCallback |
|
286 | - * @param callable $postCallback |
|
287 | - * @return bool true on success, false when at least one row was not |
|
288 | - * deleted |
|
289 | - */ |
|
290 | - public function clearCb(Callable $preCallback, Callable $postCallback): bool { |
|
291 | - $picker = $this->dbc->getQueryBuilder(); |
|
292 | - $picker->select('owncloud_name') |
|
293 | - ->from($this->getTableName()); |
|
294 | - $cursor = $picker->execute(); |
|
295 | - $result = true; |
|
296 | - while($id = $cursor->fetchColumn(0)) { |
|
297 | - $preCallback($id); |
|
298 | - if($isUnmapped = $this->unmap($id)) { |
|
299 | - $postCallback($id); |
|
300 | - } |
|
301 | - $result &= $isUnmapped; |
|
302 | - } |
|
303 | - $cursor->closeCursor(); |
|
304 | - return $result; |
|
305 | - } |
|
281 | + /** |
|
282 | + * clears the mapping table one by one and executing a callback with |
|
283 | + * each row's id (=owncloud_name col) |
|
284 | + * |
|
285 | + * @param callable $preCallback |
|
286 | + * @param callable $postCallback |
|
287 | + * @return bool true on success, false when at least one row was not |
|
288 | + * deleted |
|
289 | + */ |
|
290 | + public function clearCb(Callable $preCallback, Callable $postCallback): bool { |
|
291 | + $picker = $this->dbc->getQueryBuilder(); |
|
292 | + $picker->select('owncloud_name') |
|
293 | + ->from($this->getTableName()); |
|
294 | + $cursor = $picker->execute(); |
|
295 | + $result = true; |
|
296 | + while($id = $cursor->fetchColumn(0)) { |
|
297 | + $preCallback($id); |
|
298 | + if($isUnmapped = $this->unmap($id)) { |
|
299 | + $postCallback($id); |
|
300 | + } |
|
301 | + $result &= $isUnmapped; |
|
302 | + } |
|
303 | + $cursor->closeCursor(); |
|
304 | + return $result; |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * returns the number of entries in the mappings table |
|
309 | - * |
|
310 | - * @return int |
|
311 | - */ |
|
312 | - public function count() { |
|
313 | - $qb = $this->dbc->getQueryBuilder(); |
|
314 | - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('ldap_dn') . ')')) |
|
315 | - ->from($this->getTableName()); |
|
316 | - $res = $query->execute(); |
|
317 | - $count = $res->fetchColumn(); |
|
318 | - $res->closeCursor(); |
|
319 | - return (int)$count; |
|
320 | - } |
|
307 | + /** |
|
308 | + * returns the number of entries in the mappings table |
|
309 | + * |
|
310 | + * @return int |
|
311 | + */ |
|
312 | + public function count() { |
|
313 | + $qb = $this->dbc->getQueryBuilder(); |
|
314 | + $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('ldap_dn') . ')')) |
|
315 | + ->from($this->getTableName()); |
|
316 | + $res = $query->execute(); |
|
317 | + $count = $res->fetchColumn(); |
|
318 | + $res->closeCursor(); |
|
319 | + return (int)$count; |
|
320 | + } |
|
321 | 321 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * @return bool |
54 | 54 | */ |
55 | 55 | public function isColNameValid($col) { |
56 | - switch($col) { |
|
56 | + switch ($col) { |
|
57 | 57 | case 'ldap_dn': |
58 | 58 | case 'owncloud_name': |
59 | 59 | case 'directory_uuid': |
@@ -72,19 +72,19 @@ discard block |
||
72 | 72 | * @return string|false |
73 | 73 | */ |
74 | 74 | protected function getXbyY($fetchCol, $compareCol, $search) { |
75 | - if(!$this->isColNameValid($fetchCol)) { |
|
75 | + if (!$this->isColNameValid($fetchCol)) { |
|
76 | 76 | //this is used internally only, but we don't want to risk |
77 | 77 | //having SQL injection at all. |
78 | 78 | throw new \Exception('Invalid Column Name'); |
79 | 79 | } |
80 | 80 | $query = $this->dbc->prepare(' |
81 | - SELECT `' . $fetchCol . '` |
|
82 | - FROM `'. $this->getTableName() .'` |
|
83 | - WHERE `' . $compareCol . '` = ? |
|
81 | + SELECT `' . $fetchCol.'` |
|
82 | + FROM `'. $this->getTableName().'` |
|
83 | + WHERE `' . $compareCol.'` = ? |
|
84 | 84 | '); |
85 | 85 | |
86 | 86 | $res = $query->execute(array($search)); |
87 | - if($res !== false) { |
|
87 | + if ($res !== false) { |
|
88 | 88 | return $query->fetchColumn(); |
89 | 89 | } |
90 | 90 | |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | */ |
121 | 121 | public function setDNbyUUID($fdn, $uuid) { |
122 | 122 | $query = $this->dbc->prepare(' |
123 | - UPDATE `' . $this->getTableName() . '` |
|
123 | + UPDATE `' . $this->getTableName().'` |
|
124 | 124 | SET `ldap_dn` = ? |
125 | 125 | WHERE `directory_uuid` = ? |
126 | 126 | '); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | */ |
140 | 140 | public function setUUIDbyDN($uuid, $fdn) { |
141 | 141 | $query = $this->dbc->prepare(' |
142 | - UPDATE `' . $this->getTableName() . '` |
|
142 | + UPDATE `' . $this->getTableName().'` |
|
143 | 143 | SET `directory_uuid` = ? |
144 | 144 | WHERE `ldap_dn` = ? |
145 | 145 | '); |
@@ -166,14 +166,14 @@ discard block |
||
166 | 166 | public function getNamesBySearch($search, $prefixMatch = "", $postfixMatch = "") { |
167 | 167 | $query = $this->dbc->prepare(' |
168 | 168 | SELECT `owncloud_name` |
169 | - FROM `'. $this->getTableName() .'` |
|
169 | + FROM `'. $this->getTableName().'` |
|
170 | 170 | WHERE `owncloud_name` LIKE ? |
171 | 171 | '); |
172 | 172 | |
173 | 173 | $res = $query->execute(array($prefixMatch.$this->dbc->escapeLikeParameter($search).$postfixMatch)); |
174 | 174 | $names = array(); |
175 | - if($res !== false) { |
|
176 | - while($row = $query->fetch()) { |
|
175 | + if ($res !== false) { |
|
176 | + while ($row = $query->fetch()) { |
|
177 | 177 | $names[] = $row['owncloud_name']; |
178 | 178 | } |
179 | 179 | } |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | `ldap_dn` AS `dn`, |
212 | 212 | `owncloud_name` AS `name`, |
213 | 213 | `directory_uuid` AS `uuid` |
214 | - FROM `' . $this->getTableName() . '`', |
|
214 | + FROM `' . $this->getTableName().'`', |
|
215 | 215 | $limit, |
216 | 216 | $offset |
217 | 217 | ); |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | * @return bool |
229 | 229 | */ |
230 | 230 | public function map($fdn, $name, $uuid) { |
231 | - if(mb_strlen($fdn) > 255) { |
|
231 | + if (mb_strlen($fdn) > 255) { |
|
232 | 232 | \OC::$server->getLogger()->error( |
233 | 233 | 'Cannot map, because the DN exceeds 255 characters: {dn}', |
234 | 234 | [ |
@@ -248,7 +248,7 @@ discard block |
||
248 | 248 | try { |
249 | 249 | $result = $this->dbc->insertIfNotExist($this->getTableName(), $row); |
250 | 250 | // insertIfNotExist returns values as int |
251 | - return (bool)$result; |
|
251 | + return (bool) $result; |
|
252 | 252 | } catch (\Exception $e) { |
253 | 253 | return false; |
254 | 254 | } |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | */ |
262 | 262 | public function unmap($name) { |
263 | 263 | $query = $this->dbc->prepare(' |
264 | - DELETE FROM `'. $this->getTableName() .'` |
|
264 | + DELETE FROM `'. $this->getTableName().'` |
|
265 | 265 | WHERE `owncloud_name` = ?'); |
266 | 266 | |
267 | 267 | return $this->modify($query, array($name)); |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | public function clear() { |
275 | 275 | $sql = $this->dbc |
276 | 276 | ->getDatabasePlatform() |
277 | - ->getTruncateTableSQL('`' . $this->getTableName() . '`'); |
|
277 | + ->getTruncateTableSQL('`'.$this->getTableName().'`'); |
|
278 | 278 | return $this->dbc->prepare($sql)->execute(); |
279 | 279 | } |
280 | 280 | |
@@ -293,9 +293,9 @@ discard block |
||
293 | 293 | ->from($this->getTableName()); |
294 | 294 | $cursor = $picker->execute(); |
295 | 295 | $result = true; |
296 | - while($id = $cursor->fetchColumn(0)) { |
|
296 | + while ($id = $cursor->fetchColumn(0)) { |
|
297 | 297 | $preCallback($id); |
298 | - if($isUnmapped = $this->unmap($id)) { |
|
298 | + if ($isUnmapped = $this->unmap($id)) { |
|
299 | 299 | $postCallback($id); |
300 | 300 | } |
301 | 301 | $result &= $isUnmapped; |
@@ -311,11 +311,11 @@ discard block |
||
311 | 311 | */ |
312 | 312 | public function count() { |
313 | 313 | $qb = $this->dbc->getQueryBuilder(); |
314 | - $query = $qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('ldap_dn') . ')')) |
|
314 | + $query = $qb->select($qb->createFunction('COUNT('.$qb->getColumnName('ldap_dn').')')) |
|
315 | 315 | ->from($this->getTableName()); |
316 | 316 | $res = $query->execute(); |
317 | 317 | $count = $res->fetchColumn(); |
318 | 318 | $res->closeCursor(); |
319 | - return (int)$count; |
|
319 | + return (int) $count; |
|
320 | 320 | } |
321 | 321 | } |
@@ -37,147 +37,147 @@ |
||
37 | 37 | */ |
38 | 38 | class CleanupRemoteStorages extends Command { |
39 | 39 | |
40 | - /** |
|
41 | - * @var IDBConnection |
|
42 | - */ |
|
43 | - protected $connection; |
|
44 | - |
|
45 | - public function __construct(IDBConnection $connection) { |
|
46 | - $this->connection = $connection; |
|
47 | - parent::__construct(); |
|
48 | - } |
|
49 | - |
|
50 | - protected function configure() { |
|
51 | - $this |
|
52 | - ->setName('sharing:cleanup-remote-storages') |
|
53 | - ->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table') |
|
54 | - ->addOption( |
|
55 | - 'dry-run', |
|
56 | - null, |
|
57 | - InputOption::VALUE_NONE, |
|
58 | - 'only show which storages would be deleted' |
|
59 | - ); |
|
60 | - } |
|
61 | - |
|
62 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
63 | - |
|
64 | - $remoteStorages = $this->getRemoteStorages(); |
|
65 | - |
|
66 | - $output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked'); |
|
67 | - |
|
68 | - $remoteShareIds = $this->getRemoteShareIds(); |
|
69 | - |
|
70 | - $output->writeln(count($remoteShareIds) . ' remote share(s) exist'); |
|
71 | - |
|
72 | - foreach ($remoteShareIds as $id => $remoteShareId) { |
|
73 | - if (isset($remoteStorages[$remoteShareId])) { |
|
74 | - if ($input->getOption('dry-run') || $output->isVerbose()) { |
|
75 | - $output->writeln("<info>$remoteShareId belongs to remote share $id</info>"); |
|
76 | - } |
|
77 | - |
|
78 | - unset($remoteStorages[$remoteShareId]); |
|
79 | - } else { |
|
80 | - $output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>"); |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - if (empty($remoteStorages)) { |
|
85 | - $output->writeln('<info>no storages deleted</info>'); |
|
86 | - } else { |
|
87 | - $dryRun = $input->getOption('dry-run'); |
|
88 | - foreach ($remoteStorages as $id => $numericId) { |
|
89 | - if ($dryRun) { |
|
90 | - $output->writeln("<error>$id [$numericId] can be deleted</error>"); |
|
91 | - $this->countFiles($numericId, $output); |
|
92 | - } else { |
|
93 | - $this->deleteStorage($id, $numericId, $output); |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - public function countFiles($numericId, OutputInterface $output) { |
|
100 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
101 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(' . $queryBuilder->getColumnName('fileid') . ')')) |
|
102 | - ->from('filecache') |
|
103 | - ->where($queryBuilder->expr()->eq( |
|
104 | - 'storage', |
|
105 | - $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
106 | - IQueryBuilder::PARAM_STR) |
|
107 | - ); |
|
108 | - $result = $queryBuilder->execute(); |
|
109 | - $count = $result->fetchColumn(); |
|
110 | - $output->writeln("$count files can be deleted for storage $numericId"); |
|
111 | - } |
|
112 | - |
|
113 | - public function deleteStorage($id, $numericId, OutputInterface $output) { |
|
114 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
115 | - $queryBuilder->delete('storages') |
|
116 | - ->where($queryBuilder->expr()->eq( |
|
117 | - 'id', |
|
118 | - $queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR), |
|
119 | - IQueryBuilder::PARAM_STR) |
|
120 | - ); |
|
121 | - $output->write("deleting $id [$numericId] ... "); |
|
122 | - $count = $queryBuilder->execute(); |
|
123 | - $output->writeln("deleted $count storage"); |
|
124 | - $this->deleteFiles($numericId, $output); |
|
125 | - } |
|
126 | - |
|
127 | - public function deleteFiles($numericId, OutputInterface $output) { |
|
128 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
129 | - $queryBuilder->delete('filecache') |
|
130 | - ->where($queryBuilder->expr()->eq( |
|
131 | - 'storage', |
|
132 | - $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
133 | - IQueryBuilder::PARAM_STR) |
|
134 | - ); |
|
135 | - $output->write("deleting files for storage $numericId ... "); |
|
136 | - $count = $queryBuilder->execute(); |
|
137 | - $output->writeln("deleted $count files"); |
|
138 | - } |
|
139 | - |
|
140 | - public function getRemoteStorages() { |
|
141 | - |
|
142 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
143 | - $queryBuilder->select(['id', 'numeric_id']) |
|
144 | - ->from('storages') |
|
145 | - ->where($queryBuilder->expr()->like( |
|
146 | - 'id', |
|
147 | - // match all 'shared::' + 32 characters storages |
|
148 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)), |
|
149 | - IQueryBuilder::PARAM_STR) |
|
150 | - ) |
|
151 | - ->andWhere($queryBuilder->expr()->notLike( |
|
152 | - 'id', |
|
153 | - // but not the ones starting with a '/', they are for normal shares |
|
154 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'), |
|
155 | - IQueryBuilder::PARAM_STR) |
|
156 | - )->orderBy('numeric_id'); |
|
157 | - $query = $queryBuilder->execute(); |
|
158 | - |
|
159 | - $remoteStorages = []; |
|
160 | - |
|
161 | - while ($row = $query->fetch()) { |
|
162 | - $remoteStorages[$row['id']] = $row['numeric_id']; |
|
163 | - } |
|
164 | - |
|
165 | - return $remoteStorages; |
|
166 | - } |
|
167 | - |
|
168 | - public function getRemoteShareIds() { |
|
169 | - |
|
170 | - $queryBuilder = $this->connection->getQueryBuilder(); |
|
171 | - $queryBuilder->select(['id', 'share_token', 'remote']) |
|
172 | - ->from('share_external'); |
|
173 | - $query = $queryBuilder->execute(); |
|
174 | - |
|
175 | - $remoteShareIds = []; |
|
176 | - |
|
177 | - while ($row = $query->fetch()) { |
|
178 | - $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $row['remote']); |
|
179 | - } |
|
180 | - |
|
181 | - return $remoteShareIds; |
|
182 | - } |
|
40 | + /** |
|
41 | + * @var IDBConnection |
|
42 | + */ |
|
43 | + protected $connection; |
|
44 | + |
|
45 | + public function __construct(IDBConnection $connection) { |
|
46 | + $this->connection = $connection; |
|
47 | + parent::__construct(); |
|
48 | + } |
|
49 | + |
|
50 | + protected function configure() { |
|
51 | + $this |
|
52 | + ->setName('sharing:cleanup-remote-storages') |
|
53 | + ->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table') |
|
54 | + ->addOption( |
|
55 | + 'dry-run', |
|
56 | + null, |
|
57 | + InputOption::VALUE_NONE, |
|
58 | + 'only show which storages would be deleted' |
|
59 | + ); |
|
60 | + } |
|
61 | + |
|
62 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
63 | + |
|
64 | + $remoteStorages = $this->getRemoteStorages(); |
|
65 | + |
|
66 | + $output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked'); |
|
67 | + |
|
68 | + $remoteShareIds = $this->getRemoteShareIds(); |
|
69 | + |
|
70 | + $output->writeln(count($remoteShareIds) . ' remote share(s) exist'); |
|
71 | + |
|
72 | + foreach ($remoteShareIds as $id => $remoteShareId) { |
|
73 | + if (isset($remoteStorages[$remoteShareId])) { |
|
74 | + if ($input->getOption('dry-run') || $output->isVerbose()) { |
|
75 | + $output->writeln("<info>$remoteShareId belongs to remote share $id</info>"); |
|
76 | + } |
|
77 | + |
|
78 | + unset($remoteStorages[$remoteShareId]); |
|
79 | + } else { |
|
80 | + $output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>"); |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + if (empty($remoteStorages)) { |
|
85 | + $output->writeln('<info>no storages deleted</info>'); |
|
86 | + } else { |
|
87 | + $dryRun = $input->getOption('dry-run'); |
|
88 | + foreach ($remoteStorages as $id => $numericId) { |
|
89 | + if ($dryRun) { |
|
90 | + $output->writeln("<error>$id [$numericId] can be deleted</error>"); |
|
91 | + $this->countFiles($numericId, $output); |
|
92 | + } else { |
|
93 | + $this->deleteStorage($id, $numericId, $output); |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + public function countFiles($numericId, OutputInterface $output) { |
|
100 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
101 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(' . $queryBuilder->getColumnName('fileid') . ')')) |
|
102 | + ->from('filecache') |
|
103 | + ->where($queryBuilder->expr()->eq( |
|
104 | + 'storage', |
|
105 | + $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
106 | + IQueryBuilder::PARAM_STR) |
|
107 | + ); |
|
108 | + $result = $queryBuilder->execute(); |
|
109 | + $count = $result->fetchColumn(); |
|
110 | + $output->writeln("$count files can be deleted for storage $numericId"); |
|
111 | + } |
|
112 | + |
|
113 | + public function deleteStorage($id, $numericId, OutputInterface $output) { |
|
114 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
115 | + $queryBuilder->delete('storages') |
|
116 | + ->where($queryBuilder->expr()->eq( |
|
117 | + 'id', |
|
118 | + $queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR), |
|
119 | + IQueryBuilder::PARAM_STR) |
|
120 | + ); |
|
121 | + $output->write("deleting $id [$numericId] ... "); |
|
122 | + $count = $queryBuilder->execute(); |
|
123 | + $output->writeln("deleted $count storage"); |
|
124 | + $this->deleteFiles($numericId, $output); |
|
125 | + } |
|
126 | + |
|
127 | + public function deleteFiles($numericId, OutputInterface $output) { |
|
128 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
129 | + $queryBuilder->delete('filecache') |
|
130 | + ->where($queryBuilder->expr()->eq( |
|
131 | + 'storage', |
|
132 | + $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR), |
|
133 | + IQueryBuilder::PARAM_STR) |
|
134 | + ); |
|
135 | + $output->write("deleting files for storage $numericId ... "); |
|
136 | + $count = $queryBuilder->execute(); |
|
137 | + $output->writeln("deleted $count files"); |
|
138 | + } |
|
139 | + |
|
140 | + public function getRemoteStorages() { |
|
141 | + |
|
142 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
143 | + $queryBuilder->select(['id', 'numeric_id']) |
|
144 | + ->from('storages') |
|
145 | + ->where($queryBuilder->expr()->like( |
|
146 | + 'id', |
|
147 | + // match all 'shared::' + 32 characters storages |
|
148 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)), |
|
149 | + IQueryBuilder::PARAM_STR) |
|
150 | + ) |
|
151 | + ->andWhere($queryBuilder->expr()->notLike( |
|
152 | + 'id', |
|
153 | + // but not the ones starting with a '/', they are for normal shares |
|
154 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'), |
|
155 | + IQueryBuilder::PARAM_STR) |
|
156 | + )->orderBy('numeric_id'); |
|
157 | + $query = $queryBuilder->execute(); |
|
158 | + |
|
159 | + $remoteStorages = []; |
|
160 | + |
|
161 | + while ($row = $query->fetch()) { |
|
162 | + $remoteStorages[$row['id']] = $row['numeric_id']; |
|
163 | + } |
|
164 | + |
|
165 | + return $remoteStorages; |
|
166 | + } |
|
167 | + |
|
168 | + public function getRemoteShareIds() { |
|
169 | + |
|
170 | + $queryBuilder = $this->connection->getQueryBuilder(); |
|
171 | + $queryBuilder->select(['id', 'share_token', 'remote']) |
|
172 | + ->from('share_external'); |
|
173 | + $query = $queryBuilder->execute(); |
|
174 | + |
|
175 | + $remoteShareIds = []; |
|
176 | + |
|
177 | + while ($row = $query->fetch()) { |
|
178 | + $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $row['remote']); |
|
179 | + } |
|
180 | + |
|
181 | + return $remoteShareIds; |
|
182 | + } |
|
183 | 183 | } |
@@ -63,11 +63,11 @@ discard block |
||
63 | 63 | |
64 | 64 | $remoteStorages = $this->getRemoteStorages(); |
65 | 65 | |
66 | - $output->writeln(count($remoteStorages) . ' remote storage(s) need(s) to be checked'); |
|
66 | + $output->writeln(count($remoteStorages).' remote storage(s) need(s) to be checked'); |
|
67 | 67 | |
68 | 68 | $remoteShareIds = $this->getRemoteShareIds(); |
69 | 69 | |
70 | - $output->writeln(count($remoteShareIds) . ' remote share(s) exist'); |
|
70 | + $output->writeln(count($remoteShareIds).' remote share(s) exist'); |
|
71 | 71 | |
72 | 72 | foreach ($remoteShareIds as $id => $remoteShareId) { |
73 | 73 | if (isset($remoteStorages[$remoteShareId])) { |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | |
99 | 99 | public function countFiles($numericId, OutputInterface $output) { |
100 | 100 | $queryBuilder = $this->connection->getQueryBuilder(); |
101 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(' . $queryBuilder->getColumnName('fileid') . ')')) |
|
101 | + $queryBuilder->select($queryBuilder->createFunction('COUNT('.$queryBuilder->getColumnName('fileid').')')) |
|
102 | 102 | ->from('filecache') |
103 | 103 | ->where($queryBuilder->expr()->eq( |
104 | 104 | 'storage', |
@@ -145,13 +145,13 @@ discard block |
||
145 | 145 | ->where($queryBuilder->expr()->like( |
146 | 146 | 'id', |
147 | 147 | // match all 'shared::' + 32 characters storages |
148 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)), |
|
148 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::').str_repeat('_', 32)), |
|
149 | 149 | IQueryBuilder::PARAM_STR) |
150 | 150 | ) |
151 | 151 | ->andWhere($queryBuilder->expr()->notLike( |
152 | 152 | 'id', |
153 | 153 | // but not the ones starting with a '/', they are for normal shares |
154 | - $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'), |
|
154 | + $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/').'%'), |
|
155 | 155 | IQueryBuilder::PARAM_STR) |
156 | 156 | )->orderBy('numeric_id'); |
157 | 157 | $query = $queryBuilder->execute(); |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | $remoteShareIds = []; |
176 | 176 | |
177 | 177 | while ($row = $query->fetch()) { |
178 | - $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $row['remote']); |
|
178 | + $remoteShareIds[$row['id']] = 'shared::'.md5($row['share_token'].'@'.$row['remote']); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | return $remoteShareIds; |