@@ -19,268 +19,268 @@ |
||
19 | 19 | use Psr\Log\LoggerInterface; |
20 | 20 | |
21 | 21 | abstract class Backend { |
22 | - use TTransactional; |
|
23 | - public const ACCESS_OWNER = 1; |
|
24 | - |
|
25 | - public const ACCESS_READ_WRITE = 2; |
|
26 | - public const ACCESS_READ = 3; |
|
27 | - // 4 is already in use for public calendars |
|
28 | - public const ACCESS_UNSHARED = 5; |
|
29 | - |
|
30 | - private ICache $shareCache; |
|
31 | - |
|
32 | - public function __construct( |
|
33 | - private IUserManager $userManager, |
|
34 | - private IGroupManager $groupManager, |
|
35 | - private Principal $principalBackend, |
|
36 | - private RemoteUserPrincipalBackend $remoteUserPrincipalBackend, |
|
37 | - private ICacheFactory $cacheFactory, |
|
38 | - private SharingService $service, |
|
39 | - // TODO: Make `FederationSharingService` abstract once we support federated address book |
|
40 | - // sharing. The abstract sharing backend should not take a service scoped to calendars |
|
41 | - // by default. |
|
42 | - private FederationSharingService $federationSharingService, |
|
43 | - private LoggerInterface $logger, |
|
44 | - ) { |
|
45 | - $this->shareCache = $this->cacheFactory->createInMemory(); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
50 | - * @param list<string> $remove |
|
51 | - */ |
|
52 | - public function updateShares(IShareable $shareable, array $add, array $remove, array $oldShares = []): void { |
|
53 | - $this->shareCache->clear(); |
|
54 | - foreach ($add as $element) { |
|
55 | - // Hacky code below ... shouldn't we check the whole (principal) root collection instead? |
|
56 | - $principal = $this->principalBackend->findByUri($element['href'], '') |
|
57 | - ?? $this->remoteUserPrincipalBackend->findByUri($element['href'], ''); |
|
58 | - if (empty($principal)) { |
|
59 | - continue; |
|
60 | - } |
|
61 | - |
|
62 | - // We need to validate manually because some principals are only virtual |
|
63 | - // i.e. Group principals |
|
64 | - $principalparts = explode('/', $principal, 3); |
|
65 | - if (count($principalparts) !== 3 || $principalparts[0] !== 'principals' || !in_array($principalparts[1], ['users', 'groups', 'circles', 'remote-users'], true)) { |
|
66 | - // Invalid principal |
|
67 | - continue; |
|
68 | - } |
|
69 | - |
|
70 | - // Don't add share for owner |
|
71 | - if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
72 | - continue; |
|
73 | - } |
|
74 | - |
|
75 | - $principalparts[2] = urldecode($principalparts[2]); |
|
76 | - if (($principalparts[1] === 'users' && !$this->userManager->userExists($principalparts[2])) |
|
77 | - || ($principalparts[1] === 'groups' && !$this->groupManager->groupExists($principalparts[2]))) { |
|
78 | - // User or group does not exist |
|
79 | - continue; |
|
80 | - } |
|
81 | - |
|
82 | - $access = Backend::ACCESS_READ; |
|
83 | - if (isset($element['readOnly'])) { |
|
84 | - $access = $element['readOnly'] ? Backend::ACCESS_READ : Backend::ACCESS_READ_WRITE; |
|
85 | - } |
|
86 | - |
|
87 | - if ($principalparts[1] === 'remote-users') { |
|
88 | - $this->federationSharingService->shareWith($shareable, $principal, $access); |
|
89 | - } else { |
|
90 | - $this->service->shareWith($shareable->getResourceId(), $principal, $access); |
|
91 | - } |
|
92 | - } |
|
93 | - foreach ($remove as $element) { |
|
94 | - // Hacky code below ... shouldn't we check the whole (principal) root collection instead? |
|
95 | - $principal = $this->principalBackend->findByUri($element, '') |
|
96 | - ?? $this->remoteUserPrincipalBackend->findByUri($element, ''); |
|
97 | - if (empty($principal)) { |
|
98 | - continue; |
|
99 | - } |
|
100 | - |
|
101 | - // Don't add unshare for owner |
|
102 | - if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
103 | - continue; |
|
104 | - } |
|
105 | - |
|
106 | - // Delete any possible direct shares (since the frontend does not separate between them) |
|
107 | - $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - public function deleteAllShares(int $resourceId): void { |
|
112 | - $this->shareCache->clear(); |
|
113 | - $this->service->deleteAllShares($resourceId); |
|
114 | - } |
|
115 | - |
|
116 | - public function deleteAllSharesByUser(string $principaluri): void { |
|
117 | - $this->shareCache->clear(); |
|
118 | - $this->service->deleteAllSharesByUser($principaluri); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Returns the list of people whom this resource is shared with. |
|
123 | - * |
|
124 | - * Every element in this array should have the following properties: |
|
125 | - * * href - Often a mailto: address |
|
126 | - * * commonName - Optional, for example a first + last name |
|
127 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
128 | - * * readOnly - boolean |
|
129 | - * |
|
130 | - * @param int $resourceId |
|
131 | - * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
132 | - */ |
|
133 | - public function getShares(int $resourceId): array { |
|
134 | - /** @var list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}>|null $cached */ |
|
135 | - $cached = $this->shareCache->get((string)$resourceId); |
|
136 | - if (is_array($cached)) { |
|
137 | - return $cached; |
|
138 | - } |
|
139 | - |
|
140 | - $rows = $this->service->getShares($resourceId); |
|
141 | - $shares = []; |
|
142 | - foreach ($rows as $row) { |
|
143 | - $p = $this->getPrincipalByPath($row['principaluri']); |
|
144 | - $shares[] = [ |
|
145 | - 'href' => "principal:{$row['principaluri']}", |
|
146 | - 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
147 | - 'status' => 1, |
|
148 | - 'readOnly' => (int)$row['access'] === Backend::ACCESS_READ, |
|
149 | - '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
150 | - '{http://owncloud.org/ns}group-share' => isset($p['uri']) && (str_starts_with($p['uri'], 'principals/groups') || str_starts_with($p['uri'], 'principals/circles')), |
|
151 | - ]; |
|
152 | - } |
|
153 | - $this->shareCache->set((string)$resourceId, $shares); |
|
154 | - return $shares; |
|
155 | - } |
|
156 | - |
|
157 | - public function preloadShares(array $resourceIds): void { |
|
158 | - $resourceIds = array_filter($resourceIds, function (int $resourceId) { |
|
159 | - return empty($this->shareCache->get((string)$resourceId)); |
|
160 | - }); |
|
161 | - if (empty($resourceIds)) { |
|
162 | - return; |
|
163 | - } |
|
164 | - |
|
165 | - $rows = $this->service->getSharesForIds($resourceIds); |
|
166 | - $sharesByResource = array_fill_keys($resourceIds, []); |
|
167 | - foreach ($rows as $row) { |
|
168 | - $resourceId = (int)$row['resourceid']; |
|
169 | - $p = $this->getPrincipalByPath($row['principaluri']); |
|
170 | - $sharesByResource[$resourceId][] = [ |
|
171 | - 'href' => "principal:{$row['principaluri']}", |
|
172 | - 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
173 | - 'status' => 1, |
|
174 | - 'readOnly' => (int)$row['access'] === self::ACCESS_READ, |
|
175 | - '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
176 | - '{http://owncloud.org/ns}group-share' => isset($p['uri']) && str_starts_with($p['uri'], 'principals/groups') |
|
177 | - ]; |
|
178 | - $this->shareCache->set((string)$resourceId, $sharesByResource[$resourceId]); |
|
179 | - } |
|
180 | - |
|
181 | - // Also remember resources with no shares to prevent superfluous (empty) queries later on |
|
182 | - foreach ($resourceIds as $resourceId) { |
|
183 | - $hasShares = false; |
|
184 | - foreach ($rows as $row) { |
|
185 | - if ((int)$row['resourceid'] === $resourceId) { |
|
186 | - $hasShares = true; |
|
187 | - break; |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - if ($hasShares) { |
|
192 | - continue; |
|
193 | - } |
|
194 | - |
|
195 | - $this->shareCache->set((string)$resourceId, []); |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * For shared resources the sharee is set in the ACL of the resource |
|
201 | - * |
|
202 | - * @param int $resourceId |
|
203 | - * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
204 | - * @param list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> $shares |
|
205 | - * @return list<array{principal: string, privilege: string, protected: bool}> |
|
206 | - */ |
|
207 | - public function applyShareAcl(array $shares, array $acl): array { |
|
208 | - foreach ($shares as $share) { |
|
209 | - $acl[] = [ |
|
210 | - 'privilege' => '{DAV:}read', |
|
211 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
212 | - 'protected' => true, |
|
213 | - ]; |
|
214 | - if (!$share['readOnly']) { |
|
215 | - $acl[] = [ |
|
216 | - 'privilege' => '{DAV:}write', |
|
217 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
218 | - 'protected' => true, |
|
219 | - ]; |
|
220 | - } elseif (in_array($this->service->getResourceType(), ['calendar','addressbook'])) { |
|
221 | - // Allow changing the properties of read only calendars, |
|
222 | - // so users can change the visibility. |
|
223 | - $acl[] = [ |
|
224 | - 'privilege' => '{DAV:}write-properties', |
|
225 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
226 | - 'protected' => true, |
|
227 | - ]; |
|
228 | - } |
|
229 | - } |
|
230 | - return $acl; |
|
231 | - } |
|
232 | - |
|
233 | - public function unshare(IShareable $shareable, string $principalUri): bool { |
|
234 | - $this->shareCache->clear(); |
|
235 | - |
|
236 | - $principal = $this->principalBackend->findByUri($principalUri, ''); |
|
237 | - if (empty($principal)) { |
|
238 | - return false; |
|
239 | - } |
|
240 | - |
|
241 | - if ($shareable->getOwner() === $principal) { |
|
242 | - return false; |
|
243 | - } |
|
244 | - |
|
245 | - // Delete any possible direct shares (since the frontend does not separate between them) |
|
246 | - $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
247 | - |
|
248 | - $needsUnshare = $this->hasAccessByGroupOrCirclesMembership( |
|
249 | - $shareable->getResourceId(), |
|
250 | - $principal |
|
251 | - ); |
|
252 | - |
|
253 | - if ($needsUnshare) { |
|
254 | - $this->service->unshare($shareable->getResourceId(), $principal); |
|
255 | - } |
|
256 | - |
|
257 | - return true; |
|
258 | - } |
|
259 | - |
|
260 | - private function hasAccessByGroupOrCirclesMembership(int $resourceId, string $principal) { |
|
261 | - $memberships = array_merge( |
|
262 | - $this->principalBackend->getGroupMembership($principal, true), |
|
263 | - $this->principalBackend->getCircleMembership($principal) |
|
264 | - ); |
|
265 | - |
|
266 | - $shares = array_column( |
|
267 | - $this->service->getShares($resourceId), |
|
268 | - 'principaluri' |
|
269 | - ); |
|
270 | - |
|
271 | - return count(array_intersect($memberships, $shares)) > 0; |
|
272 | - } |
|
273 | - |
|
274 | - public function getSharesByShareePrincipal(string $principal): array { |
|
275 | - return $this->service->getSharesByPrincipals([$principal]); |
|
276 | - } |
|
277 | - |
|
278 | - private function getPrincipalByPath(string $principalUri): ?array { |
|
279 | - // Hacky code below ... shouldn't we check the whole (principal) root collection instead? |
|
280 | - if (str_starts_with($principalUri, RemoteUserPrincipalBackend::PRINCIPAL_PREFIX)) { |
|
281 | - return $this->remoteUserPrincipalBackend->getPrincipalByPath($principalUri); |
|
282 | - } |
|
283 | - |
|
284 | - return $this->principalBackend->getPrincipalByPath($principalUri); |
|
285 | - } |
|
22 | + use TTransactional; |
|
23 | + public const ACCESS_OWNER = 1; |
|
24 | + |
|
25 | + public const ACCESS_READ_WRITE = 2; |
|
26 | + public const ACCESS_READ = 3; |
|
27 | + // 4 is already in use for public calendars |
|
28 | + public const ACCESS_UNSHARED = 5; |
|
29 | + |
|
30 | + private ICache $shareCache; |
|
31 | + |
|
32 | + public function __construct( |
|
33 | + private IUserManager $userManager, |
|
34 | + private IGroupManager $groupManager, |
|
35 | + private Principal $principalBackend, |
|
36 | + private RemoteUserPrincipalBackend $remoteUserPrincipalBackend, |
|
37 | + private ICacheFactory $cacheFactory, |
|
38 | + private SharingService $service, |
|
39 | + // TODO: Make `FederationSharingService` abstract once we support federated address book |
|
40 | + // sharing. The abstract sharing backend should not take a service scoped to calendars |
|
41 | + // by default. |
|
42 | + private FederationSharingService $federationSharingService, |
|
43 | + private LoggerInterface $logger, |
|
44 | + ) { |
|
45 | + $this->shareCache = $this->cacheFactory->createInMemory(); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @param list<array{href: string, commonName: string, readOnly: bool}> $add |
|
50 | + * @param list<string> $remove |
|
51 | + */ |
|
52 | + public function updateShares(IShareable $shareable, array $add, array $remove, array $oldShares = []): void { |
|
53 | + $this->shareCache->clear(); |
|
54 | + foreach ($add as $element) { |
|
55 | + // Hacky code below ... shouldn't we check the whole (principal) root collection instead? |
|
56 | + $principal = $this->principalBackend->findByUri($element['href'], '') |
|
57 | + ?? $this->remoteUserPrincipalBackend->findByUri($element['href'], ''); |
|
58 | + if (empty($principal)) { |
|
59 | + continue; |
|
60 | + } |
|
61 | + |
|
62 | + // We need to validate manually because some principals are only virtual |
|
63 | + // i.e. Group principals |
|
64 | + $principalparts = explode('/', $principal, 3); |
|
65 | + if (count($principalparts) !== 3 || $principalparts[0] !== 'principals' || !in_array($principalparts[1], ['users', 'groups', 'circles', 'remote-users'], true)) { |
|
66 | + // Invalid principal |
|
67 | + continue; |
|
68 | + } |
|
69 | + |
|
70 | + // Don't add share for owner |
|
71 | + if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
72 | + continue; |
|
73 | + } |
|
74 | + |
|
75 | + $principalparts[2] = urldecode($principalparts[2]); |
|
76 | + if (($principalparts[1] === 'users' && !$this->userManager->userExists($principalparts[2])) |
|
77 | + || ($principalparts[1] === 'groups' && !$this->groupManager->groupExists($principalparts[2]))) { |
|
78 | + // User or group does not exist |
|
79 | + continue; |
|
80 | + } |
|
81 | + |
|
82 | + $access = Backend::ACCESS_READ; |
|
83 | + if (isset($element['readOnly'])) { |
|
84 | + $access = $element['readOnly'] ? Backend::ACCESS_READ : Backend::ACCESS_READ_WRITE; |
|
85 | + } |
|
86 | + |
|
87 | + if ($principalparts[1] === 'remote-users') { |
|
88 | + $this->federationSharingService->shareWith($shareable, $principal, $access); |
|
89 | + } else { |
|
90 | + $this->service->shareWith($shareable->getResourceId(), $principal, $access); |
|
91 | + } |
|
92 | + } |
|
93 | + foreach ($remove as $element) { |
|
94 | + // Hacky code below ... shouldn't we check the whole (principal) root collection instead? |
|
95 | + $principal = $this->principalBackend->findByUri($element, '') |
|
96 | + ?? $this->remoteUserPrincipalBackend->findByUri($element, ''); |
|
97 | + if (empty($principal)) { |
|
98 | + continue; |
|
99 | + } |
|
100 | + |
|
101 | + // Don't add unshare for owner |
|
102 | + if ($shareable->getOwner() !== null && strcasecmp($shareable->getOwner(), $principal) === 0) { |
|
103 | + continue; |
|
104 | + } |
|
105 | + |
|
106 | + // Delete any possible direct shares (since the frontend does not separate between them) |
|
107 | + $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + public function deleteAllShares(int $resourceId): void { |
|
112 | + $this->shareCache->clear(); |
|
113 | + $this->service->deleteAllShares($resourceId); |
|
114 | + } |
|
115 | + |
|
116 | + public function deleteAllSharesByUser(string $principaluri): void { |
|
117 | + $this->shareCache->clear(); |
|
118 | + $this->service->deleteAllSharesByUser($principaluri); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Returns the list of people whom this resource is shared with. |
|
123 | + * |
|
124 | + * Every element in this array should have the following properties: |
|
125 | + * * href - Often a mailto: address |
|
126 | + * * commonName - Optional, for example a first + last name |
|
127 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
128 | + * * readOnly - boolean |
|
129 | + * |
|
130 | + * @param int $resourceId |
|
131 | + * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> |
|
132 | + */ |
|
133 | + public function getShares(int $resourceId): array { |
|
134 | + /** @var list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}>|null $cached */ |
|
135 | + $cached = $this->shareCache->get((string)$resourceId); |
|
136 | + if (is_array($cached)) { |
|
137 | + return $cached; |
|
138 | + } |
|
139 | + |
|
140 | + $rows = $this->service->getShares($resourceId); |
|
141 | + $shares = []; |
|
142 | + foreach ($rows as $row) { |
|
143 | + $p = $this->getPrincipalByPath($row['principaluri']); |
|
144 | + $shares[] = [ |
|
145 | + 'href' => "principal:{$row['principaluri']}", |
|
146 | + 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
147 | + 'status' => 1, |
|
148 | + 'readOnly' => (int)$row['access'] === Backend::ACCESS_READ, |
|
149 | + '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
150 | + '{http://owncloud.org/ns}group-share' => isset($p['uri']) && (str_starts_with($p['uri'], 'principals/groups') || str_starts_with($p['uri'], 'principals/circles')), |
|
151 | + ]; |
|
152 | + } |
|
153 | + $this->shareCache->set((string)$resourceId, $shares); |
|
154 | + return $shares; |
|
155 | + } |
|
156 | + |
|
157 | + public function preloadShares(array $resourceIds): void { |
|
158 | + $resourceIds = array_filter($resourceIds, function (int $resourceId) { |
|
159 | + return empty($this->shareCache->get((string)$resourceId)); |
|
160 | + }); |
|
161 | + if (empty($resourceIds)) { |
|
162 | + return; |
|
163 | + } |
|
164 | + |
|
165 | + $rows = $this->service->getSharesForIds($resourceIds); |
|
166 | + $sharesByResource = array_fill_keys($resourceIds, []); |
|
167 | + foreach ($rows as $row) { |
|
168 | + $resourceId = (int)$row['resourceid']; |
|
169 | + $p = $this->getPrincipalByPath($row['principaluri']); |
|
170 | + $sharesByResource[$resourceId][] = [ |
|
171 | + 'href' => "principal:{$row['principaluri']}", |
|
172 | + 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
173 | + 'status' => 1, |
|
174 | + 'readOnly' => (int)$row['access'] === self::ACCESS_READ, |
|
175 | + '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
176 | + '{http://owncloud.org/ns}group-share' => isset($p['uri']) && str_starts_with($p['uri'], 'principals/groups') |
|
177 | + ]; |
|
178 | + $this->shareCache->set((string)$resourceId, $sharesByResource[$resourceId]); |
|
179 | + } |
|
180 | + |
|
181 | + // Also remember resources with no shares to prevent superfluous (empty) queries later on |
|
182 | + foreach ($resourceIds as $resourceId) { |
|
183 | + $hasShares = false; |
|
184 | + foreach ($rows as $row) { |
|
185 | + if ((int)$row['resourceid'] === $resourceId) { |
|
186 | + $hasShares = true; |
|
187 | + break; |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + if ($hasShares) { |
|
192 | + continue; |
|
193 | + } |
|
194 | + |
|
195 | + $this->shareCache->set((string)$resourceId, []); |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * For shared resources the sharee is set in the ACL of the resource |
|
201 | + * |
|
202 | + * @param int $resourceId |
|
203 | + * @param list<array{privilege: string, principal: string, protected: bool}> $acl |
|
204 | + * @param list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}> $shares |
|
205 | + * @return list<array{principal: string, privilege: string, protected: bool}> |
|
206 | + */ |
|
207 | + public function applyShareAcl(array $shares, array $acl): array { |
|
208 | + foreach ($shares as $share) { |
|
209 | + $acl[] = [ |
|
210 | + 'privilege' => '{DAV:}read', |
|
211 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
212 | + 'protected' => true, |
|
213 | + ]; |
|
214 | + if (!$share['readOnly']) { |
|
215 | + $acl[] = [ |
|
216 | + 'privilege' => '{DAV:}write', |
|
217 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
218 | + 'protected' => true, |
|
219 | + ]; |
|
220 | + } elseif (in_array($this->service->getResourceType(), ['calendar','addressbook'])) { |
|
221 | + // Allow changing the properties of read only calendars, |
|
222 | + // so users can change the visibility. |
|
223 | + $acl[] = [ |
|
224 | + 'privilege' => '{DAV:}write-properties', |
|
225 | + 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
226 | + 'protected' => true, |
|
227 | + ]; |
|
228 | + } |
|
229 | + } |
|
230 | + return $acl; |
|
231 | + } |
|
232 | + |
|
233 | + public function unshare(IShareable $shareable, string $principalUri): bool { |
|
234 | + $this->shareCache->clear(); |
|
235 | + |
|
236 | + $principal = $this->principalBackend->findByUri($principalUri, ''); |
|
237 | + if (empty($principal)) { |
|
238 | + return false; |
|
239 | + } |
|
240 | + |
|
241 | + if ($shareable->getOwner() === $principal) { |
|
242 | + return false; |
|
243 | + } |
|
244 | + |
|
245 | + // Delete any possible direct shares (since the frontend does not separate between them) |
|
246 | + $this->service->deleteShare($shareable->getResourceId(), $principal); |
|
247 | + |
|
248 | + $needsUnshare = $this->hasAccessByGroupOrCirclesMembership( |
|
249 | + $shareable->getResourceId(), |
|
250 | + $principal |
|
251 | + ); |
|
252 | + |
|
253 | + if ($needsUnshare) { |
|
254 | + $this->service->unshare($shareable->getResourceId(), $principal); |
|
255 | + } |
|
256 | + |
|
257 | + return true; |
|
258 | + } |
|
259 | + |
|
260 | + private function hasAccessByGroupOrCirclesMembership(int $resourceId, string $principal) { |
|
261 | + $memberships = array_merge( |
|
262 | + $this->principalBackend->getGroupMembership($principal, true), |
|
263 | + $this->principalBackend->getCircleMembership($principal) |
|
264 | + ); |
|
265 | + |
|
266 | + $shares = array_column( |
|
267 | + $this->service->getShares($resourceId), |
|
268 | + 'principaluri' |
|
269 | + ); |
|
270 | + |
|
271 | + return count(array_intersect($memberships, $shares)) > 0; |
|
272 | + } |
|
273 | + |
|
274 | + public function getSharesByShareePrincipal(string $principal): array { |
|
275 | + return $this->service->getSharesByPrincipals([$principal]); |
|
276 | + } |
|
277 | + |
|
278 | + private function getPrincipalByPath(string $principalUri): ?array { |
|
279 | + // Hacky code below ... shouldn't we check the whole (principal) root collection instead? |
|
280 | + if (str_starts_with($principalUri, RemoteUserPrincipalBackend::PRINCIPAL_PREFIX)) { |
|
281 | + return $this->remoteUserPrincipalBackend->getPrincipalByPath($principalUri); |
|
282 | + } |
|
283 | + |
|
284 | + return $this->principalBackend->getPrincipalByPath($principalUri); |
|
285 | + } |
|
286 | 286 | } |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | */ |
133 | 133 | public function getShares(int $resourceId): array { |
134 | 134 | /** @var list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}>|null $cached */ |
135 | - $cached = $this->shareCache->get((string)$resourceId); |
|
135 | + $cached = $this->shareCache->get((string) $resourceId); |
|
136 | 136 | if (is_array($cached)) { |
137 | 137 | return $cached; |
138 | 138 | } |
@@ -143,20 +143,20 @@ discard block |
||
143 | 143 | $p = $this->getPrincipalByPath($row['principaluri']); |
144 | 144 | $shares[] = [ |
145 | 145 | 'href' => "principal:{$row['principaluri']}", |
146 | - 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
146 | + 'commonName' => isset($p['{DAV:}displayname']) ? (string) $p['{DAV:}displayname'] : '', |
|
147 | 147 | 'status' => 1, |
148 | - 'readOnly' => (int)$row['access'] === Backend::ACCESS_READ, |
|
149 | - '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
148 | + 'readOnly' => (int) $row['access'] === Backend::ACCESS_READ, |
|
149 | + '{http://owncloud.org/ns}principal' => (string) $row['principaluri'], |
|
150 | 150 | '{http://owncloud.org/ns}group-share' => isset($p['uri']) && (str_starts_with($p['uri'], 'principals/groups') || str_starts_with($p['uri'], 'principals/circles')), |
151 | 151 | ]; |
152 | 152 | } |
153 | - $this->shareCache->set((string)$resourceId, $shares); |
|
153 | + $this->shareCache->set((string) $resourceId, $shares); |
|
154 | 154 | return $shares; |
155 | 155 | } |
156 | 156 | |
157 | 157 | public function preloadShares(array $resourceIds): void { |
158 | - $resourceIds = array_filter($resourceIds, function (int $resourceId) { |
|
159 | - return empty($this->shareCache->get((string)$resourceId)); |
|
158 | + $resourceIds = array_filter($resourceIds, function(int $resourceId) { |
|
159 | + return empty($this->shareCache->get((string) $resourceId)); |
|
160 | 160 | }); |
161 | 161 | if (empty($resourceIds)) { |
162 | 162 | return; |
@@ -165,24 +165,24 @@ discard block |
||
165 | 165 | $rows = $this->service->getSharesForIds($resourceIds); |
166 | 166 | $sharesByResource = array_fill_keys($resourceIds, []); |
167 | 167 | foreach ($rows as $row) { |
168 | - $resourceId = (int)$row['resourceid']; |
|
168 | + $resourceId = (int) $row['resourceid']; |
|
169 | 169 | $p = $this->getPrincipalByPath($row['principaluri']); |
170 | 170 | $sharesByResource[$resourceId][] = [ |
171 | 171 | 'href' => "principal:{$row['principaluri']}", |
172 | - 'commonName' => isset($p['{DAV:}displayname']) ? (string)$p['{DAV:}displayname'] : '', |
|
172 | + 'commonName' => isset($p['{DAV:}displayname']) ? (string) $p['{DAV:}displayname'] : '', |
|
173 | 173 | 'status' => 1, |
174 | - 'readOnly' => (int)$row['access'] === self::ACCESS_READ, |
|
175 | - '{http://owncloud.org/ns}principal' => (string)$row['principaluri'], |
|
174 | + 'readOnly' => (int) $row['access'] === self::ACCESS_READ, |
|
175 | + '{http://owncloud.org/ns}principal' => (string) $row['principaluri'], |
|
176 | 176 | '{http://owncloud.org/ns}group-share' => isset($p['uri']) && str_starts_with($p['uri'], 'principals/groups') |
177 | 177 | ]; |
178 | - $this->shareCache->set((string)$resourceId, $sharesByResource[$resourceId]); |
|
178 | + $this->shareCache->set((string) $resourceId, $sharesByResource[$resourceId]); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | // Also remember resources with no shares to prevent superfluous (empty) queries later on |
182 | 182 | foreach ($resourceIds as $resourceId) { |
183 | 183 | $hasShares = false; |
184 | 184 | foreach ($rows as $row) { |
185 | - if ((int)$row['resourceid'] === $resourceId) { |
|
185 | + if ((int) $row['resourceid'] === $resourceId) { |
|
186 | 186 | $hasShares = true; |
187 | 187 | break; |
188 | 188 | } |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | continue; |
193 | 193 | } |
194 | 194 | |
195 | - $this->shareCache->set((string)$resourceId, []); |
|
195 | + $this->shareCache->set((string) $resourceId, []); |
|
196 | 196 | } |
197 | 197 | } |
198 | 198 | |
@@ -208,21 +208,21 @@ discard block |
||
208 | 208 | foreach ($shares as $share) { |
209 | 209 | $acl[] = [ |
210 | 210 | 'privilege' => '{DAV:}read', |
211 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
211 | + 'principal' => $share['{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}principal'], |
|
212 | 212 | 'protected' => true, |
213 | 213 | ]; |
214 | 214 | if (!$share['readOnly']) { |
215 | 215 | $acl[] = [ |
216 | 216 | 'privilege' => '{DAV:}write', |
217 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
217 | + 'principal' => $share['{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}principal'], |
|
218 | 218 | 'protected' => true, |
219 | 219 | ]; |
220 | - } elseif (in_array($this->service->getResourceType(), ['calendar','addressbook'])) { |
|
220 | + } elseif (in_array($this->service->getResourceType(), ['calendar', 'addressbook'])) { |
|
221 | 221 | // Allow changing the properties of read only calendars, |
222 | 222 | // so users can change the visibility. |
223 | 223 | $acl[] = [ |
224 | 224 | 'privilege' => '{DAV:}write-properties', |
225 | - 'principal' => $share['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}principal'], |
|
225 | + 'principal' => $share['{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}principal'], |
|
226 | 226 | 'protected' => true, |
227 | 227 | ]; |
228 | 228 | } |