summaryrefslogtreecommitdiffstats
path: root/lib/private/Collaboration/Resources/Manager.php
blob: a0fba16998fb2827499a3d14144b53bd572d637a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
 *
 * @author Daniel Kesselberg <mail@danielkesselberg.de>
 * @author Joas Schilling <coding@schilljs.com>
 * @author Julius Härtl <jus@bitgrid.net>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OC\Collaboration\Resources;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\Collaboration\Resources\CollectionException;
use OCP\Collaboration\Resources\ICollection;
use OCP\Collaboration\Resources\IManager;
use OCP\Collaboration\Resources\IProvider;
use OCP\Collaboration\Resources\IProviderManager;
use OCP\Collaboration\Resources\IResource;
use OCP\Collaboration\Resources\ResourceException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;
use Psr\Log\LoggerInterface;

class Manager implements IManager {
	public const TABLE_COLLECTIONS = 'collres_collections';
	public const TABLE_RESOURCES = 'collres_resources';
	public const TABLE_ACCESS_CACHE = 'collres_accesscache';

	/** @var IDBConnection */
	protected $connection;
	/** @var IProviderManager */
	protected $providerManager;
	/** @var LoggerInterface */
	protected $logger;

	/** @var string[] */
	protected $providers = [];


	public function __construct(IDBConnection $connection, IProviderManager $providerManager, LoggerInterface $logger) {
		$this->connection = $connection;
		$this->providerManager = $providerManager;
		$this->logger = $logger;
	}

	/**
	 * @param int $id
	 * @return ICollection
	 * @throws CollectionException when the collection could not be found
	 * @since 16.0.0
	 */
	public function getCollection(int $id): ICollection {
		$query = $this->connection->getQueryBuilder();
		$query->select('*')
			->from(self::TABLE_COLLECTIONS)
			->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
		$result = $query->execute();
		$row = $result->fetch();
		$result->closeCursor();

		if (!$row) {
			throw new CollectionException('Collection not found');
		}

		return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name']);
	}

	/**
	 * @param int $id
	 * @param IUser|null $user
	 * @return ICollection
	 * @throws CollectionException when the collection could not be found
	 * @since 16.0.0
	 */
	public function getCollectionForUser(int $id, ?IUser $user): ICollection {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->select('*')
			->from(self::TABLE_COLLECTIONS, 'c')
			->leftJoin(
				'c', self::TABLE_ACCESS_CACHE, 'a',
				$query->expr()->andX(
					$query->expr()->eq('c.id', 'a.collection_id'),
					$query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
				)
			)
			->where($query->expr()->eq('c.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
		$result = $query->execute();
		$row = $result->fetch();
		$result->closeCursor();

		if (!$row) {
			throw new CollectionException('Collection not found');
		}

		$access = $row['access'] === null ? null : (bool) $row['access'];
		if ($user instanceof IUser) {
			return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access);
		}

		return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access);
	}

	/**
	 * @param IUser $user
	 * @param string $filter
	 * @param int $limit
	 * @param int $start
	 * @return ICollection[]
	 * @since 16.0.0
	 */
	public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->select('c.*', 'a.access')
			->from(self::TABLE_COLLECTIONS, 'c')
			->leftJoin(
				'c', self::TABLE_ACCESS_CACHE, 'a',
				$query->expr()->andX(
					$query->expr()->eq('c.id', 'a.collection_id'),
					$query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
				)
			)
			->where($query->expr()->eq('a.access', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
			->orderBy('c.id')
			->setMaxResults($limit)
			->setFirstResult($start);

		if ($filter !== '') {
			$query->where($query->expr()->iLike('c.name', $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($filter) . '%')));
		}

		$result = $query->execute();
		$collections = [];

		$foundResults = 0;
		while ($row = $result->fetch()) {
			$foundResults++;
			$access = $row['access'] === null ? null : (bool) $row['access'];
			$collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access);
			if ($collection->canAccess($user)) {
				$collections[] = $collection;
			}
		}
		$result->closeCursor();

		if (empty($collections) && $foundResults === $limit) {
			return $this->searchCollections($user, $filter, $limit, $start + $limit);
		}

		return $collections;
	}

	/**
	 * @param string $name
	 * @return ICollection
	 * @since 16.0.0
	 */
	public function newCollection(string $name): ICollection {
		$query = $this->connection->getQueryBuilder();
		$query->insert(self::TABLE_COLLECTIONS)
			->values([
				'name' => $query->createNamedParameter($name),
			]);
		$query->execute();

		return new Collection($this, $this->connection, $query->getLastInsertId(), $name);
	}

	/**
	 * @param string $type
	 * @param string $id
	 * @return IResource
	 * @since 16.0.0
	 */
	public function createResource(string $type, string $id): IResource {
		return new Resource($this, $this->connection, $type, $id);
	}

	/**
	 * @param string $type
	 * @param string $id
	 * @param IUser|null $user
	 * @return IResource
	 * @throws ResourceException
	 * @since 16.0.0
	 */
	public function getResourceForUser(string $type, string $id, ?IUser $user): IResource {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->select('r.*', 'a.access')
			->from(self::TABLE_RESOURCES, 'r')
			->leftJoin(
				'r', self::TABLE_ACCESS_CACHE, 'a',
				$query->expr()->andX(
					$query->expr()->eq('r.resource_id', 'a.resource_id'),
					$query->expr()->eq('r.resource_type', 'a.resource_type'),
					$query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
				)
			)
			->where($query->expr()->eq('r.resource_type', $query->createNamedParameter($type, IQueryBuilder::PARAM_STR)))
			->andWhere($query->expr()->eq('r.resource_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_STR)));
		$result = $query->execute();
		$row = $result->fetch();
		$result->closeCursor();

		if (!$row) {
			throw new ResourceException('Resource not found');
		}

		$access = $row['access'] === null ? null : (bool) $row['access'];
		if ($user instanceof IUser) {
			return new Resource($this, $this->connection, $type, $id, $user, $access);
		}

		return new Resource($this, $this->connection, $type, $id, null, $access);
	}

	/**
	 * @param ICollection $collection
	 * @param IUser|null $user
	 * @return IResource[]
	 * @since 16.0.0
	 */
	public function getResourcesByCollectionForUser(ICollection $collection, ?IUser $user): array {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->select('r.*', 'a.access')
			->from(self::TABLE_RESOURCES, 'r')
			->leftJoin(
				'r', self::TABLE_ACCESS_CACHE, 'a',
				$query->expr()->andX(
					$query->expr()->eq('r.resource_id', 'a.resource_id'),
					$query->expr()->eq('r.resource_type', 'a.resource_type'),
					$query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
				)
			)
			->where($query->expr()->eq('r.collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT)));

		$resources = [];
		$result = $query->execute();
		while ($row = $result->fetch()) {
			$access = $row['access'] === null ? null : (bool) $row['access'];
			$resources[] = new Resource($this, $this->connection, $row['resource_type'], $row['resource_id'], $user, $access);
		}
		$result->closeCursor();

		return $resources;
	}

	/**
	 * Get the rich object data of a resource
	 *
	 * @param IResource $resource
	 * @return array
	 * @since 16.0.0
	 */
	public function getResourceRichObject(IResource $resource): array {
		foreach ($this->providerManager->getResourceProviders() as $provider) {
			if ($provider->getType() === $resource->getType()) {
				try {
					return $provider->getResourceRichObject($resource);
				} catch (ResourceException $e) {
				}
			}
		}

		return [];
	}

	/**
	 * Can a user/guest access the collection
	 *
	 * @param IResource $resource
	 * @param IUser|null $user
	 * @return bool
	 * @since 16.0.0
	 */
	public function canAccessResource(IResource $resource, ?IUser $user): bool {
		$access = $this->checkAccessCacheForUserByResource($resource, $user);
		if (\is_bool($access)) {
			return $access;
		}

		$access = false;
		foreach ($this->providerManager->getResourceProviders() as $provider) {
			if ($provider->getType() === $resource->getType()) {
				try {
					if ($provider->canAccessResource($resource, $user)) {
						$access = true;
						break;
					}
				} catch (ResourceException $e) {
				}
			}
		}

		$this->cacheAccessForResource($resource, $user, $access);
		return $access;
	}

	/**
	 * Can a user/guest access the collection
	 *
	 * @param ICollection $collection
	 * @param IUser|null $user
	 * @return bool
	 * @since 16.0.0
	 */
	public function canAccessCollection(ICollection $collection, ?IUser $user): bool {
		$access = $this->checkAccessCacheForUserByCollection($collection, $user);
		if (\is_bool($access)) {
			return $access;
		}

		$access = null;
		// Access is granted when a user can access all resources
		foreach ($collection->getResources() as $resource) {
			if (!$resource->canAccess($user)) {
				$access = false;
				break;
			}

			$access = true;
		}

		$this->cacheAccessForCollection($collection, $user, $access);
		return $access;
	}

	protected function checkAccessCacheForUserByResource(IResource $resource, ?IUser $user): ?bool {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->select('access')
			->from(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId(), IQueryBuilder::PARAM_STR)))
			->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR)))
			->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
			->setMaxResults(1);

		$hasAccess = null;
		$result = $query->execute();
		if ($row = $result->fetch()) {
			$hasAccess = (bool) $row['access'];
		}
		$result->closeCursor();

		return $hasAccess;
	}

	protected function checkAccessCacheForUserByCollection(ICollection $collection, ?IUser $user): ?bool {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->select('access')
			->from(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT)))
			->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
			->setMaxResults(1);

		$hasAccess = null;
		$result = $query->execute();
		if ($row = $result->fetch()) {
			$hasAccess = (bool) $row['access'];
		}
		$result->closeCursor();

		return $hasAccess;
	}

	public function cacheAccessForResource(IResource $resource, ?IUser $user, bool $access): void {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->insert(self::TABLE_ACCESS_CACHE)
			->values([
				'user_id' => $query->createNamedParameter($userId),
				'resource_id' => $query->createNamedParameter($resource->getId()),
				'resource_type' => $query->createNamedParameter($resource->getType()),
				'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL),
			]);
		try {
			$query->execute();
		} catch (UniqueConstraintViolationException $e) {
		}
	}

	public function cacheAccessForCollection(ICollection $collection, ?IUser $user, bool $access): void {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->insert(self::TABLE_ACCESS_CACHE)
			->values([
				'user_id' => $query->createNamedParameter($userId),
				'collection_id' => $query->createNamedParameter($collection->getId()),
				'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL),
			]);
		try {
			$query->execute();
		} catch (UniqueConstraintViolationException $e) {
		}
	}

	public function invalidateAccessCacheForUser(?IUser $user): void {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
		$query->execute();
	}

	public function invalidateAccessCacheForResource(IResource $resource): void {
		$query = $this->connection->getQueryBuilder();

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId())))
			->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR)));
		$query->execute();

		foreach ($resource->getCollections() as $collection) {
			$this->invalidateAccessCacheForCollection($collection);
		}
	}

	public function invalidateAccessCacheForAllCollections(): void {
		$query = $this->connection->getQueryBuilder();

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->neq('collection_id', $query->createNamedParameter(0)));
		$query->execute();
	}

	public function invalidateAccessCacheForCollection(ICollection $collection): void {
		$query = $this->connection->getQueryBuilder();

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId())));
		$query->execute();
	}

	public function invalidateAccessCacheForProvider(IProvider $provider): void {
		$query = $this->connection->getQueryBuilder();

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR)));
		$query->execute();
	}

	public function invalidateAccessCacheForResourceByUser(IResource $resource, ?IUser $user): void {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId())))
			->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
		$query->execute();

		foreach ($resource->getCollections() as $collection) {
			$this->invalidateAccessCacheForCollectionByUser($collection, $user);
		}
	}

	protected function invalidateAccessCacheForCollectionByUser(ICollection $collection, ?IUser $user): void {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId())))
			->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
		$query->execute();
	}

	public function invalidateAccessCacheForProviderByUser(IProvider $provider, ?IUser $user): void {
		$query = $this->connection->getQueryBuilder();
		$userId = $user instanceof IUser ? $user->getUID() : '';

		$query->delete(self::TABLE_ACCESS_CACHE)
			->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR)))
			->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
		$query->execute();
	}

	/**
	 * @param string $provider
	 */
	public function registerResourceProvider(string $provider): void {
		$this->logger->debug('\OC\Collaboration\Resources\Manager::registerResourceProvider is deprecated', ['provider' => $provider]);
		$this->providerManager->registerResourceProvider($provider);
	}

	/**
	 * Get the resource type of the provider
	 *
	 * @return string
	 * @since 16.0.0
	 */
	public function getType(): string {
		return '';
	}
}