aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Collaboration/Resources/Resource.php
blob: ae011e319de238009e29f50da079f4132ccd274b (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
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Collaboration\Resources;

use OCP\Collaboration\Resources\ICollection;
use OCP\Collaboration\Resources\IManager;
use OCP\Collaboration\Resources\IResource;
use OCP\IDBConnection;
use OCP\IUser;

class Resource implements IResource {
	protected ?array $data = null;

	public function __construct(
		protected IManager $manager,
		protected IDBConnection $connection,
		protected string $type,
		protected string $id,
		protected ?IUser $userForAccess = null,
		protected ?bool $access = null
	) {
	}

	/**
	 * @since 16.0.0
	 */
	public function getType(): string {
		return $this->type;
	}

	/**
	 * @since 16.0.0
	 */
	public function getId(): string {
		return $this->id;
	}

	/**
	 * @since 16.0.0
	 */
	public function getRichObject(): array {
		if ($this->data === null) {
			$this->data = $this->manager->getResourceRichObject($this);
		}

		return $this->data;
	}

	/**
	 * Can a user/guest access the resource
	 *
	 * @since 16.0.0
	 */
	public function canAccess(?IUser $user): bool {
		if ($user instanceof IUser) {
			return $this->canUserAccess($user);
		}
		return $this->canGuestAccess();
	}

	protected function canUserAccess(IUser $user): bool {
		if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
			return $this->access;
		}

		$access = $this->manager->canAccessResource($this, $user);
		if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) {
			$this->access = $access;
		}
		return $access;
	}

	protected function canGuestAccess(): bool {
		if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) {
			return $this->access;
		}

		$access = $this->manager->canAccessResource($this, null);
		if (!$this->userForAccess instanceof IUser) {
			$this->access = $access;
		}
		return $access;
	}

	/**
	 * @return ICollection[]
	 * @since 16.0.0
	 */
	public function getCollections(): array {
		$collections = [];

		$query = $this->connection->getQueryBuilder();

		$query->select('collection_id')
			->from('collres_resources')
			->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType())))
			->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId())));

		$result = $query->execute();
		while ($row = $result->fetch()) {
			$collections[] = $this->manager->getCollection((int) $row['collection_id']);
		}
		$result->closeCursor();

		return $collections;
	}
}