Skip to content

Commit ab2abfd

Browse files
committed
Provider functionality
Signed-off-by: Joas Schilling <[email protected]>
1 parent b4a640e commit ab2abfd

File tree

7 files changed

+160
-5
lines changed

7 files changed

+160
-5
lines changed

core/Controller/CollaborationResourcesController.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,26 @@
3131
use OCP\Collaboration\Resources\IResource;
3232
use OCP\Collaboration\Resources\ResourceException;
3333
use OCP\IRequest;
34+
use OCP\IUserSession;
3435

3536
class CollaborationResourcesController extends OCSController {
37+
3638
/** @var IManager */
3739
private $manager;
3840

41+
/** @var IUserSession */
42+
private $userSession;
43+
3944
public function __construct(
40-
$appName,
45+
string $appName,
4146
IRequest $request,
42-
IManager $manager
47+
IManager $manager,
48+
IUserSession $userSession
4349
) {
4450
parent::__construct($appName, $request);
4551

4652
$this->manager = $manager;
53+
$this->userSession = $userSession;
4754
}
4855

4956
/**
@@ -54,7 +61,7 @@ public function __construct(
5461
protected function getCollection(int $collectionId): ICollection {
5562
$collection = $this->manager->getCollection($collectionId);
5663

57-
if (false) { // TODO auth checking
64+
if (!$collection->canAccess($this->userSession->getUser())) {
5865
throw new CollectionException('Not found');
5966
}
6067

@@ -141,12 +148,15 @@ public function removeResource(int $collectionId, string $resourceType, string $
141148
*/
142149
public function getCollectionsByResource(string $resourceType, string $resourceId): DataResponse {
143150
try {
144-
// TODO auth checking
145151
$resource = $this->manager->getResource($resourceType, $resourceId);
146152
} catch (CollectionException $e) {
147153
return new DataResponse([], Http::STATUS_NOT_FOUND);
148154
}
149155

156+
if (!$resource->canAccess($this->userSession->getUser())) {
157+
return new DataResponse([], Http::STATUS_NOT_FOUND);
158+
}
159+
150160
return new DataResponse(array_map([$this, 'prepareCollection'], $resource->getCollections()));
151161
}
152162

@@ -157,7 +167,8 @@ protected function prepareCollection(ICollection $collection): array {
157167
protected function prepareResources(IResource $resource): array {
158168
return [
159169
'type' => $resource->getType(),
160-
'id' => $resource->getId()
170+
'id' => $resource->getId(),
171+
'name' => $resource->getName(),
161172
];
162173
}
163174
}

lib/private/Collaboration/Resources/Collection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCP\Collaboration\Resources\IResource;
3131
use OCP\DB\QueryBuilder\IQueryBuilder;
3232
use OCP\IDBConnection;
33+
use OCP\IUser;
3334

3435
class Collection implements ICollection {
3536

@@ -131,6 +132,23 @@ public function removeResource(IResource $resource) {
131132
}
132133
}
133134

135+
/**
136+
* Can a user/guest access the collection
137+
*
138+
* @param IUser $user
139+
* @return bool
140+
* @since 15.0.0
141+
*/
142+
public function canAccess(IUser $user = null): bool {
143+
foreach ($this->getResources() as $resource) {
144+
if ($resource->canAccess($user)) {
145+
return true;
146+
}
147+
}
148+
149+
return false;
150+
}
151+
134152
protected function isSameResource(IResource $resource1, IResource $resource2): bool {
135153
return $resource1->getType() === $resource2->getType() &&
136154
$resource1->getId() === $resource2->getId();

lib/private/Collaboration/Resources/Manager.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525

2626
use OCP\Collaboration\Resources\ICollection;
2727
use OCP\Collaboration\Resources\IManager;
28+
use OCP\Collaboration\Resources\IProvider;
2829
use OCP\Collaboration\Resources\IResource;
30+
use OCP\Collaboration\Resources\ResourceException;
2931
use OCP\IDBConnection;
32+
use OCP\IUser;
3033

3134
class Manager implements IManager {
3235

@@ -55,4 +58,51 @@ public function getCollection(int $id): ICollection {
5558
public function getResource(string $type, string $id): IResource {
5659
return new Resource($this, $this->connection, $type, $id);
5760
}
61+
62+
/**
63+
* @return IProvider[]
64+
* @since 15.0.0
65+
*/
66+
public function getProviders(): array {
67+
return [];
68+
}
69+
70+
/**
71+
* Get the display name of a resource
72+
*
73+
* @param IResource $resource
74+
* @return string
75+
* @since 15.0.0
76+
*/
77+
public function getName(IResource $resource): string {
78+
foreach ($this->getProviders() as $provider) {
79+
try {
80+
return $provider->getName($resource);
81+
} catch (ResourceException $e) {
82+
}
83+
}
84+
85+
return '';
86+
}
87+
88+
/**
89+
* Can a user/guest access the collection
90+
*
91+
* @param IResource $resource
92+
* @param IUser $user
93+
* @return bool
94+
* @since 15.0.0
95+
*/
96+
public function canAccess(IResource $resource, IUser $user = null): bool {
97+
foreach ($this->getProviders() as $provider) {
98+
try {
99+
if ($provider->canAccess($resource, $user)) {
100+
return true;
101+
}
102+
} catch (ResourceException $e) {
103+
}
104+
}
105+
106+
return false;
107+
}
58108
}

lib/private/Collaboration/Resources/Resource.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
use OCP\Collaboration\Resources\ICollection;
2727
use OCP\Collaboration\Resources\IManager;
2828
use OCP\Collaboration\Resources\IResource;
29+
use OCP\Collaboration\Resources\ResourceException;
2930
use OCP\IDBConnection;
31+
use OCP\IUser;
3032

3133
class Resource implements IResource {
3234

@@ -42,6 +44,9 @@ class Resource implements IResource {
4244
/** @var string */
4345
protected $id;
4446

47+
/** @var string|null */
48+
protected $name;
49+
4550
public function __construct(IManager $manager, IDBConnection $connection, string $type, string $id) {
4651
$this->manager = $manager;
4752
$this->connection = $connection;
@@ -65,6 +70,29 @@ public function getId(): string {
6570
return $this->id;
6671
}
6772

73+
/**
74+
* @return string
75+
* @since 15.0.0
76+
*/
77+
public function getName(): string {
78+
if ($this->name === null) {
79+
$this->name = $this->manager->getName($this);
80+
}
81+
82+
return $this->name;
83+
}
84+
85+
/**
86+
* Can a user/guest access the resource
87+
*
88+
* @param IUser $user
89+
* @return bool
90+
* @since 15.0.0
91+
*/
92+
public function canAccess(IUser $user = null): bool {
93+
return $this->manager->canAccess($this, $user);
94+
}
95+
6896
/**
6997
* @return ICollection[]
7098
* @since 15.0.0

lib/public/Collaboration/Resources/ICollection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace OCP\Collaboration\Resources;
2424

25+
use OCP\IUser;
26+
2527
/**
2628
* @since 15.0.0
2729
*/
@@ -49,4 +51,13 @@ public function addResource(IResource $resource);
4951
* @since 15.0.0
5052
*/
5153
public function removeResource(IResource $resource);
54+
55+
/**
56+
* Can a user/guest access the collection
57+
*
58+
* @param IUser $user
59+
* @return bool
60+
* @since 15.0.0
61+
*/
62+
public function canAccess(IUser $user = null): bool;
5263
}

lib/public/Collaboration/Resources/IProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,27 @@
2222

2323
namespace OCP\Collaboration\Resources;
2424

25+
use OCP\IUser;
2526

2627
interface IProvider {
2728

29+
/**
30+
* Get the display name of a resource
31+
*
32+
* @param IResource $resource
33+
* @return string
34+
* @since 15.0.0
35+
*/
36+
public function getName(IResource $resource): string;
37+
38+
/**
39+
* Can a user/guest access the collection
40+
*
41+
* @param IResource $resource
42+
* @param IUser $user
43+
* @return bool
44+
* @since 15.0.0
45+
*/
46+
public function canAccess(IResource $resource, IUser $user = null): bool;
47+
2848
}

lib/public/Collaboration/Resources/IResource.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace OCP\Collaboration\Resources;
2424

25+
use OCP\IUser;
26+
2527
/**
2628
* @since 15.0.0
2729
*/
@@ -39,6 +41,21 @@ public function getType(): string;
3941
*/
4042
public function getId(): string;
4143

44+
/**
45+
* @return string
46+
* @since 15.0.0
47+
*/
48+
public function getName(): string;
49+
50+
/**
51+
* Can a user/guest access the resource
52+
*
53+
* @param IUser $user
54+
* @return bool
55+
* @since 15.0.0
56+
*/
57+
public function canAccess(IUser $user = null): bool;
58+
4259
/**
4360
* @return ICollection[]
4461
* @since 15.0.0

0 commit comments

Comments
 (0)