1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2018 Joas Schilling <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @license GNU AGPL version 3 or any later version |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
11
|
|
|
* License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OC\Core\Controller; |
24
|
|
|
|
25
|
|
|
use OCP\AppFramework\Http; |
26
|
|
|
use OCP\AppFramework\OCSController; |
|
|
|
|
27
|
|
|
use OCP\AppFramework\Http\DataResponse; |
28
|
|
|
use OCP\Collaboration\Resources\CollectionException; |
29
|
|
|
use OCP\Collaboration\Resources\ICollection; |
30
|
|
|
use OCP\Collaboration\Resources\IManager; |
31
|
|
|
use OCP\Collaboration\Resources\IResource; |
32
|
|
|
use OCP\Collaboration\Resources\ResourceException; |
33
|
|
|
use OCP\IRequest; |
34
|
|
|
use OCP\IUserSession; |
35
|
|
|
|
36
|
|
|
class CollaborationResourcesController extends OCSController { |
37
|
|
|
|
38
|
|
|
/** @var IManager */ |
39
|
|
|
private $manager; |
40
|
|
|
|
41
|
|
|
/** @var IUserSession */ |
42
|
|
|
private $userSession; |
43
|
|
|
|
44
|
|
|
public function __construct( |
45
|
|
|
string $appName, |
46
|
|
|
IRequest $request, |
47
|
|
|
IManager $manager, |
48
|
|
|
IUserSession $userSession |
49
|
|
|
) { |
50
|
|
|
parent::__construct($appName, $request); |
51
|
|
|
|
52
|
|
|
$this->manager = $manager; |
53
|
|
|
$this->userSession = $userSession; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param int $collectionId |
58
|
|
|
* @return ICollection |
59
|
|
|
* @throws CollectionException when the collection was not found for the user |
60
|
|
|
*/ |
61
|
|
|
protected function getCollection(int $collectionId): ICollection { |
62
|
|
|
$collection = $this->manager->getCollectionForUser($collectionId, $this->userSession->getUser()); |
63
|
|
|
|
64
|
|
|
if (!$collection->canAccess($this->userSession->getUser())) { |
65
|
|
|
throw new CollectionException('Not found'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $collection; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @NoAdminRequired |
73
|
|
|
* |
74
|
|
|
* @param int $collectionId |
75
|
|
|
* @return DataResponse |
76
|
|
|
*/ |
77
|
|
|
public function listCollection(int $collectionId): DataResponse { |
78
|
|
|
try { |
79
|
|
|
$collection = $this->getCollection($collectionId); |
80
|
|
|
} catch (CollectionException $e) { |
81
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return new DataResponse($this->prepareCollection($collection)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @NoAdminRequired |
89
|
|
|
* |
90
|
|
|
* @param string $filter |
91
|
|
|
* @return DataResponse |
92
|
|
|
*/ |
93
|
|
|
public function searchCollections(string $filter): DataResponse { |
94
|
|
|
try { |
95
|
|
|
$collections = $this->manager->searchCollections($this->userSession->getUser(), $filter); |
|
|
|
|
96
|
|
|
} catch (CollectionException $e) { |
97
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return new DataResponse(array_map([$this, 'prepareCollection'], $collections)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @NoAdminRequired |
105
|
|
|
* |
106
|
|
|
* @param int $collectionId |
107
|
|
|
* @param string $resourceType |
108
|
|
|
* @param string $resourceId |
109
|
|
|
* @return DataResponse |
110
|
|
|
*/ |
111
|
|
|
public function addResource(int $collectionId, string $resourceType, string $resourceId): DataResponse { |
112
|
|
|
try { |
113
|
|
|
$collection = $this->getCollection($collectionId); |
114
|
|
|
} catch (CollectionException $e) { |
115
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$resource = $this->manager->createResource($resourceType, $resourceId); |
119
|
|
|
|
120
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) { |
121
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
try { |
125
|
|
|
$collection->addResource($resource); |
126
|
|
|
} catch (ResourceException $e) { |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return new DataResponse($this->prepareCollection($collection)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @NoAdminRequired |
134
|
|
|
* |
135
|
|
|
* @param int $collectionId |
136
|
|
|
* @param string $resourceType |
137
|
|
|
* @param string $resourceId |
138
|
|
|
* @return DataResponse |
139
|
|
|
*/ |
140
|
|
|
public function removeResource(int $collectionId, string $resourceType, string $resourceId): DataResponse { |
141
|
|
|
try { |
142
|
|
|
$collection = $this->getCollection($collectionId); |
143
|
|
|
} catch (CollectionException $e) { |
144
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
$resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser()); |
149
|
|
|
} catch (CollectionException $e) { |
150
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$collection->removeResource($resource); |
154
|
|
|
|
155
|
|
|
return new DataResponse($this->prepareCollection($collection)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @NoAdminRequired |
160
|
|
|
* |
161
|
|
|
* @param string $resourceType |
162
|
|
|
* @param string $resourceId |
163
|
|
|
* @return DataResponse |
164
|
|
|
*/ |
165
|
|
|
public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse { |
166
|
|
|
try { |
167
|
|
|
$resource = $this->manager->getResourceForUser($resourceType, $resourceId, $this->userSession->getUser()); |
168
|
|
|
} catch (ResourceException $e) { |
169
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) { |
173
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return new DataResponse(array_map([$this, 'prepareCollection'], $resource->getCollections())); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @NoAdminRequired |
181
|
|
|
* |
182
|
|
|
* @param string $baseResourceType |
183
|
|
|
* @param string $baseResourceId |
184
|
|
|
* @param string $name |
185
|
|
|
* @return DataResponse |
186
|
|
|
*/ |
187
|
|
|
public function createCollectionOnResource(string $baseResourceType, string $baseResourceId, string $name): DataResponse { |
188
|
|
|
if (!isset($name[0]) || isset($name[64])) { |
189
|
|
|
return new DataResponse([], Http::STATUS_BAD_REQUEST); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
try { |
193
|
|
|
$resource = $this->manager->createResource($baseResourceType, $baseResourceId); |
194
|
|
|
} catch (CollectionException $e) { |
195
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) { |
199
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$collection = $this->manager->newCollection($name); |
203
|
|
|
$collection->addResource($resource); |
204
|
|
|
|
205
|
|
|
return new DataResponse($this->prepareCollection($collection)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @NoAdminRequired |
210
|
|
|
* |
211
|
|
|
* @param int $collectionId |
212
|
|
|
* @param string $collectionName |
213
|
|
|
* @return DataResponse |
214
|
|
|
*/ |
215
|
|
|
public function renameCollection(int $collectionId, string $collectionName): DataResponse { |
216
|
|
|
try { |
217
|
|
|
$collection = $this->getCollection($collectionId); |
218
|
|
|
} catch (CollectionException $exception) { |
219
|
|
|
return new DataResponse([], Http::STATUS_NOT_FOUND); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$collection->setName($collectionName); |
223
|
|
|
|
224
|
|
|
return new DataResponse($this->prepareCollection($collection)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
protected function prepareCollection(ICollection $collection): array { |
228
|
|
|
if (!$collection->canAccess($this->userSession->getUser())) { |
229
|
|
|
return null; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return [ |
233
|
|
|
'id' => $collection->getId(), |
234
|
|
|
'name' => $collection->getName(), |
235
|
|
|
'resources' => array_values(array_filter(array_map([$this, 'prepareResources'], $collection->getResources()))), |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
protected function prepareResources(IResource $resource): ?array { |
240
|
|
|
if (!$resource->canAccess($this->userSession->getUser())) { |
241
|
|
|
return null; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return [ |
245
|
|
|
'type' => $resource->getType(), |
246
|
|
|
'id' => $resource->getId(), |
247
|
|
|
'name' => $resource->getName(), |
248
|
|
|
'iconClass' => $resource->getIconClass(), |
249
|
|
|
'link' => $resource->getLink(), |
250
|
|
|
]; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: