summaryrefslogtreecommitdiffstats
path: root/lib/private/Collaboration/Resources/Collection.php
blob: c538580b8f8330a0eb09a32cba120de7f1bd6328 (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
<?php
declare(strict_types=1);
/**
 * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
 *
 * @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\ConstraintViolationException;
use OCP\Collaboration\Resources\IManager;
use OCP\Collaboration\Resources\ResourceException;
use OCP\Collaboration\Resources\ICollection;
use OCP\Collaboration\Resources\IResource;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;

class Collection implements ICollection {

	/** @var IManager */
	protected $manager;

	/** @var IDBConnection */
	protected $connection;

	/** @var int */
	protected $id;

	/** @var string */
	protected $name;

	/** @var bool|null */
	protected $access;

	/** @var IResource[] */
	protected $resources;

	public function __construct(
		IManager $manager,
		IDBConnection $connection,
		int $id,
		string $name,
		?bool $access
	) {
		$this->manager = $manager;
		$this->connection = $connection;
		$this->id = $id;
		$this->name = $name;
		$this->access = $access;
		$this->resources = [];
	}

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

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

	/**
	 * @return IResource[]
	 * @since 16.0.0
	 */
	public function getResources(): array {
		if (empty($this->resources)) {
			$query = $this->connection->getQueryBuilder();
			$query->select('resource_type', 'resource_id')
				->from('collres_resources')
				->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT)));

			$result = $query->execute();
			while ($row = $result->fetch()) {
				$this->resources[] = $this->manager->getResource($row['resource_type'], $row['resource_id']);
			}
			$result->closeCursor();
		}

		return $this->resources;
	}

	/**
	 * Adds a resource to a collection
	 *
	 * @param IResource $resource
	 * @throws ResourceException when the resource is already part of the collection
	 * @since 16.0.0
	 */
	public function addResource(IResource $resource) {
		array_map(function(IResource $r) use ($resource) {
			if ($this->isSameResource($r, $resource)) {
				throw new ResourceException('Already part of the collection');
			}
		}, $this->resources);

		$this->resources[] = $resource;

		$query = $this->connection->getQueryBuilder();
		$query->insert('collres_resources')
			->values([
				'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT),
				'resource_type' => $query->createNamedParameter($resource->getType()),
				'resource_id' => $query->createNamedParameter($resource->getId()),
			]);

		try {
			$query->execute();
		} catch (ConstraintViolationException $e) {
			throw new ResourceException('Already part of the collection');
		}
	}

	/**
	 * Removes a resource from a collection
	 *
	 * @param IResource $resource
	 * @since 16.0.0
	 */
	public function removeResource(IResource $resource) {
		$this->resources = array_filter($this->resources, function(IResource $r) use ($resource) {
			return !$this->isSameResource($r, $resource);
		});

		$query = $this->connection->getQueryBuilder();
		$query->delete('collres_resources')
			->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT)))
			->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType())))
			->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId())));
		$query->execute();

		if (empty($this->resources)) {
			$this->removeCollection();
		}
	}

	/**
	 * Can a user/guest access the collection
	 *
	 * @param IUser $user
	 * @return bool
	 * @since 16.0.0
	 */
	public function canAccess(IUser $user = null): bool {
		if ($this->access === null) {
			$this->access = false;
			foreach ($this->getResources() as $resource) {
				if ($resource->canAccess($user)) {
					$this->access = true;
				}
			}
		}

		return $this->access;
	}

	protected function isSameResource(IResource $resource1, IResource $resource2): bool {
		return $resource1->getType() === $resource2->getType() &&
			$resource1->getId() === $resource2->getId();
	}

	protected function removeCollection() {
		$query = $this->connection->getQueryBuilder();
		$query->delete('collres_collections')
			->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT)));
		$query->execute();

		$this->id = 0;
	}
}