@@ -78,1846 +78,1846 @@ |
||
78 | 78 | */ |
79 | 79 | class Manager implements IManager { |
80 | 80 | |
81 | - /** @var IProviderFactory */ |
|
82 | - private $factory; |
|
83 | - /** @var ILogger */ |
|
84 | - private $logger; |
|
85 | - /** @var IConfig */ |
|
86 | - private $config; |
|
87 | - /** @var ISecureRandom */ |
|
88 | - private $secureRandom; |
|
89 | - /** @var IHasher */ |
|
90 | - private $hasher; |
|
91 | - /** @var IMountManager */ |
|
92 | - private $mountManager; |
|
93 | - /** @var IGroupManager */ |
|
94 | - private $groupManager; |
|
95 | - /** @var IL10N */ |
|
96 | - private $l; |
|
97 | - /** @var IFactory */ |
|
98 | - private $l10nFactory; |
|
99 | - /** @var IUserManager */ |
|
100 | - private $userManager; |
|
101 | - /** @var IRootFolder */ |
|
102 | - private $rootFolder; |
|
103 | - /** @var CappedMemoryCache */ |
|
104 | - private $sharingDisabledForUsersCache; |
|
105 | - /** @var EventDispatcherInterface */ |
|
106 | - private $legacyDispatcher; |
|
107 | - /** @var LegacyHooks */ |
|
108 | - private $legacyHooks; |
|
109 | - /** @var IMailer */ |
|
110 | - private $mailer; |
|
111 | - /** @var IURLGenerator */ |
|
112 | - private $urlGenerator; |
|
113 | - /** @var \OC_Defaults */ |
|
114 | - private $defaults; |
|
115 | - /** @var IEventDispatcher */ |
|
116 | - private $dispatcher; |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Manager constructor. |
|
121 | - * |
|
122 | - * @param ILogger $logger |
|
123 | - * @param IConfig $config |
|
124 | - * @param ISecureRandom $secureRandom |
|
125 | - * @param IHasher $hasher |
|
126 | - * @param IMountManager $mountManager |
|
127 | - * @param IGroupManager $groupManager |
|
128 | - * @param IL10N $l |
|
129 | - * @param IFactory $l10nFactory |
|
130 | - * @param IProviderFactory $factory |
|
131 | - * @param IUserManager $userManager |
|
132 | - * @param IRootFolder $rootFolder |
|
133 | - * @param EventDispatcherInterface $eventDispatcher |
|
134 | - * @param IMailer $mailer |
|
135 | - * @param IURLGenerator $urlGenerator |
|
136 | - * @param \OC_Defaults $defaults |
|
137 | - */ |
|
138 | - public function __construct( |
|
139 | - ILogger $logger, |
|
140 | - IConfig $config, |
|
141 | - ISecureRandom $secureRandom, |
|
142 | - IHasher $hasher, |
|
143 | - IMountManager $mountManager, |
|
144 | - IGroupManager $groupManager, |
|
145 | - IL10N $l, |
|
146 | - IFactory $l10nFactory, |
|
147 | - IProviderFactory $factory, |
|
148 | - IUserManager $userManager, |
|
149 | - IRootFolder $rootFolder, |
|
150 | - EventDispatcherInterface $legacyDispatcher, |
|
151 | - IMailer $mailer, |
|
152 | - IURLGenerator $urlGenerator, |
|
153 | - \OC_Defaults $defaults, |
|
154 | - IEventDispatcher $dispatcher |
|
155 | - ) { |
|
156 | - $this->logger = $logger; |
|
157 | - $this->config = $config; |
|
158 | - $this->secureRandom = $secureRandom; |
|
159 | - $this->hasher = $hasher; |
|
160 | - $this->mountManager = $mountManager; |
|
161 | - $this->groupManager = $groupManager; |
|
162 | - $this->l = $l; |
|
163 | - $this->l10nFactory = $l10nFactory; |
|
164 | - $this->factory = $factory; |
|
165 | - $this->userManager = $userManager; |
|
166 | - $this->rootFolder = $rootFolder; |
|
167 | - $this->legacyDispatcher = $legacyDispatcher; |
|
168 | - $this->sharingDisabledForUsersCache = new CappedMemoryCache(); |
|
169 | - $this->legacyHooks = new LegacyHooks($this->legacyDispatcher); |
|
170 | - $this->mailer = $mailer; |
|
171 | - $this->urlGenerator = $urlGenerator; |
|
172 | - $this->defaults = $defaults; |
|
173 | - $this->dispatcher = $dispatcher; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Convert from a full share id to a tuple (providerId, shareId) |
|
178 | - * |
|
179 | - * @param string $id |
|
180 | - * @return string[] |
|
181 | - */ |
|
182 | - private function splitFullId($id) { |
|
183 | - return explode(':', $id, 2); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Verify if a password meets all requirements |
|
188 | - * |
|
189 | - * @param string $password |
|
190 | - * @throws \Exception |
|
191 | - */ |
|
192 | - protected function verifyPassword($password) { |
|
193 | - if ($password === null) { |
|
194 | - // No password is set, check if this is allowed. |
|
195 | - if ($this->shareApiLinkEnforcePassword()) { |
|
196 | - throw new \InvalidArgumentException('Passwords are enforced for link shares'); |
|
197 | - } |
|
198 | - |
|
199 | - return; |
|
200 | - } |
|
201 | - |
|
202 | - // Let others verify the password |
|
203 | - try { |
|
204 | - $this->legacyDispatcher->dispatch(new ValidatePasswordPolicyEvent($password)); |
|
205 | - } catch (HintException $e) { |
|
206 | - throw new \Exception($e->getHint()); |
|
207 | - } |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Check for generic requirements before creating a share |
|
212 | - * |
|
213 | - * @param IShare $share |
|
214 | - * @throws \InvalidArgumentException |
|
215 | - * @throws GenericShareException |
|
216 | - * |
|
217 | - * @suppress PhanUndeclaredClassMethod |
|
218 | - */ |
|
219 | - protected function generalCreateChecks(IShare $share) { |
|
220 | - if ($share->getShareType() === IShare::TYPE_USER) { |
|
221 | - // We expect a valid user as sharedWith for user shares |
|
222 | - if (!$this->userManager->userExists($share->getSharedWith())) { |
|
223 | - throw new \InvalidArgumentException('SharedWith is not a valid user'); |
|
224 | - } |
|
225 | - } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
226 | - // We expect a valid group as sharedWith for group shares |
|
227 | - if (!$this->groupManager->groupExists($share->getSharedWith())) { |
|
228 | - throw new \InvalidArgumentException('SharedWith is not a valid group'); |
|
229 | - } |
|
230 | - } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
231 | - if ($share->getSharedWith() !== null) { |
|
232 | - throw new \InvalidArgumentException('SharedWith should be empty'); |
|
233 | - } |
|
234 | - } elseif ($share->getShareType() === IShare::TYPE_REMOTE) { |
|
235 | - if ($share->getSharedWith() === null) { |
|
236 | - throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
237 | - } |
|
238 | - } elseif ($share->getShareType() === IShare::TYPE_REMOTE_GROUP) { |
|
239 | - if ($share->getSharedWith() === null) { |
|
240 | - throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
241 | - } |
|
242 | - } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
243 | - if ($share->getSharedWith() === null) { |
|
244 | - throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
245 | - } |
|
246 | - } elseif ($share->getShareType() === IShare::TYPE_CIRCLE) { |
|
247 | - $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith()); |
|
248 | - if ($circle === null) { |
|
249 | - throw new \InvalidArgumentException('SharedWith is not a valid circle'); |
|
250 | - } |
|
251 | - } elseif ($share->getShareType() === IShare::TYPE_ROOM) { |
|
252 | - } elseif ($share->getShareType() === IShare::TYPE_DECK) { |
|
253 | - } else { |
|
254 | - // We can't handle other types yet |
|
255 | - throw new \InvalidArgumentException('unknown share type'); |
|
256 | - } |
|
257 | - |
|
258 | - // Verify the initiator of the share is set |
|
259 | - if ($share->getSharedBy() === null) { |
|
260 | - throw new \InvalidArgumentException('SharedBy should be set'); |
|
261 | - } |
|
262 | - |
|
263 | - // Cannot share with yourself |
|
264 | - if ($share->getShareType() === IShare::TYPE_USER && |
|
265 | - $share->getSharedWith() === $share->getSharedBy()) { |
|
266 | - throw new \InvalidArgumentException('Can’t share with yourself'); |
|
267 | - } |
|
268 | - |
|
269 | - // The path should be set |
|
270 | - if ($share->getNode() === null) { |
|
271 | - throw new \InvalidArgumentException('Path should be set'); |
|
272 | - } |
|
273 | - |
|
274 | - // And it should be a file or a folder |
|
275 | - if (!($share->getNode() instanceof \OCP\Files\File) && |
|
276 | - !($share->getNode() instanceof \OCP\Files\Folder)) { |
|
277 | - throw new \InvalidArgumentException('Path should be either a file or a folder'); |
|
278 | - } |
|
279 | - |
|
280 | - // And you can't share your rootfolder |
|
281 | - if ($this->userManager->userExists($share->getSharedBy())) { |
|
282 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
283 | - } else { |
|
284 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
285 | - } |
|
286 | - if ($userFolder->getId() === $share->getNode()->getId()) { |
|
287 | - throw new \InvalidArgumentException('You can’t share your root folder'); |
|
288 | - } |
|
289 | - |
|
290 | - // Check if we actually have share permissions |
|
291 | - if (!$share->getNode()->isShareable()) { |
|
292 | - $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getName()]); |
|
293 | - throw new GenericShareException($message_t, $message_t, 404); |
|
294 | - } |
|
295 | - |
|
296 | - // Permissions should be set |
|
297 | - if ($share->getPermissions() === null) { |
|
298 | - throw new \InvalidArgumentException('A share requires permissions'); |
|
299 | - } |
|
300 | - |
|
301 | - $isFederatedShare = $share->getNode()->getStorage()->instanceOfStorage('\OCA\Files_Sharing\External\Storage'); |
|
302 | - $permissions = 0; |
|
303 | - |
|
304 | - if (!$isFederatedShare && $share->getNode()->getOwner() && $share->getNode()->getOwner()->getUID() !== $share->getSharedBy()) { |
|
305 | - $userMounts = array_filter($userFolder->getById($share->getNode()->getId()), function ($mount) { |
|
306 | - // We need to filter since there might be other mountpoints that contain the file |
|
307 | - // e.g. if the user has access to the same external storage that the file is originating from |
|
308 | - return $mount->getStorage()->instanceOfStorage(ISharedStorage::class); |
|
309 | - }); |
|
310 | - $userMount = array_shift($userMounts); |
|
311 | - if ($userMount === null) { |
|
312 | - throw new GenericShareException('Could not get proper share mount for ' . $share->getNode()->getId() . '. Failing since else the next calls are called with null'); |
|
313 | - } |
|
314 | - $mount = $userMount->getMountPoint(); |
|
315 | - // When it's a reshare use the parent share permissions as maximum |
|
316 | - $userMountPointId = $mount->getStorageRootId(); |
|
317 | - $userMountPoints = $userFolder->getById($userMountPointId); |
|
318 | - $userMountPoint = array_shift($userMountPoints); |
|
319 | - |
|
320 | - if ($userMountPoint === null) { |
|
321 | - throw new GenericShareException('Could not get proper user mount for ' . $userMountPointId . '. Failing since else the next calls are called with null'); |
|
322 | - } |
|
323 | - |
|
324 | - /* Check if this is an incoming share */ |
|
325 | - $incomingShares = $this->getSharedWith($share->getSharedBy(), IShare::TYPE_USER, $userMountPoint, -1, 0); |
|
326 | - $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), IShare::TYPE_GROUP, $userMountPoint, -1, 0)); |
|
327 | - $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), IShare::TYPE_CIRCLE, $userMountPoint, -1, 0)); |
|
328 | - $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), IShare::TYPE_ROOM, $userMountPoint, -1, 0)); |
|
329 | - |
|
330 | - /** @var IShare[] $incomingShares */ |
|
331 | - if (!empty($incomingShares)) { |
|
332 | - foreach ($incomingShares as $incomingShare) { |
|
333 | - $permissions |= $incomingShare->getPermissions(); |
|
334 | - } |
|
335 | - } |
|
336 | - } else { |
|
337 | - /* |
|
81 | + /** @var IProviderFactory */ |
|
82 | + private $factory; |
|
83 | + /** @var ILogger */ |
|
84 | + private $logger; |
|
85 | + /** @var IConfig */ |
|
86 | + private $config; |
|
87 | + /** @var ISecureRandom */ |
|
88 | + private $secureRandom; |
|
89 | + /** @var IHasher */ |
|
90 | + private $hasher; |
|
91 | + /** @var IMountManager */ |
|
92 | + private $mountManager; |
|
93 | + /** @var IGroupManager */ |
|
94 | + private $groupManager; |
|
95 | + /** @var IL10N */ |
|
96 | + private $l; |
|
97 | + /** @var IFactory */ |
|
98 | + private $l10nFactory; |
|
99 | + /** @var IUserManager */ |
|
100 | + private $userManager; |
|
101 | + /** @var IRootFolder */ |
|
102 | + private $rootFolder; |
|
103 | + /** @var CappedMemoryCache */ |
|
104 | + private $sharingDisabledForUsersCache; |
|
105 | + /** @var EventDispatcherInterface */ |
|
106 | + private $legacyDispatcher; |
|
107 | + /** @var LegacyHooks */ |
|
108 | + private $legacyHooks; |
|
109 | + /** @var IMailer */ |
|
110 | + private $mailer; |
|
111 | + /** @var IURLGenerator */ |
|
112 | + private $urlGenerator; |
|
113 | + /** @var \OC_Defaults */ |
|
114 | + private $defaults; |
|
115 | + /** @var IEventDispatcher */ |
|
116 | + private $dispatcher; |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Manager constructor. |
|
121 | + * |
|
122 | + * @param ILogger $logger |
|
123 | + * @param IConfig $config |
|
124 | + * @param ISecureRandom $secureRandom |
|
125 | + * @param IHasher $hasher |
|
126 | + * @param IMountManager $mountManager |
|
127 | + * @param IGroupManager $groupManager |
|
128 | + * @param IL10N $l |
|
129 | + * @param IFactory $l10nFactory |
|
130 | + * @param IProviderFactory $factory |
|
131 | + * @param IUserManager $userManager |
|
132 | + * @param IRootFolder $rootFolder |
|
133 | + * @param EventDispatcherInterface $eventDispatcher |
|
134 | + * @param IMailer $mailer |
|
135 | + * @param IURLGenerator $urlGenerator |
|
136 | + * @param \OC_Defaults $defaults |
|
137 | + */ |
|
138 | + public function __construct( |
|
139 | + ILogger $logger, |
|
140 | + IConfig $config, |
|
141 | + ISecureRandom $secureRandom, |
|
142 | + IHasher $hasher, |
|
143 | + IMountManager $mountManager, |
|
144 | + IGroupManager $groupManager, |
|
145 | + IL10N $l, |
|
146 | + IFactory $l10nFactory, |
|
147 | + IProviderFactory $factory, |
|
148 | + IUserManager $userManager, |
|
149 | + IRootFolder $rootFolder, |
|
150 | + EventDispatcherInterface $legacyDispatcher, |
|
151 | + IMailer $mailer, |
|
152 | + IURLGenerator $urlGenerator, |
|
153 | + \OC_Defaults $defaults, |
|
154 | + IEventDispatcher $dispatcher |
|
155 | + ) { |
|
156 | + $this->logger = $logger; |
|
157 | + $this->config = $config; |
|
158 | + $this->secureRandom = $secureRandom; |
|
159 | + $this->hasher = $hasher; |
|
160 | + $this->mountManager = $mountManager; |
|
161 | + $this->groupManager = $groupManager; |
|
162 | + $this->l = $l; |
|
163 | + $this->l10nFactory = $l10nFactory; |
|
164 | + $this->factory = $factory; |
|
165 | + $this->userManager = $userManager; |
|
166 | + $this->rootFolder = $rootFolder; |
|
167 | + $this->legacyDispatcher = $legacyDispatcher; |
|
168 | + $this->sharingDisabledForUsersCache = new CappedMemoryCache(); |
|
169 | + $this->legacyHooks = new LegacyHooks($this->legacyDispatcher); |
|
170 | + $this->mailer = $mailer; |
|
171 | + $this->urlGenerator = $urlGenerator; |
|
172 | + $this->defaults = $defaults; |
|
173 | + $this->dispatcher = $dispatcher; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Convert from a full share id to a tuple (providerId, shareId) |
|
178 | + * |
|
179 | + * @param string $id |
|
180 | + * @return string[] |
|
181 | + */ |
|
182 | + private function splitFullId($id) { |
|
183 | + return explode(':', $id, 2); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Verify if a password meets all requirements |
|
188 | + * |
|
189 | + * @param string $password |
|
190 | + * @throws \Exception |
|
191 | + */ |
|
192 | + protected function verifyPassword($password) { |
|
193 | + if ($password === null) { |
|
194 | + // No password is set, check if this is allowed. |
|
195 | + if ($this->shareApiLinkEnforcePassword()) { |
|
196 | + throw new \InvalidArgumentException('Passwords are enforced for link shares'); |
|
197 | + } |
|
198 | + |
|
199 | + return; |
|
200 | + } |
|
201 | + |
|
202 | + // Let others verify the password |
|
203 | + try { |
|
204 | + $this->legacyDispatcher->dispatch(new ValidatePasswordPolicyEvent($password)); |
|
205 | + } catch (HintException $e) { |
|
206 | + throw new \Exception($e->getHint()); |
|
207 | + } |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Check for generic requirements before creating a share |
|
212 | + * |
|
213 | + * @param IShare $share |
|
214 | + * @throws \InvalidArgumentException |
|
215 | + * @throws GenericShareException |
|
216 | + * |
|
217 | + * @suppress PhanUndeclaredClassMethod |
|
218 | + */ |
|
219 | + protected function generalCreateChecks(IShare $share) { |
|
220 | + if ($share->getShareType() === IShare::TYPE_USER) { |
|
221 | + // We expect a valid user as sharedWith for user shares |
|
222 | + if (!$this->userManager->userExists($share->getSharedWith())) { |
|
223 | + throw new \InvalidArgumentException('SharedWith is not a valid user'); |
|
224 | + } |
|
225 | + } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
226 | + // We expect a valid group as sharedWith for group shares |
|
227 | + if (!$this->groupManager->groupExists($share->getSharedWith())) { |
|
228 | + throw new \InvalidArgumentException('SharedWith is not a valid group'); |
|
229 | + } |
|
230 | + } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
231 | + if ($share->getSharedWith() !== null) { |
|
232 | + throw new \InvalidArgumentException('SharedWith should be empty'); |
|
233 | + } |
|
234 | + } elseif ($share->getShareType() === IShare::TYPE_REMOTE) { |
|
235 | + if ($share->getSharedWith() === null) { |
|
236 | + throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
237 | + } |
|
238 | + } elseif ($share->getShareType() === IShare::TYPE_REMOTE_GROUP) { |
|
239 | + if ($share->getSharedWith() === null) { |
|
240 | + throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
241 | + } |
|
242 | + } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
243 | + if ($share->getSharedWith() === null) { |
|
244 | + throw new \InvalidArgumentException('SharedWith should not be empty'); |
|
245 | + } |
|
246 | + } elseif ($share->getShareType() === IShare::TYPE_CIRCLE) { |
|
247 | + $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($share->getSharedWith()); |
|
248 | + if ($circle === null) { |
|
249 | + throw new \InvalidArgumentException('SharedWith is not a valid circle'); |
|
250 | + } |
|
251 | + } elseif ($share->getShareType() === IShare::TYPE_ROOM) { |
|
252 | + } elseif ($share->getShareType() === IShare::TYPE_DECK) { |
|
253 | + } else { |
|
254 | + // We can't handle other types yet |
|
255 | + throw new \InvalidArgumentException('unknown share type'); |
|
256 | + } |
|
257 | + |
|
258 | + // Verify the initiator of the share is set |
|
259 | + if ($share->getSharedBy() === null) { |
|
260 | + throw new \InvalidArgumentException('SharedBy should be set'); |
|
261 | + } |
|
262 | + |
|
263 | + // Cannot share with yourself |
|
264 | + if ($share->getShareType() === IShare::TYPE_USER && |
|
265 | + $share->getSharedWith() === $share->getSharedBy()) { |
|
266 | + throw new \InvalidArgumentException('Can’t share with yourself'); |
|
267 | + } |
|
268 | + |
|
269 | + // The path should be set |
|
270 | + if ($share->getNode() === null) { |
|
271 | + throw new \InvalidArgumentException('Path should be set'); |
|
272 | + } |
|
273 | + |
|
274 | + // And it should be a file or a folder |
|
275 | + if (!($share->getNode() instanceof \OCP\Files\File) && |
|
276 | + !($share->getNode() instanceof \OCP\Files\Folder)) { |
|
277 | + throw new \InvalidArgumentException('Path should be either a file or a folder'); |
|
278 | + } |
|
279 | + |
|
280 | + // And you can't share your rootfolder |
|
281 | + if ($this->userManager->userExists($share->getSharedBy())) { |
|
282 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
283 | + } else { |
|
284 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
285 | + } |
|
286 | + if ($userFolder->getId() === $share->getNode()->getId()) { |
|
287 | + throw new \InvalidArgumentException('You can’t share your root folder'); |
|
288 | + } |
|
289 | + |
|
290 | + // Check if we actually have share permissions |
|
291 | + if (!$share->getNode()->isShareable()) { |
|
292 | + $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getName()]); |
|
293 | + throw new GenericShareException($message_t, $message_t, 404); |
|
294 | + } |
|
295 | + |
|
296 | + // Permissions should be set |
|
297 | + if ($share->getPermissions() === null) { |
|
298 | + throw new \InvalidArgumentException('A share requires permissions'); |
|
299 | + } |
|
300 | + |
|
301 | + $isFederatedShare = $share->getNode()->getStorage()->instanceOfStorage('\OCA\Files_Sharing\External\Storage'); |
|
302 | + $permissions = 0; |
|
303 | + |
|
304 | + if (!$isFederatedShare && $share->getNode()->getOwner() && $share->getNode()->getOwner()->getUID() !== $share->getSharedBy()) { |
|
305 | + $userMounts = array_filter($userFolder->getById($share->getNode()->getId()), function ($mount) { |
|
306 | + // We need to filter since there might be other mountpoints that contain the file |
|
307 | + // e.g. if the user has access to the same external storage that the file is originating from |
|
308 | + return $mount->getStorage()->instanceOfStorage(ISharedStorage::class); |
|
309 | + }); |
|
310 | + $userMount = array_shift($userMounts); |
|
311 | + if ($userMount === null) { |
|
312 | + throw new GenericShareException('Could not get proper share mount for ' . $share->getNode()->getId() . '. Failing since else the next calls are called with null'); |
|
313 | + } |
|
314 | + $mount = $userMount->getMountPoint(); |
|
315 | + // When it's a reshare use the parent share permissions as maximum |
|
316 | + $userMountPointId = $mount->getStorageRootId(); |
|
317 | + $userMountPoints = $userFolder->getById($userMountPointId); |
|
318 | + $userMountPoint = array_shift($userMountPoints); |
|
319 | + |
|
320 | + if ($userMountPoint === null) { |
|
321 | + throw new GenericShareException('Could not get proper user mount for ' . $userMountPointId . '. Failing since else the next calls are called with null'); |
|
322 | + } |
|
323 | + |
|
324 | + /* Check if this is an incoming share */ |
|
325 | + $incomingShares = $this->getSharedWith($share->getSharedBy(), IShare::TYPE_USER, $userMountPoint, -1, 0); |
|
326 | + $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), IShare::TYPE_GROUP, $userMountPoint, -1, 0)); |
|
327 | + $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), IShare::TYPE_CIRCLE, $userMountPoint, -1, 0)); |
|
328 | + $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), IShare::TYPE_ROOM, $userMountPoint, -1, 0)); |
|
329 | + |
|
330 | + /** @var IShare[] $incomingShares */ |
|
331 | + if (!empty($incomingShares)) { |
|
332 | + foreach ($incomingShares as $incomingShare) { |
|
333 | + $permissions |= $incomingShare->getPermissions(); |
|
334 | + } |
|
335 | + } |
|
336 | + } else { |
|
337 | + /* |
|
338 | 338 | * Quick fix for #23536 |
339 | 339 | * Non moveable mount points do not have update and delete permissions |
340 | 340 | * while we 'most likely' do have that on the storage. |
341 | 341 | */ |
342 | - $permissions = $share->getNode()->getPermissions(); |
|
343 | - if (!($share->getNode()->getMountPoint() instanceof MoveableMount)) { |
|
344 | - $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
345 | - } |
|
346 | - } |
|
347 | - |
|
348 | - // Check that we do not share with more permissions than we have |
|
349 | - if ($share->getPermissions() & ~$permissions) { |
|
350 | - $path = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
351 | - $message_t = $this->l->t('Can’t increase permissions of %s', [$path]); |
|
352 | - throw new GenericShareException($message_t, $message_t, 404); |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - // Check that read permissions are always set |
|
357 | - // Link shares are allowed to have no read permissions to allow upload to hidden folders |
|
358 | - $noReadPermissionRequired = $share->getShareType() === IShare::TYPE_LINK |
|
359 | - || $share->getShareType() === IShare::TYPE_EMAIL; |
|
360 | - if (!$noReadPermissionRequired && |
|
361 | - ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { |
|
362 | - throw new \InvalidArgumentException('Shares need at least read permissions'); |
|
363 | - } |
|
364 | - |
|
365 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
366 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { |
|
367 | - $message_t = $this->l->t('Files can’t be shared with delete permissions'); |
|
368 | - throw new GenericShareException($message_t); |
|
369 | - } |
|
370 | - if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { |
|
371 | - $message_t = $this->l->t('Files can’t be shared with create permissions'); |
|
372 | - throw new GenericShareException($message_t); |
|
373 | - } |
|
374 | - } |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Validate if the expiration date fits the system settings |
|
379 | - * |
|
380 | - * @param IShare $share The share to validate the expiration date of |
|
381 | - * @return IShare The modified share object |
|
382 | - * @throws GenericShareException |
|
383 | - * @throws \InvalidArgumentException |
|
384 | - * @throws \Exception |
|
385 | - */ |
|
386 | - protected function validateExpirationDateInternal(IShare $share) { |
|
387 | - $expirationDate = $share->getExpirationDate(); |
|
388 | - |
|
389 | - if ($expirationDate !== null) { |
|
390 | - //Make sure the expiration date is a date |
|
391 | - $expirationDate->setTime(0, 0, 0); |
|
392 | - |
|
393 | - $date = new \DateTime(); |
|
394 | - $date->setTime(0, 0, 0); |
|
395 | - if ($date >= $expirationDate) { |
|
396 | - $message = $this->l->t('Expiration date is in the past'); |
|
397 | - throw new GenericShareException($message, $message, 404); |
|
398 | - } |
|
399 | - } |
|
400 | - |
|
401 | - // If expiredate is empty set a default one if there is a default |
|
402 | - $fullId = null; |
|
403 | - try { |
|
404 | - $fullId = $share->getFullId(); |
|
405 | - } catch (\UnexpectedValueException $e) { |
|
406 | - // This is a new share |
|
407 | - } |
|
408 | - |
|
409 | - if ($fullId === null && $expirationDate === null && $this->shareApiInternalDefaultExpireDate()) { |
|
410 | - $expirationDate = new \DateTime(); |
|
411 | - $expirationDate->setTime(0,0,0); |
|
412 | - |
|
413 | - $days = (int)$this->config->getAppValue('core', 'internal_defaultExpDays', (string)$this->shareApiInternalDefaultExpireDays()); |
|
414 | - if ($days > $this->shareApiInternalDefaultExpireDays()) { |
|
415 | - $days = $this->shareApiInternalDefaultExpireDays(); |
|
416 | - } |
|
417 | - $expirationDate->add(new \DateInterval('P'.$days.'D')); |
|
418 | - } |
|
419 | - |
|
420 | - // If we enforce the expiration date check that is does not exceed |
|
421 | - if ($this->shareApiInternalDefaultExpireDateEnforced()) { |
|
422 | - if ($expirationDate === null) { |
|
423 | - throw new \InvalidArgumentException('Expiration date is enforced'); |
|
424 | - } |
|
425 | - |
|
426 | - $date = new \DateTime(); |
|
427 | - $date->setTime(0, 0, 0); |
|
428 | - $date->add(new \DateInterval('P' . $this->shareApiInternalDefaultExpireDays() . 'D')); |
|
429 | - if ($date < $expirationDate) { |
|
430 | - $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiInternalDefaultExpireDays()]); |
|
431 | - throw new GenericShareException($message, $message, 404); |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - $accepted = true; |
|
436 | - $message = ''; |
|
437 | - \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
438 | - 'expirationDate' => &$expirationDate, |
|
439 | - 'accepted' => &$accepted, |
|
440 | - 'message' => &$message, |
|
441 | - 'passwordSet' => $share->getPassword() !== null, |
|
442 | - ]); |
|
443 | - |
|
444 | - if (!$accepted) { |
|
445 | - throw new \Exception($message); |
|
446 | - } |
|
447 | - |
|
448 | - $share->setExpirationDate($expirationDate); |
|
449 | - |
|
450 | - return $share; |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Validate if the expiration date fits the system settings |
|
455 | - * |
|
456 | - * @param IShare $share The share to validate the expiration date of |
|
457 | - * @return IShare The modified share object |
|
458 | - * @throws GenericShareException |
|
459 | - * @throws \InvalidArgumentException |
|
460 | - * @throws \Exception |
|
461 | - */ |
|
462 | - protected function validateExpirationDateLink(IShare $share) { |
|
463 | - $expirationDate = $share->getExpirationDate(); |
|
464 | - |
|
465 | - if ($expirationDate !== null) { |
|
466 | - //Make sure the expiration date is a date |
|
467 | - $expirationDate->setTime(0, 0, 0); |
|
468 | - |
|
469 | - $date = new \DateTime(); |
|
470 | - $date->setTime(0, 0, 0); |
|
471 | - if ($date >= $expirationDate) { |
|
472 | - $message = $this->l->t('Expiration date is in the past'); |
|
473 | - throw new GenericShareException($message, $message, 404); |
|
474 | - } |
|
475 | - } |
|
476 | - |
|
477 | - // If expiredate is empty set a default one if there is a default |
|
478 | - $fullId = null; |
|
479 | - try { |
|
480 | - $fullId = $share->getFullId(); |
|
481 | - } catch (\UnexpectedValueException $e) { |
|
482 | - // This is a new share |
|
483 | - } |
|
484 | - |
|
485 | - if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
|
486 | - $expirationDate = new \DateTime(); |
|
487 | - $expirationDate->setTime(0,0,0); |
|
488 | - |
|
489 | - $days = (int)$this->config->getAppValue('core', 'link_defaultExpDays', $this->shareApiLinkDefaultExpireDays()); |
|
490 | - if ($days > $this->shareApiLinkDefaultExpireDays()) { |
|
491 | - $days = $this->shareApiLinkDefaultExpireDays(); |
|
492 | - } |
|
493 | - $expirationDate->add(new \DateInterval('P'.$days.'D')); |
|
494 | - } |
|
495 | - |
|
496 | - // If we enforce the expiration date check that is does not exceed |
|
497 | - if ($this->shareApiLinkDefaultExpireDateEnforced()) { |
|
498 | - if ($expirationDate === null) { |
|
499 | - throw new \InvalidArgumentException('Expiration date is enforced'); |
|
500 | - } |
|
501 | - |
|
502 | - $date = new \DateTime(); |
|
503 | - $date->setTime(0, 0, 0); |
|
504 | - $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
505 | - if ($date < $expirationDate) { |
|
506 | - $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
|
507 | - throw new GenericShareException($message, $message, 404); |
|
508 | - } |
|
509 | - } |
|
510 | - |
|
511 | - $accepted = true; |
|
512 | - $message = ''; |
|
513 | - \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
514 | - 'expirationDate' => &$expirationDate, |
|
515 | - 'accepted' => &$accepted, |
|
516 | - 'message' => &$message, |
|
517 | - 'passwordSet' => $share->getPassword() !== null, |
|
518 | - ]); |
|
519 | - |
|
520 | - if (!$accepted) { |
|
521 | - throw new \Exception($message); |
|
522 | - } |
|
523 | - |
|
524 | - $share->setExpirationDate($expirationDate); |
|
525 | - |
|
526 | - return $share; |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Check for pre share requirements for user shares |
|
531 | - * |
|
532 | - * @param IShare $share |
|
533 | - * @throws \Exception |
|
534 | - */ |
|
535 | - protected function userCreateChecks(IShare $share) { |
|
536 | - // Check if we can share with group members only |
|
537 | - if ($this->shareWithGroupMembersOnly()) { |
|
538 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
539 | - $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
540 | - // Verify we can share with this user |
|
541 | - $groups = array_intersect( |
|
542 | - $this->groupManager->getUserGroupIds($sharedBy), |
|
543 | - $this->groupManager->getUserGroupIds($sharedWith) |
|
544 | - ); |
|
545 | - if (empty($groups)) { |
|
546 | - $message_t = $this->l->t('Sharing is only allowed with group members'); |
|
547 | - throw new \Exception($message_t); |
|
548 | - } |
|
549 | - } |
|
550 | - |
|
551 | - /* |
|
342 | + $permissions = $share->getNode()->getPermissions(); |
|
343 | + if (!($share->getNode()->getMountPoint() instanceof MoveableMount)) { |
|
344 | + $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
|
345 | + } |
|
346 | + } |
|
347 | + |
|
348 | + // Check that we do not share with more permissions than we have |
|
349 | + if ($share->getPermissions() & ~$permissions) { |
|
350 | + $path = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
351 | + $message_t = $this->l->t('Can’t increase permissions of %s', [$path]); |
|
352 | + throw new GenericShareException($message_t, $message_t, 404); |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + // Check that read permissions are always set |
|
357 | + // Link shares are allowed to have no read permissions to allow upload to hidden folders |
|
358 | + $noReadPermissionRequired = $share->getShareType() === IShare::TYPE_LINK |
|
359 | + || $share->getShareType() === IShare::TYPE_EMAIL; |
|
360 | + if (!$noReadPermissionRequired && |
|
361 | + ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { |
|
362 | + throw new \InvalidArgumentException('Shares need at least read permissions'); |
|
363 | + } |
|
364 | + |
|
365 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
366 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { |
|
367 | + $message_t = $this->l->t('Files can’t be shared with delete permissions'); |
|
368 | + throw new GenericShareException($message_t); |
|
369 | + } |
|
370 | + if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { |
|
371 | + $message_t = $this->l->t('Files can’t be shared with create permissions'); |
|
372 | + throw new GenericShareException($message_t); |
|
373 | + } |
|
374 | + } |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Validate if the expiration date fits the system settings |
|
379 | + * |
|
380 | + * @param IShare $share The share to validate the expiration date of |
|
381 | + * @return IShare The modified share object |
|
382 | + * @throws GenericShareException |
|
383 | + * @throws \InvalidArgumentException |
|
384 | + * @throws \Exception |
|
385 | + */ |
|
386 | + protected function validateExpirationDateInternal(IShare $share) { |
|
387 | + $expirationDate = $share->getExpirationDate(); |
|
388 | + |
|
389 | + if ($expirationDate !== null) { |
|
390 | + //Make sure the expiration date is a date |
|
391 | + $expirationDate->setTime(0, 0, 0); |
|
392 | + |
|
393 | + $date = new \DateTime(); |
|
394 | + $date->setTime(0, 0, 0); |
|
395 | + if ($date >= $expirationDate) { |
|
396 | + $message = $this->l->t('Expiration date is in the past'); |
|
397 | + throw new GenericShareException($message, $message, 404); |
|
398 | + } |
|
399 | + } |
|
400 | + |
|
401 | + // If expiredate is empty set a default one if there is a default |
|
402 | + $fullId = null; |
|
403 | + try { |
|
404 | + $fullId = $share->getFullId(); |
|
405 | + } catch (\UnexpectedValueException $e) { |
|
406 | + // This is a new share |
|
407 | + } |
|
408 | + |
|
409 | + if ($fullId === null && $expirationDate === null && $this->shareApiInternalDefaultExpireDate()) { |
|
410 | + $expirationDate = new \DateTime(); |
|
411 | + $expirationDate->setTime(0,0,0); |
|
412 | + |
|
413 | + $days = (int)$this->config->getAppValue('core', 'internal_defaultExpDays', (string)$this->shareApiInternalDefaultExpireDays()); |
|
414 | + if ($days > $this->shareApiInternalDefaultExpireDays()) { |
|
415 | + $days = $this->shareApiInternalDefaultExpireDays(); |
|
416 | + } |
|
417 | + $expirationDate->add(new \DateInterval('P'.$days.'D')); |
|
418 | + } |
|
419 | + |
|
420 | + // If we enforce the expiration date check that is does not exceed |
|
421 | + if ($this->shareApiInternalDefaultExpireDateEnforced()) { |
|
422 | + if ($expirationDate === null) { |
|
423 | + throw new \InvalidArgumentException('Expiration date is enforced'); |
|
424 | + } |
|
425 | + |
|
426 | + $date = new \DateTime(); |
|
427 | + $date->setTime(0, 0, 0); |
|
428 | + $date->add(new \DateInterval('P' . $this->shareApiInternalDefaultExpireDays() . 'D')); |
|
429 | + if ($date < $expirationDate) { |
|
430 | + $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiInternalDefaultExpireDays()]); |
|
431 | + throw new GenericShareException($message, $message, 404); |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + $accepted = true; |
|
436 | + $message = ''; |
|
437 | + \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
438 | + 'expirationDate' => &$expirationDate, |
|
439 | + 'accepted' => &$accepted, |
|
440 | + 'message' => &$message, |
|
441 | + 'passwordSet' => $share->getPassword() !== null, |
|
442 | + ]); |
|
443 | + |
|
444 | + if (!$accepted) { |
|
445 | + throw new \Exception($message); |
|
446 | + } |
|
447 | + |
|
448 | + $share->setExpirationDate($expirationDate); |
|
449 | + |
|
450 | + return $share; |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Validate if the expiration date fits the system settings |
|
455 | + * |
|
456 | + * @param IShare $share The share to validate the expiration date of |
|
457 | + * @return IShare The modified share object |
|
458 | + * @throws GenericShareException |
|
459 | + * @throws \InvalidArgumentException |
|
460 | + * @throws \Exception |
|
461 | + */ |
|
462 | + protected function validateExpirationDateLink(IShare $share) { |
|
463 | + $expirationDate = $share->getExpirationDate(); |
|
464 | + |
|
465 | + if ($expirationDate !== null) { |
|
466 | + //Make sure the expiration date is a date |
|
467 | + $expirationDate->setTime(0, 0, 0); |
|
468 | + |
|
469 | + $date = new \DateTime(); |
|
470 | + $date->setTime(0, 0, 0); |
|
471 | + if ($date >= $expirationDate) { |
|
472 | + $message = $this->l->t('Expiration date is in the past'); |
|
473 | + throw new GenericShareException($message, $message, 404); |
|
474 | + } |
|
475 | + } |
|
476 | + |
|
477 | + // If expiredate is empty set a default one if there is a default |
|
478 | + $fullId = null; |
|
479 | + try { |
|
480 | + $fullId = $share->getFullId(); |
|
481 | + } catch (\UnexpectedValueException $e) { |
|
482 | + // This is a new share |
|
483 | + } |
|
484 | + |
|
485 | + if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { |
|
486 | + $expirationDate = new \DateTime(); |
|
487 | + $expirationDate->setTime(0,0,0); |
|
488 | + |
|
489 | + $days = (int)$this->config->getAppValue('core', 'link_defaultExpDays', $this->shareApiLinkDefaultExpireDays()); |
|
490 | + if ($days > $this->shareApiLinkDefaultExpireDays()) { |
|
491 | + $days = $this->shareApiLinkDefaultExpireDays(); |
|
492 | + } |
|
493 | + $expirationDate->add(new \DateInterval('P'.$days.'D')); |
|
494 | + } |
|
495 | + |
|
496 | + // If we enforce the expiration date check that is does not exceed |
|
497 | + if ($this->shareApiLinkDefaultExpireDateEnforced()) { |
|
498 | + if ($expirationDate === null) { |
|
499 | + throw new \InvalidArgumentException('Expiration date is enforced'); |
|
500 | + } |
|
501 | + |
|
502 | + $date = new \DateTime(); |
|
503 | + $date->setTime(0, 0, 0); |
|
504 | + $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); |
|
505 | + if ($date < $expirationDate) { |
|
506 | + $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); |
|
507 | + throw new GenericShareException($message, $message, 404); |
|
508 | + } |
|
509 | + } |
|
510 | + |
|
511 | + $accepted = true; |
|
512 | + $message = ''; |
|
513 | + \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ |
|
514 | + 'expirationDate' => &$expirationDate, |
|
515 | + 'accepted' => &$accepted, |
|
516 | + 'message' => &$message, |
|
517 | + 'passwordSet' => $share->getPassword() !== null, |
|
518 | + ]); |
|
519 | + |
|
520 | + if (!$accepted) { |
|
521 | + throw new \Exception($message); |
|
522 | + } |
|
523 | + |
|
524 | + $share->setExpirationDate($expirationDate); |
|
525 | + |
|
526 | + return $share; |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Check for pre share requirements for user shares |
|
531 | + * |
|
532 | + * @param IShare $share |
|
533 | + * @throws \Exception |
|
534 | + */ |
|
535 | + protected function userCreateChecks(IShare $share) { |
|
536 | + // Check if we can share with group members only |
|
537 | + if ($this->shareWithGroupMembersOnly()) { |
|
538 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
539 | + $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
540 | + // Verify we can share with this user |
|
541 | + $groups = array_intersect( |
|
542 | + $this->groupManager->getUserGroupIds($sharedBy), |
|
543 | + $this->groupManager->getUserGroupIds($sharedWith) |
|
544 | + ); |
|
545 | + if (empty($groups)) { |
|
546 | + $message_t = $this->l->t('Sharing is only allowed with group members'); |
|
547 | + throw new \Exception($message_t); |
|
548 | + } |
|
549 | + } |
|
550 | + |
|
551 | + /* |
|
552 | 552 | * TODO: Could be costly, fix |
553 | 553 | * |
554 | 554 | * Also this is not what we want in the future.. then we want to squash identical shares. |
555 | 555 | */ |
556 | - $provider = $this->factory->getProviderForType(IShare::TYPE_USER); |
|
557 | - $existingShares = $provider->getSharesByPath($share->getNode()); |
|
558 | - foreach ($existingShares as $existingShare) { |
|
559 | - // Ignore if it is the same share |
|
560 | - try { |
|
561 | - if ($existingShare->getFullId() === $share->getFullId()) { |
|
562 | - continue; |
|
563 | - } |
|
564 | - } catch (\UnexpectedValueException $e) { |
|
565 | - //Shares are not identical |
|
566 | - } |
|
567 | - |
|
568 | - // Identical share already exists |
|
569 | - if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) { |
|
570 | - $message = $this->l->t('Sharing %s failed, because this item is already shared with user %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]); |
|
571 | - throw new AlreadySharedException($message, $existingShare); |
|
572 | - } |
|
573 | - |
|
574 | - // The share is already shared with this user via a group share |
|
575 | - if ($existingShare->getShareType() === IShare::TYPE_GROUP) { |
|
576 | - $group = $this->groupManager->get($existingShare->getSharedWith()); |
|
577 | - if (!is_null($group)) { |
|
578 | - $user = $this->userManager->get($share->getSharedWith()); |
|
579 | - |
|
580 | - if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { |
|
581 | - $message = $this->l->t('Sharing %s failed, because this item is already shared with user %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]); |
|
582 | - throw new AlreadySharedException($message, $existingShare); |
|
583 | - } |
|
584 | - } |
|
585 | - } |
|
586 | - } |
|
587 | - } |
|
588 | - |
|
589 | - /** |
|
590 | - * Check for pre share requirements for group shares |
|
591 | - * |
|
592 | - * @param IShare $share |
|
593 | - * @throws \Exception |
|
594 | - */ |
|
595 | - protected function groupCreateChecks(IShare $share) { |
|
596 | - // Verify group shares are allowed |
|
597 | - if (!$this->allowGroupSharing()) { |
|
598 | - throw new \Exception('Group sharing is now allowed'); |
|
599 | - } |
|
600 | - |
|
601 | - // Verify if the user can share with this group |
|
602 | - if ($this->shareWithGroupMembersOnly()) { |
|
603 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
604 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
605 | - if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { |
|
606 | - throw new \Exception('Sharing is only allowed within your own groups'); |
|
607 | - } |
|
608 | - } |
|
609 | - |
|
610 | - /* |
|
556 | + $provider = $this->factory->getProviderForType(IShare::TYPE_USER); |
|
557 | + $existingShares = $provider->getSharesByPath($share->getNode()); |
|
558 | + foreach ($existingShares as $existingShare) { |
|
559 | + // Ignore if it is the same share |
|
560 | + try { |
|
561 | + if ($existingShare->getFullId() === $share->getFullId()) { |
|
562 | + continue; |
|
563 | + } |
|
564 | + } catch (\UnexpectedValueException $e) { |
|
565 | + //Shares are not identical |
|
566 | + } |
|
567 | + |
|
568 | + // Identical share already exists |
|
569 | + if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) { |
|
570 | + $message = $this->l->t('Sharing %s failed, because this item is already shared with user %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]); |
|
571 | + throw new AlreadySharedException($message, $existingShare); |
|
572 | + } |
|
573 | + |
|
574 | + // The share is already shared with this user via a group share |
|
575 | + if ($existingShare->getShareType() === IShare::TYPE_GROUP) { |
|
576 | + $group = $this->groupManager->get($existingShare->getSharedWith()); |
|
577 | + if (!is_null($group)) { |
|
578 | + $user = $this->userManager->get($share->getSharedWith()); |
|
579 | + |
|
580 | + if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { |
|
581 | + $message = $this->l->t('Sharing %s failed, because this item is already shared with user %s', [$share->getNode()->getName(), $share->getSharedWithDisplayName()]); |
|
582 | + throw new AlreadySharedException($message, $existingShare); |
|
583 | + } |
|
584 | + } |
|
585 | + } |
|
586 | + } |
|
587 | + } |
|
588 | + |
|
589 | + /** |
|
590 | + * Check for pre share requirements for group shares |
|
591 | + * |
|
592 | + * @param IShare $share |
|
593 | + * @throws \Exception |
|
594 | + */ |
|
595 | + protected function groupCreateChecks(IShare $share) { |
|
596 | + // Verify group shares are allowed |
|
597 | + if (!$this->allowGroupSharing()) { |
|
598 | + throw new \Exception('Group sharing is now allowed'); |
|
599 | + } |
|
600 | + |
|
601 | + // Verify if the user can share with this group |
|
602 | + if ($this->shareWithGroupMembersOnly()) { |
|
603 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
604 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
605 | + if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { |
|
606 | + throw new \Exception('Sharing is only allowed within your own groups'); |
|
607 | + } |
|
608 | + } |
|
609 | + |
|
610 | + /* |
|
611 | 611 | * TODO: Could be costly, fix |
612 | 612 | * |
613 | 613 | * Also this is not what we want in the future.. then we want to squash identical shares. |
614 | 614 | */ |
615 | - $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP); |
|
616 | - $existingShares = $provider->getSharesByPath($share->getNode()); |
|
617 | - foreach ($existingShares as $existingShare) { |
|
618 | - try { |
|
619 | - if ($existingShare->getFullId() === $share->getFullId()) { |
|
620 | - continue; |
|
621 | - } |
|
622 | - } catch (\UnexpectedValueException $e) { |
|
623 | - //It is a new share so just continue |
|
624 | - } |
|
625 | - |
|
626 | - if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) { |
|
627 | - throw new AlreadySharedException('Path is already shared with this group', $existingShare); |
|
628 | - } |
|
629 | - } |
|
630 | - } |
|
631 | - |
|
632 | - /** |
|
633 | - * Check for pre share requirements for link shares |
|
634 | - * |
|
635 | - * @param IShare $share |
|
636 | - * @throws \Exception |
|
637 | - */ |
|
638 | - protected function linkCreateChecks(IShare $share) { |
|
639 | - // Are link shares allowed? |
|
640 | - if (!$this->shareApiAllowLinks()) { |
|
641 | - throw new \Exception('Link sharing is not allowed'); |
|
642 | - } |
|
643 | - |
|
644 | - // Check if public upload is allowed |
|
645 | - if (!$this->shareApiLinkAllowPublicUpload() && |
|
646 | - ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { |
|
647 | - throw new \InvalidArgumentException('Public upload is not allowed'); |
|
648 | - } |
|
649 | - } |
|
650 | - |
|
651 | - /** |
|
652 | - * To make sure we don't get invisible link shares we set the parent |
|
653 | - * of a link if it is a reshare. This is a quick word around |
|
654 | - * until we can properly display multiple link shares in the UI |
|
655 | - * |
|
656 | - * See: https://github.com/owncloud/core/issues/22295 |
|
657 | - * |
|
658 | - * FIXME: Remove once multiple link shares can be properly displayed |
|
659 | - * |
|
660 | - * @param IShare $share |
|
661 | - */ |
|
662 | - protected function setLinkParent(IShare $share) { |
|
663 | - |
|
664 | - // No sense in checking if the method is not there. |
|
665 | - if (method_exists($share, 'setParent')) { |
|
666 | - $storage = $share->getNode()->getStorage(); |
|
667 | - if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
668 | - /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
669 | - $share->setParent($storage->getShareId()); |
|
670 | - } |
|
671 | - } |
|
672 | - } |
|
673 | - |
|
674 | - /** |
|
675 | - * @param File|Folder $path |
|
676 | - */ |
|
677 | - protected function pathCreateChecks($path) { |
|
678 | - // Make sure that we do not share a path that contains a shared mountpoint |
|
679 | - if ($path instanceof \OCP\Files\Folder) { |
|
680 | - $mounts = $this->mountManager->findIn($path->getPath()); |
|
681 | - foreach ($mounts as $mount) { |
|
682 | - if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
683 | - throw new \InvalidArgumentException('Path contains files shared with you'); |
|
684 | - } |
|
685 | - } |
|
686 | - } |
|
687 | - } |
|
688 | - |
|
689 | - /** |
|
690 | - * Check if the user that is sharing can actually share |
|
691 | - * |
|
692 | - * @param IShare $share |
|
693 | - * @throws \Exception |
|
694 | - */ |
|
695 | - protected function canShare(IShare $share) { |
|
696 | - if (!$this->shareApiEnabled()) { |
|
697 | - throw new \Exception('Sharing is disabled'); |
|
698 | - } |
|
699 | - |
|
700 | - if ($this->sharingDisabledForUser($share->getSharedBy())) { |
|
701 | - throw new \Exception('Sharing is disabled for you'); |
|
702 | - } |
|
703 | - } |
|
704 | - |
|
705 | - /** |
|
706 | - * Share a path |
|
707 | - * |
|
708 | - * @param IShare $share |
|
709 | - * @return IShare The share object |
|
710 | - * @throws \Exception |
|
711 | - * |
|
712 | - * TODO: handle link share permissions or check them |
|
713 | - */ |
|
714 | - public function createShare(IShare $share) { |
|
715 | - $this->canShare($share); |
|
716 | - |
|
717 | - $this->generalCreateChecks($share); |
|
718 | - |
|
719 | - // Verify if there are any issues with the path |
|
720 | - $this->pathCreateChecks($share->getNode()); |
|
721 | - |
|
722 | - /* |
|
615 | + $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP); |
|
616 | + $existingShares = $provider->getSharesByPath($share->getNode()); |
|
617 | + foreach ($existingShares as $existingShare) { |
|
618 | + try { |
|
619 | + if ($existingShare->getFullId() === $share->getFullId()) { |
|
620 | + continue; |
|
621 | + } |
|
622 | + } catch (\UnexpectedValueException $e) { |
|
623 | + //It is a new share so just continue |
|
624 | + } |
|
625 | + |
|
626 | + if ($existingShare->getSharedWith() === $share->getSharedWith() && $existingShare->getShareType() === $share->getShareType()) { |
|
627 | + throw new AlreadySharedException('Path is already shared with this group', $existingShare); |
|
628 | + } |
|
629 | + } |
|
630 | + } |
|
631 | + |
|
632 | + /** |
|
633 | + * Check for pre share requirements for link shares |
|
634 | + * |
|
635 | + * @param IShare $share |
|
636 | + * @throws \Exception |
|
637 | + */ |
|
638 | + protected function linkCreateChecks(IShare $share) { |
|
639 | + // Are link shares allowed? |
|
640 | + if (!$this->shareApiAllowLinks()) { |
|
641 | + throw new \Exception('Link sharing is not allowed'); |
|
642 | + } |
|
643 | + |
|
644 | + // Check if public upload is allowed |
|
645 | + if (!$this->shareApiLinkAllowPublicUpload() && |
|
646 | + ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { |
|
647 | + throw new \InvalidArgumentException('Public upload is not allowed'); |
|
648 | + } |
|
649 | + } |
|
650 | + |
|
651 | + /** |
|
652 | + * To make sure we don't get invisible link shares we set the parent |
|
653 | + * of a link if it is a reshare. This is a quick word around |
|
654 | + * until we can properly display multiple link shares in the UI |
|
655 | + * |
|
656 | + * See: https://github.com/owncloud/core/issues/22295 |
|
657 | + * |
|
658 | + * FIXME: Remove once multiple link shares can be properly displayed |
|
659 | + * |
|
660 | + * @param IShare $share |
|
661 | + */ |
|
662 | + protected function setLinkParent(IShare $share) { |
|
663 | + |
|
664 | + // No sense in checking if the method is not there. |
|
665 | + if (method_exists($share, 'setParent')) { |
|
666 | + $storage = $share->getNode()->getStorage(); |
|
667 | + if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
668 | + /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
|
669 | + $share->setParent($storage->getShareId()); |
|
670 | + } |
|
671 | + } |
|
672 | + } |
|
673 | + |
|
674 | + /** |
|
675 | + * @param File|Folder $path |
|
676 | + */ |
|
677 | + protected function pathCreateChecks($path) { |
|
678 | + // Make sure that we do not share a path that contains a shared mountpoint |
|
679 | + if ($path instanceof \OCP\Files\Folder) { |
|
680 | + $mounts = $this->mountManager->findIn($path->getPath()); |
|
681 | + foreach ($mounts as $mount) { |
|
682 | + if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
683 | + throw new \InvalidArgumentException('Path contains files shared with you'); |
|
684 | + } |
|
685 | + } |
|
686 | + } |
|
687 | + } |
|
688 | + |
|
689 | + /** |
|
690 | + * Check if the user that is sharing can actually share |
|
691 | + * |
|
692 | + * @param IShare $share |
|
693 | + * @throws \Exception |
|
694 | + */ |
|
695 | + protected function canShare(IShare $share) { |
|
696 | + if (!$this->shareApiEnabled()) { |
|
697 | + throw new \Exception('Sharing is disabled'); |
|
698 | + } |
|
699 | + |
|
700 | + if ($this->sharingDisabledForUser($share->getSharedBy())) { |
|
701 | + throw new \Exception('Sharing is disabled for you'); |
|
702 | + } |
|
703 | + } |
|
704 | + |
|
705 | + /** |
|
706 | + * Share a path |
|
707 | + * |
|
708 | + * @param IShare $share |
|
709 | + * @return IShare The share object |
|
710 | + * @throws \Exception |
|
711 | + * |
|
712 | + * TODO: handle link share permissions or check them |
|
713 | + */ |
|
714 | + public function createShare(IShare $share) { |
|
715 | + $this->canShare($share); |
|
716 | + |
|
717 | + $this->generalCreateChecks($share); |
|
718 | + |
|
719 | + // Verify if there are any issues with the path |
|
720 | + $this->pathCreateChecks($share->getNode()); |
|
721 | + |
|
722 | + /* |
|
723 | 723 | * On creation of a share the owner is always the owner of the path |
724 | 724 | * Except for mounted federated shares. |
725 | 725 | */ |
726 | - $storage = $share->getNode()->getStorage(); |
|
727 | - if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
728 | - $parent = $share->getNode()->getParent(); |
|
729 | - while ($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
730 | - $parent = $parent->getParent(); |
|
731 | - } |
|
732 | - $share->setShareOwner($parent->getOwner()->getUID()); |
|
733 | - } else { |
|
734 | - if ($share->getNode()->getOwner()) { |
|
735 | - $share->setShareOwner($share->getNode()->getOwner()->getUID()); |
|
736 | - } else { |
|
737 | - $share->setShareOwner($share->getSharedBy()); |
|
738 | - } |
|
739 | - } |
|
740 | - |
|
741 | - try { |
|
742 | - //Verify share type |
|
743 | - if ($share->getShareType() === IShare::TYPE_USER) { |
|
744 | - $this->userCreateChecks($share); |
|
745 | - |
|
746 | - //Verify the expiration date |
|
747 | - $share = $this->validateExpirationDateInternal($share); |
|
748 | - } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
749 | - $this->groupCreateChecks($share); |
|
750 | - |
|
751 | - //Verify the expiration date |
|
752 | - $share = $this->validateExpirationDateInternal($share); |
|
753 | - } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
754 | - $this->linkCreateChecks($share); |
|
755 | - $this->setLinkParent($share); |
|
756 | - |
|
757 | - /* |
|
726 | + $storage = $share->getNode()->getStorage(); |
|
727 | + if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
728 | + $parent = $share->getNode()->getParent(); |
|
729 | + while ($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
|
730 | + $parent = $parent->getParent(); |
|
731 | + } |
|
732 | + $share->setShareOwner($parent->getOwner()->getUID()); |
|
733 | + } else { |
|
734 | + if ($share->getNode()->getOwner()) { |
|
735 | + $share->setShareOwner($share->getNode()->getOwner()->getUID()); |
|
736 | + } else { |
|
737 | + $share->setShareOwner($share->getSharedBy()); |
|
738 | + } |
|
739 | + } |
|
740 | + |
|
741 | + try { |
|
742 | + //Verify share type |
|
743 | + if ($share->getShareType() === IShare::TYPE_USER) { |
|
744 | + $this->userCreateChecks($share); |
|
745 | + |
|
746 | + //Verify the expiration date |
|
747 | + $share = $this->validateExpirationDateInternal($share); |
|
748 | + } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
749 | + $this->groupCreateChecks($share); |
|
750 | + |
|
751 | + //Verify the expiration date |
|
752 | + $share = $this->validateExpirationDateInternal($share); |
|
753 | + } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
754 | + $this->linkCreateChecks($share); |
|
755 | + $this->setLinkParent($share); |
|
756 | + |
|
757 | + /* |
|
758 | 758 | * For now ignore a set token. |
759 | 759 | */ |
760 | - $share->setToken( |
|
761 | - $this->secureRandom->generate( |
|
762 | - \OC\Share\Constants::TOKEN_LENGTH, |
|
763 | - \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
764 | - ) |
|
765 | - ); |
|
766 | - |
|
767 | - //Verify the expiration date |
|
768 | - $share = $this->validateExpirationDateLink($share); |
|
769 | - |
|
770 | - //Verify the password |
|
771 | - $this->verifyPassword($share->getPassword()); |
|
772 | - |
|
773 | - // If a password is set. Hash it! |
|
774 | - if ($share->getPassword() !== null) { |
|
775 | - $share->setPassword($this->hasher->hash($share->getPassword())); |
|
776 | - } |
|
777 | - } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
778 | - $share->setToken( |
|
779 | - $this->secureRandom->generate( |
|
780 | - \OC\Share\Constants::TOKEN_LENGTH, |
|
781 | - \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
782 | - ) |
|
783 | - ); |
|
784 | - } |
|
785 | - |
|
786 | - // Cannot share with the owner |
|
787 | - if ($share->getShareType() === IShare::TYPE_USER && |
|
788 | - $share->getSharedWith() === $share->getShareOwner()) { |
|
789 | - throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
790 | - } |
|
791 | - |
|
792 | - // Generate the target |
|
793 | - $target = $this->config->getSystemValue('share_folder', '/') . '/' . $share->getNode()->getName(); |
|
794 | - $target = \OC\Files\Filesystem::normalizePath($target); |
|
795 | - $share->setTarget($target); |
|
796 | - |
|
797 | - // Pre share event |
|
798 | - $event = new GenericEvent($share); |
|
799 | - $this->legacyDispatcher->dispatch('OCP\Share::preShare', $event); |
|
800 | - if ($event->isPropagationStopped() && $event->hasArgument('error')) { |
|
801 | - throw new \Exception($event->getArgument('error')); |
|
802 | - } |
|
803 | - |
|
804 | - $oldShare = $share; |
|
805 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
806 | - $share = $provider->create($share); |
|
807 | - //reuse the node we already have |
|
808 | - $share->setNode($oldShare->getNode()); |
|
809 | - |
|
810 | - // Reset the target if it is null for the new share |
|
811 | - if ($share->getTarget() === '') { |
|
812 | - $share->setTarget($target); |
|
813 | - } |
|
814 | - } catch (AlreadySharedException $e) { |
|
815 | - // if a share for the same target already exists, dont create a new one, but do trigger the hooks and notifications again |
|
816 | - $share = $e->getExistingShare(); |
|
817 | - } |
|
818 | - |
|
819 | - // Post share event |
|
820 | - $event = new GenericEvent($share); |
|
821 | - $this->legacyDispatcher->dispatch('OCP\Share::postShare', $event); |
|
822 | - |
|
823 | - $this->dispatcher->dispatchTyped(new Share\Events\ShareCreatedEvent($share)); |
|
824 | - |
|
825 | - if ($this->config->getSystemValueBool('sharing.enable_share_mail', true) |
|
826 | - && $share->getShareType() === IShare::TYPE_USER) { |
|
827 | - $mailSend = $share->getMailSend(); |
|
828 | - if ($mailSend === true) { |
|
829 | - $user = $this->userManager->get($share->getSharedWith()); |
|
830 | - if ($user !== null) { |
|
831 | - $emailAddress = $user->getEMailAddress(); |
|
832 | - if ($emailAddress !== null && $emailAddress !== '') { |
|
833 | - $userLang = $this->l10nFactory->getUserLanguage($user); |
|
834 | - $l = $this->l10nFactory->get('lib', $userLang); |
|
835 | - $this->sendMailNotification( |
|
836 | - $l, |
|
837 | - $share->getNode()->getName(), |
|
838 | - $this->urlGenerator->linkToRouteAbsolute('files_sharing.Accept.accept', ['shareId' => $share->getFullId()]), |
|
839 | - $share->getSharedBy(), |
|
840 | - $emailAddress, |
|
841 | - $share->getExpirationDate() |
|
842 | - ); |
|
843 | - $this->logger->debug('Sent share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); |
|
844 | - } else { |
|
845 | - $this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); |
|
846 | - } |
|
847 | - } else { |
|
848 | - $this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); |
|
849 | - } |
|
850 | - } else { |
|
851 | - $this->logger->debug('Share notification not sent because mailsend is false.', ['app' => 'share']); |
|
852 | - } |
|
853 | - } |
|
854 | - |
|
855 | - return $share; |
|
856 | - } |
|
857 | - |
|
858 | - /** |
|
859 | - * Send mail notifications |
|
860 | - * |
|
861 | - * This method will catch and log mail transmission errors |
|
862 | - * |
|
863 | - * @param IL10N $l Language of the recipient |
|
864 | - * @param string $filename file/folder name |
|
865 | - * @param string $link link to the file/folder |
|
866 | - * @param string $initiator user ID of share sender |
|
867 | - * @param string $shareWith email address of share receiver |
|
868 | - * @param \DateTime|null $expiration |
|
869 | - */ |
|
870 | - protected function sendMailNotification(IL10N $l, |
|
871 | - $filename, |
|
872 | - $link, |
|
873 | - $initiator, |
|
874 | - $shareWith, |
|
875 | - \DateTime $expiration = null) { |
|
876 | - $initiatorUser = $this->userManager->get($initiator); |
|
877 | - $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
878 | - |
|
879 | - $message = $this->mailer->createMessage(); |
|
880 | - |
|
881 | - $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [ |
|
882 | - 'filename' => $filename, |
|
883 | - 'link' => $link, |
|
884 | - 'initiator' => $initiatorDisplayName, |
|
885 | - 'expiration' => $expiration, |
|
886 | - 'shareWith' => $shareWith, |
|
887 | - ]); |
|
888 | - |
|
889 | - $emailTemplate->setSubject($l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename])); |
|
890 | - $emailTemplate->addHeader(); |
|
891 | - $emailTemplate->addHeading($l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false); |
|
892 | - $text = $l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]); |
|
893 | - |
|
894 | - $emailTemplate->addBodyText( |
|
895 | - htmlspecialchars($text . ' ' . $l->t('Click the button below to open it.')), |
|
896 | - $text |
|
897 | - ); |
|
898 | - $emailTemplate->addBodyButton( |
|
899 | - $l->t('Open »%s«', [$filename]), |
|
900 | - $link |
|
901 | - ); |
|
902 | - |
|
903 | - $message->setTo([$shareWith]); |
|
904 | - |
|
905 | - // The "From" contains the sharers name |
|
906 | - $instanceName = $this->defaults->getName(); |
|
907 | - $senderName = $l->t( |
|
908 | - '%1$s via %2$s', |
|
909 | - [ |
|
910 | - $initiatorDisplayName, |
|
911 | - $instanceName |
|
912 | - ] |
|
913 | - ); |
|
914 | - $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
915 | - |
|
916 | - // The "Reply-To" is set to the sharer if an mail address is configured |
|
917 | - // also the default footer contains a "Do not reply" which needs to be adjusted. |
|
918 | - $initiatorEmail = $initiatorUser->getEMailAddress(); |
|
919 | - if ($initiatorEmail !== null) { |
|
920 | - $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); |
|
921 | - $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan($l->getLanguageCode()) !== '' ? ' - ' . $this->defaults->getSlogan($l->getLanguageCode()) : '')); |
|
922 | - } else { |
|
923 | - $emailTemplate->addFooter('', $l->getLanguageCode()); |
|
924 | - } |
|
925 | - |
|
926 | - $message->useTemplate($emailTemplate); |
|
927 | - try { |
|
928 | - $failedRecipients = $this->mailer->send($message); |
|
929 | - if (!empty($failedRecipients)) { |
|
930 | - $this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failedRecipients)); |
|
931 | - return; |
|
932 | - } |
|
933 | - } catch (\Exception $e) { |
|
934 | - $this->logger->logException($e, ['message' => 'Share notification mail could not be sent']); |
|
935 | - } |
|
936 | - } |
|
937 | - |
|
938 | - /** |
|
939 | - * Update a share |
|
940 | - * |
|
941 | - * @param IShare $share |
|
942 | - * @return IShare The share object |
|
943 | - * @throws \InvalidArgumentException |
|
944 | - */ |
|
945 | - public function updateShare(IShare $share) { |
|
946 | - $expirationDateUpdated = false; |
|
947 | - |
|
948 | - $this->canShare($share); |
|
949 | - |
|
950 | - try { |
|
951 | - $originalShare = $this->getShareById($share->getFullId()); |
|
952 | - } catch (\UnexpectedValueException $e) { |
|
953 | - throw new \InvalidArgumentException('Share does not have a full id'); |
|
954 | - } |
|
955 | - |
|
956 | - // We can't change the share type! |
|
957 | - if ($share->getShareType() !== $originalShare->getShareType()) { |
|
958 | - throw new \InvalidArgumentException('Can’t change share type'); |
|
959 | - } |
|
960 | - |
|
961 | - // We can only change the recipient on user shares |
|
962 | - if ($share->getSharedWith() !== $originalShare->getSharedWith() && |
|
963 | - $share->getShareType() !== IShare::TYPE_USER) { |
|
964 | - throw new \InvalidArgumentException('Can only update recipient on user shares'); |
|
965 | - } |
|
966 | - |
|
967 | - // Cannot share with the owner |
|
968 | - if ($share->getShareType() === IShare::TYPE_USER && |
|
969 | - $share->getSharedWith() === $share->getShareOwner()) { |
|
970 | - throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
971 | - } |
|
972 | - |
|
973 | - $this->generalCreateChecks($share); |
|
974 | - |
|
975 | - if ($share->getShareType() === IShare::TYPE_USER) { |
|
976 | - $this->userCreateChecks($share); |
|
977 | - |
|
978 | - if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
979 | - //Verify the expiration date |
|
980 | - $this->validateExpirationDateInternal($share); |
|
981 | - $expirationDateUpdated = true; |
|
982 | - } |
|
983 | - } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
984 | - $this->groupCreateChecks($share); |
|
985 | - |
|
986 | - if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
987 | - //Verify the expiration date |
|
988 | - $this->validateExpirationDateInternal($share); |
|
989 | - $expirationDateUpdated = true; |
|
990 | - } |
|
991 | - } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
992 | - $this->linkCreateChecks($share); |
|
993 | - |
|
994 | - $plainTextPassword = $share->getPassword(); |
|
995 | - |
|
996 | - $this->updateSharePasswordIfNeeded($share, $originalShare); |
|
997 | - |
|
998 | - if (empty($plainTextPassword) && $share->getSendPasswordByTalk()) { |
|
999 | - throw new \InvalidArgumentException('Can’t enable sending the password by Talk with an empty password'); |
|
1000 | - } |
|
1001 | - |
|
1002 | - if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
1003 | - //Verify the expiration date |
|
1004 | - $this->validateExpirationDateLink($share); |
|
1005 | - $expirationDateUpdated = true; |
|
1006 | - } |
|
1007 | - } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
1008 | - // The new password is not set again if it is the same as the old |
|
1009 | - // one. |
|
1010 | - $plainTextPassword = $share->getPassword(); |
|
1011 | - if (!empty($plainTextPassword) && !$this->updateSharePasswordIfNeeded($share, $originalShare)) { |
|
1012 | - $plainTextPassword = null; |
|
1013 | - } |
|
1014 | - if (empty($plainTextPassword) && !$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) { |
|
1015 | - // If the same password was already sent by mail the recipient |
|
1016 | - // would already have access to the share without having to call |
|
1017 | - // the sharer to verify her identity |
|
1018 | - throw new \InvalidArgumentException('Can’t enable sending the password by Talk without setting a new password'); |
|
1019 | - } elseif (empty($plainTextPassword) && $originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()) { |
|
1020 | - throw new \InvalidArgumentException('Can’t disable sending the password by Talk without setting a new password'); |
|
1021 | - } |
|
1022 | - } |
|
1023 | - |
|
1024 | - $this->pathCreateChecks($share->getNode()); |
|
1025 | - |
|
1026 | - // Now update the share! |
|
1027 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1028 | - if ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
1029 | - $share = $provider->update($share, $plainTextPassword); |
|
1030 | - } else { |
|
1031 | - $share = $provider->update($share); |
|
1032 | - } |
|
1033 | - |
|
1034 | - if ($expirationDateUpdated === true) { |
|
1035 | - \OC_Hook::emit(Share::class, 'post_set_expiration_date', [ |
|
1036 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
1037 | - 'itemSource' => $share->getNode()->getId(), |
|
1038 | - 'date' => $share->getExpirationDate(), |
|
1039 | - 'uidOwner' => $share->getSharedBy(), |
|
1040 | - ]); |
|
1041 | - } |
|
1042 | - |
|
1043 | - if ($share->getPassword() !== $originalShare->getPassword()) { |
|
1044 | - \OC_Hook::emit(Share::class, 'post_update_password', [ |
|
1045 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
1046 | - 'itemSource' => $share->getNode()->getId(), |
|
1047 | - 'uidOwner' => $share->getSharedBy(), |
|
1048 | - 'token' => $share->getToken(), |
|
1049 | - 'disabled' => is_null($share->getPassword()), |
|
1050 | - ]); |
|
1051 | - } |
|
1052 | - |
|
1053 | - if ($share->getPermissions() !== $originalShare->getPermissions()) { |
|
1054 | - if ($this->userManager->userExists($share->getShareOwner())) { |
|
1055 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
1056 | - } else { |
|
1057 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
1058 | - } |
|
1059 | - \OC_Hook::emit(Share::class, 'post_update_permissions', [ |
|
1060 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
1061 | - 'itemSource' => $share->getNode()->getId(), |
|
1062 | - 'shareType' => $share->getShareType(), |
|
1063 | - 'shareWith' => $share->getSharedWith(), |
|
1064 | - 'uidOwner' => $share->getSharedBy(), |
|
1065 | - 'permissions' => $share->getPermissions(), |
|
1066 | - 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), |
|
1067 | - ]); |
|
1068 | - } |
|
1069 | - |
|
1070 | - return $share; |
|
1071 | - } |
|
1072 | - |
|
1073 | - /** |
|
1074 | - * Accept a share. |
|
1075 | - * |
|
1076 | - * @param IShare $share |
|
1077 | - * @param string $recipientId |
|
1078 | - * @return IShare The share object |
|
1079 | - * @throws \InvalidArgumentException |
|
1080 | - * @since 9.0.0 |
|
1081 | - */ |
|
1082 | - public function acceptShare(IShare $share, string $recipientId): IShare { |
|
1083 | - [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1084 | - $provider = $this->factory->getProvider($providerId); |
|
1085 | - |
|
1086 | - if (!method_exists($provider, 'acceptShare')) { |
|
1087 | - // TODO FIX ME |
|
1088 | - throw new \InvalidArgumentException('Share provider does not support accepting'); |
|
1089 | - } |
|
1090 | - $provider->acceptShare($share, $recipientId); |
|
1091 | - $event = new GenericEvent($share); |
|
1092 | - $this->legacyDispatcher->dispatch('OCP\Share::postAcceptShare', $event); |
|
1093 | - |
|
1094 | - return $share; |
|
1095 | - } |
|
1096 | - |
|
1097 | - /** |
|
1098 | - * Updates the password of the given share if it is not the same as the |
|
1099 | - * password of the original share. |
|
1100 | - * |
|
1101 | - * @param IShare $share the share to update its password. |
|
1102 | - * @param IShare $originalShare the original share to compare its |
|
1103 | - * password with. |
|
1104 | - * @return boolean whether the password was updated or not. |
|
1105 | - */ |
|
1106 | - private function updateSharePasswordIfNeeded(IShare $share, IShare $originalShare) { |
|
1107 | - $passwordsAreDifferent = ($share->getPassword() !== $originalShare->getPassword()) && |
|
1108 | - (($share->getPassword() !== null && $originalShare->getPassword() === null) || |
|
1109 | - ($share->getPassword() === null && $originalShare->getPassword() !== null) || |
|
1110 | - ($share->getPassword() !== null && $originalShare->getPassword() !== null && |
|
1111 | - !$this->hasher->verify($share->getPassword(), $originalShare->getPassword()))); |
|
1112 | - |
|
1113 | - // Password updated. |
|
1114 | - if ($passwordsAreDifferent) { |
|
1115 | - //Verify the password |
|
1116 | - $this->verifyPassword($share->getPassword()); |
|
1117 | - |
|
1118 | - // If a password is set. Hash it! |
|
1119 | - if ($share->getPassword() !== null) { |
|
1120 | - $share->setPassword($this->hasher->hash($share->getPassword())); |
|
1121 | - |
|
1122 | - return true; |
|
1123 | - } |
|
1124 | - } else { |
|
1125 | - // Reset the password to the original one, as it is either the same |
|
1126 | - // as the "new" password or a hashed version of it. |
|
1127 | - $share->setPassword($originalShare->getPassword()); |
|
1128 | - } |
|
1129 | - |
|
1130 | - return false; |
|
1131 | - } |
|
1132 | - |
|
1133 | - /** |
|
1134 | - * Delete all the children of this share |
|
1135 | - * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
1136 | - * |
|
1137 | - * @param IShare $share |
|
1138 | - * @return IShare[] List of deleted shares |
|
1139 | - */ |
|
1140 | - protected function deleteChildren(IShare $share) { |
|
1141 | - $deletedShares = []; |
|
1142 | - |
|
1143 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1144 | - |
|
1145 | - foreach ($provider->getChildren($share) as $child) { |
|
1146 | - $deletedChildren = $this->deleteChildren($child); |
|
1147 | - $deletedShares = array_merge($deletedShares, $deletedChildren); |
|
1148 | - |
|
1149 | - $provider->delete($child); |
|
1150 | - $this->dispatcher->dispatchTyped(new Share\Events\ShareDeletedEvent($child)); |
|
1151 | - $deletedShares[] = $child; |
|
1152 | - } |
|
1153 | - |
|
1154 | - return $deletedShares; |
|
1155 | - } |
|
1156 | - |
|
1157 | - /** |
|
1158 | - * Delete a share |
|
1159 | - * |
|
1160 | - * @param IShare $share |
|
1161 | - * @throws ShareNotFound |
|
1162 | - * @throws \InvalidArgumentException |
|
1163 | - */ |
|
1164 | - public function deleteShare(IShare $share) { |
|
1165 | - try { |
|
1166 | - $share->getFullId(); |
|
1167 | - } catch (\UnexpectedValueException $e) { |
|
1168 | - throw new \InvalidArgumentException('Share does not have a full id'); |
|
1169 | - } |
|
1170 | - |
|
1171 | - $event = new GenericEvent($share); |
|
1172 | - $this->legacyDispatcher->dispatch('OCP\Share::preUnshare', $event); |
|
1173 | - |
|
1174 | - // Get all children and delete them as well |
|
1175 | - $deletedShares = $this->deleteChildren($share); |
|
1176 | - |
|
1177 | - // Do the actual delete |
|
1178 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1179 | - $provider->delete($share); |
|
1180 | - |
|
1181 | - $this->dispatcher->dispatchTyped(new Share\Events\ShareDeletedEvent($share)); |
|
1182 | - |
|
1183 | - // All the deleted shares caused by this delete |
|
1184 | - $deletedShares[] = $share; |
|
1185 | - |
|
1186 | - // Emit post hook |
|
1187 | - $event->setArgument('deletedShares', $deletedShares); |
|
1188 | - $this->legacyDispatcher->dispatch('OCP\Share::postUnshare', $event); |
|
1189 | - } |
|
1190 | - |
|
1191 | - |
|
1192 | - /** |
|
1193 | - * Unshare a file as the recipient. |
|
1194 | - * This can be different from a regular delete for example when one of |
|
1195 | - * the users in a groups deletes that share. But the provider should |
|
1196 | - * handle this. |
|
1197 | - * |
|
1198 | - * @param IShare $share |
|
1199 | - * @param string $recipientId |
|
1200 | - */ |
|
1201 | - public function deleteFromSelf(IShare $share, $recipientId) { |
|
1202 | - [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1203 | - $provider = $this->factory->getProvider($providerId); |
|
1204 | - |
|
1205 | - $provider->deleteFromSelf($share, $recipientId); |
|
1206 | - $event = new GenericEvent($share); |
|
1207 | - $this->legacyDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event); |
|
1208 | - } |
|
1209 | - |
|
1210 | - public function restoreShare(IShare $share, string $recipientId): IShare { |
|
1211 | - [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1212 | - $provider = $this->factory->getProvider($providerId); |
|
1213 | - |
|
1214 | - return $provider->restore($share, $recipientId); |
|
1215 | - } |
|
1216 | - |
|
1217 | - /** |
|
1218 | - * @inheritdoc |
|
1219 | - */ |
|
1220 | - public function moveShare(IShare $share, $recipientId) { |
|
1221 | - if ($share->getShareType() === IShare::TYPE_LINK) { |
|
1222 | - throw new \InvalidArgumentException('Can’t change target of link share'); |
|
1223 | - } |
|
1224 | - |
|
1225 | - if ($share->getShareType() === IShare::TYPE_USER && $share->getSharedWith() !== $recipientId) { |
|
1226 | - throw new \InvalidArgumentException('Invalid recipient'); |
|
1227 | - } |
|
1228 | - |
|
1229 | - if ($share->getShareType() === IShare::TYPE_GROUP) { |
|
1230 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
1231 | - if (is_null($sharedWith)) { |
|
1232 | - throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
1233 | - } |
|
1234 | - $recipient = $this->userManager->get($recipientId); |
|
1235 | - if (!$sharedWith->inGroup($recipient)) { |
|
1236 | - throw new \InvalidArgumentException('Invalid recipient'); |
|
1237 | - } |
|
1238 | - } |
|
1239 | - |
|
1240 | - [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1241 | - $provider = $this->factory->getProvider($providerId); |
|
1242 | - |
|
1243 | - return $provider->move($share, $recipientId); |
|
1244 | - } |
|
1245 | - |
|
1246 | - public function getSharesInFolder($userId, Folder $node, $reshares = false) { |
|
1247 | - $providers = $this->factory->getAllProviders(); |
|
1248 | - |
|
1249 | - return array_reduce($providers, function ($shares, IShareProvider $provider) use ($userId, $node, $reshares) { |
|
1250 | - $newShares = $provider->getSharesInFolder($userId, $node, $reshares); |
|
1251 | - foreach ($newShares as $fid => $data) { |
|
1252 | - if (!isset($shares[$fid])) { |
|
1253 | - $shares[$fid] = []; |
|
1254 | - } |
|
1255 | - |
|
1256 | - $shares[$fid] = array_merge($shares[$fid], $data); |
|
1257 | - } |
|
1258 | - return $shares; |
|
1259 | - }, []); |
|
1260 | - } |
|
1261 | - |
|
1262 | - /** |
|
1263 | - * @inheritdoc |
|
1264 | - */ |
|
1265 | - public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { |
|
1266 | - if ($path !== null && |
|
1267 | - !($path instanceof \OCP\Files\File) && |
|
1268 | - !($path instanceof \OCP\Files\Folder)) { |
|
1269 | - throw new \InvalidArgumentException('invalid path'); |
|
1270 | - } |
|
1271 | - |
|
1272 | - try { |
|
1273 | - $provider = $this->factory->getProviderForType($shareType); |
|
1274 | - } catch (ProviderException $e) { |
|
1275 | - return []; |
|
1276 | - } |
|
1277 | - |
|
1278 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
1279 | - |
|
1280 | - /* |
|
760 | + $share->setToken( |
|
761 | + $this->secureRandom->generate( |
|
762 | + \OC\Share\Constants::TOKEN_LENGTH, |
|
763 | + \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
764 | + ) |
|
765 | + ); |
|
766 | + |
|
767 | + //Verify the expiration date |
|
768 | + $share = $this->validateExpirationDateLink($share); |
|
769 | + |
|
770 | + //Verify the password |
|
771 | + $this->verifyPassword($share->getPassword()); |
|
772 | + |
|
773 | + // If a password is set. Hash it! |
|
774 | + if ($share->getPassword() !== null) { |
|
775 | + $share->setPassword($this->hasher->hash($share->getPassword())); |
|
776 | + } |
|
777 | + } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
778 | + $share->setToken( |
|
779 | + $this->secureRandom->generate( |
|
780 | + \OC\Share\Constants::TOKEN_LENGTH, |
|
781 | + \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE |
|
782 | + ) |
|
783 | + ); |
|
784 | + } |
|
785 | + |
|
786 | + // Cannot share with the owner |
|
787 | + if ($share->getShareType() === IShare::TYPE_USER && |
|
788 | + $share->getSharedWith() === $share->getShareOwner()) { |
|
789 | + throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
790 | + } |
|
791 | + |
|
792 | + // Generate the target |
|
793 | + $target = $this->config->getSystemValue('share_folder', '/') . '/' . $share->getNode()->getName(); |
|
794 | + $target = \OC\Files\Filesystem::normalizePath($target); |
|
795 | + $share->setTarget($target); |
|
796 | + |
|
797 | + // Pre share event |
|
798 | + $event = new GenericEvent($share); |
|
799 | + $this->legacyDispatcher->dispatch('OCP\Share::preShare', $event); |
|
800 | + if ($event->isPropagationStopped() && $event->hasArgument('error')) { |
|
801 | + throw new \Exception($event->getArgument('error')); |
|
802 | + } |
|
803 | + |
|
804 | + $oldShare = $share; |
|
805 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
806 | + $share = $provider->create($share); |
|
807 | + //reuse the node we already have |
|
808 | + $share->setNode($oldShare->getNode()); |
|
809 | + |
|
810 | + // Reset the target if it is null for the new share |
|
811 | + if ($share->getTarget() === '') { |
|
812 | + $share->setTarget($target); |
|
813 | + } |
|
814 | + } catch (AlreadySharedException $e) { |
|
815 | + // if a share for the same target already exists, dont create a new one, but do trigger the hooks and notifications again |
|
816 | + $share = $e->getExistingShare(); |
|
817 | + } |
|
818 | + |
|
819 | + // Post share event |
|
820 | + $event = new GenericEvent($share); |
|
821 | + $this->legacyDispatcher->dispatch('OCP\Share::postShare', $event); |
|
822 | + |
|
823 | + $this->dispatcher->dispatchTyped(new Share\Events\ShareCreatedEvent($share)); |
|
824 | + |
|
825 | + if ($this->config->getSystemValueBool('sharing.enable_share_mail', true) |
|
826 | + && $share->getShareType() === IShare::TYPE_USER) { |
|
827 | + $mailSend = $share->getMailSend(); |
|
828 | + if ($mailSend === true) { |
|
829 | + $user = $this->userManager->get($share->getSharedWith()); |
|
830 | + if ($user !== null) { |
|
831 | + $emailAddress = $user->getEMailAddress(); |
|
832 | + if ($emailAddress !== null && $emailAddress !== '') { |
|
833 | + $userLang = $this->l10nFactory->getUserLanguage($user); |
|
834 | + $l = $this->l10nFactory->get('lib', $userLang); |
|
835 | + $this->sendMailNotification( |
|
836 | + $l, |
|
837 | + $share->getNode()->getName(), |
|
838 | + $this->urlGenerator->linkToRouteAbsolute('files_sharing.Accept.accept', ['shareId' => $share->getFullId()]), |
|
839 | + $share->getSharedBy(), |
|
840 | + $emailAddress, |
|
841 | + $share->getExpirationDate() |
|
842 | + ); |
|
843 | + $this->logger->debug('Sent share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); |
|
844 | + } else { |
|
845 | + $this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); |
|
846 | + } |
|
847 | + } else { |
|
848 | + $this->logger->debug('Share notification not sent to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); |
|
849 | + } |
|
850 | + } else { |
|
851 | + $this->logger->debug('Share notification not sent because mailsend is false.', ['app' => 'share']); |
|
852 | + } |
|
853 | + } |
|
854 | + |
|
855 | + return $share; |
|
856 | + } |
|
857 | + |
|
858 | + /** |
|
859 | + * Send mail notifications |
|
860 | + * |
|
861 | + * This method will catch and log mail transmission errors |
|
862 | + * |
|
863 | + * @param IL10N $l Language of the recipient |
|
864 | + * @param string $filename file/folder name |
|
865 | + * @param string $link link to the file/folder |
|
866 | + * @param string $initiator user ID of share sender |
|
867 | + * @param string $shareWith email address of share receiver |
|
868 | + * @param \DateTime|null $expiration |
|
869 | + */ |
|
870 | + protected function sendMailNotification(IL10N $l, |
|
871 | + $filename, |
|
872 | + $link, |
|
873 | + $initiator, |
|
874 | + $shareWith, |
|
875 | + \DateTime $expiration = null) { |
|
876 | + $initiatorUser = $this->userManager->get($initiator); |
|
877 | + $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; |
|
878 | + |
|
879 | + $message = $this->mailer->createMessage(); |
|
880 | + |
|
881 | + $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [ |
|
882 | + 'filename' => $filename, |
|
883 | + 'link' => $link, |
|
884 | + 'initiator' => $initiatorDisplayName, |
|
885 | + 'expiration' => $expiration, |
|
886 | + 'shareWith' => $shareWith, |
|
887 | + ]); |
|
888 | + |
|
889 | + $emailTemplate->setSubject($l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename])); |
|
890 | + $emailTemplate->addHeader(); |
|
891 | + $emailTemplate->addHeading($l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false); |
|
892 | + $text = $l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]); |
|
893 | + |
|
894 | + $emailTemplate->addBodyText( |
|
895 | + htmlspecialchars($text . ' ' . $l->t('Click the button below to open it.')), |
|
896 | + $text |
|
897 | + ); |
|
898 | + $emailTemplate->addBodyButton( |
|
899 | + $l->t('Open »%s«', [$filename]), |
|
900 | + $link |
|
901 | + ); |
|
902 | + |
|
903 | + $message->setTo([$shareWith]); |
|
904 | + |
|
905 | + // The "From" contains the sharers name |
|
906 | + $instanceName = $this->defaults->getName(); |
|
907 | + $senderName = $l->t( |
|
908 | + '%1$s via %2$s', |
|
909 | + [ |
|
910 | + $initiatorDisplayName, |
|
911 | + $instanceName |
|
912 | + ] |
|
913 | + ); |
|
914 | + $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); |
|
915 | + |
|
916 | + // The "Reply-To" is set to the sharer if an mail address is configured |
|
917 | + // also the default footer contains a "Do not reply" which needs to be adjusted. |
|
918 | + $initiatorEmail = $initiatorUser->getEMailAddress(); |
|
919 | + if ($initiatorEmail !== null) { |
|
920 | + $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); |
|
921 | + $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan($l->getLanguageCode()) !== '' ? ' - ' . $this->defaults->getSlogan($l->getLanguageCode()) : '')); |
|
922 | + } else { |
|
923 | + $emailTemplate->addFooter('', $l->getLanguageCode()); |
|
924 | + } |
|
925 | + |
|
926 | + $message->useTemplate($emailTemplate); |
|
927 | + try { |
|
928 | + $failedRecipients = $this->mailer->send($message); |
|
929 | + if (!empty($failedRecipients)) { |
|
930 | + $this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failedRecipients)); |
|
931 | + return; |
|
932 | + } |
|
933 | + } catch (\Exception $e) { |
|
934 | + $this->logger->logException($e, ['message' => 'Share notification mail could not be sent']); |
|
935 | + } |
|
936 | + } |
|
937 | + |
|
938 | + /** |
|
939 | + * Update a share |
|
940 | + * |
|
941 | + * @param IShare $share |
|
942 | + * @return IShare The share object |
|
943 | + * @throws \InvalidArgumentException |
|
944 | + */ |
|
945 | + public function updateShare(IShare $share) { |
|
946 | + $expirationDateUpdated = false; |
|
947 | + |
|
948 | + $this->canShare($share); |
|
949 | + |
|
950 | + try { |
|
951 | + $originalShare = $this->getShareById($share->getFullId()); |
|
952 | + } catch (\UnexpectedValueException $e) { |
|
953 | + throw new \InvalidArgumentException('Share does not have a full id'); |
|
954 | + } |
|
955 | + |
|
956 | + // We can't change the share type! |
|
957 | + if ($share->getShareType() !== $originalShare->getShareType()) { |
|
958 | + throw new \InvalidArgumentException('Can’t change share type'); |
|
959 | + } |
|
960 | + |
|
961 | + // We can only change the recipient on user shares |
|
962 | + if ($share->getSharedWith() !== $originalShare->getSharedWith() && |
|
963 | + $share->getShareType() !== IShare::TYPE_USER) { |
|
964 | + throw new \InvalidArgumentException('Can only update recipient on user shares'); |
|
965 | + } |
|
966 | + |
|
967 | + // Cannot share with the owner |
|
968 | + if ($share->getShareType() === IShare::TYPE_USER && |
|
969 | + $share->getSharedWith() === $share->getShareOwner()) { |
|
970 | + throw new \InvalidArgumentException('Can’t share with the share owner'); |
|
971 | + } |
|
972 | + |
|
973 | + $this->generalCreateChecks($share); |
|
974 | + |
|
975 | + if ($share->getShareType() === IShare::TYPE_USER) { |
|
976 | + $this->userCreateChecks($share); |
|
977 | + |
|
978 | + if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
979 | + //Verify the expiration date |
|
980 | + $this->validateExpirationDateInternal($share); |
|
981 | + $expirationDateUpdated = true; |
|
982 | + } |
|
983 | + } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
984 | + $this->groupCreateChecks($share); |
|
985 | + |
|
986 | + if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
987 | + //Verify the expiration date |
|
988 | + $this->validateExpirationDateInternal($share); |
|
989 | + $expirationDateUpdated = true; |
|
990 | + } |
|
991 | + } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
992 | + $this->linkCreateChecks($share); |
|
993 | + |
|
994 | + $plainTextPassword = $share->getPassword(); |
|
995 | + |
|
996 | + $this->updateSharePasswordIfNeeded($share, $originalShare); |
|
997 | + |
|
998 | + if (empty($plainTextPassword) && $share->getSendPasswordByTalk()) { |
|
999 | + throw new \InvalidArgumentException('Can’t enable sending the password by Talk with an empty password'); |
|
1000 | + } |
|
1001 | + |
|
1002 | + if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { |
|
1003 | + //Verify the expiration date |
|
1004 | + $this->validateExpirationDateLink($share); |
|
1005 | + $expirationDateUpdated = true; |
|
1006 | + } |
|
1007 | + } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
1008 | + // The new password is not set again if it is the same as the old |
|
1009 | + // one. |
|
1010 | + $plainTextPassword = $share->getPassword(); |
|
1011 | + if (!empty($plainTextPassword) && !$this->updateSharePasswordIfNeeded($share, $originalShare)) { |
|
1012 | + $plainTextPassword = null; |
|
1013 | + } |
|
1014 | + if (empty($plainTextPassword) && !$originalShare->getSendPasswordByTalk() && $share->getSendPasswordByTalk()) { |
|
1015 | + // If the same password was already sent by mail the recipient |
|
1016 | + // would already have access to the share without having to call |
|
1017 | + // the sharer to verify her identity |
|
1018 | + throw new \InvalidArgumentException('Can’t enable sending the password by Talk without setting a new password'); |
|
1019 | + } elseif (empty($plainTextPassword) && $originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()) { |
|
1020 | + throw new \InvalidArgumentException('Can’t disable sending the password by Talk without setting a new password'); |
|
1021 | + } |
|
1022 | + } |
|
1023 | + |
|
1024 | + $this->pathCreateChecks($share->getNode()); |
|
1025 | + |
|
1026 | + // Now update the share! |
|
1027 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1028 | + if ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
1029 | + $share = $provider->update($share, $plainTextPassword); |
|
1030 | + } else { |
|
1031 | + $share = $provider->update($share); |
|
1032 | + } |
|
1033 | + |
|
1034 | + if ($expirationDateUpdated === true) { |
|
1035 | + \OC_Hook::emit(Share::class, 'post_set_expiration_date', [ |
|
1036 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
1037 | + 'itemSource' => $share->getNode()->getId(), |
|
1038 | + 'date' => $share->getExpirationDate(), |
|
1039 | + 'uidOwner' => $share->getSharedBy(), |
|
1040 | + ]); |
|
1041 | + } |
|
1042 | + |
|
1043 | + if ($share->getPassword() !== $originalShare->getPassword()) { |
|
1044 | + \OC_Hook::emit(Share::class, 'post_update_password', [ |
|
1045 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
1046 | + 'itemSource' => $share->getNode()->getId(), |
|
1047 | + 'uidOwner' => $share->getSharedBy(), |
|
1048 | + 'token' => $share->getToken(), |
|
1049 | + 'disabled' => is_null($share->getPassword()), |
|
1050 | + ]); |
|
1051 | + } |
|
1052 | + |
|
1053 | + if ($share->getPermissions() !== $originalShare->getPermissions()) { |
|
1054 | + if ($this->userManager->userExists($share->getShareOwner())) { |
|
1055 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
1056 | + } else { |
|
1057 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
1058 | + } |
|
1059 | + \OC_Hook::emit(Share::class, 'post_update_permissions', [ |
|
1060 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', |
|
1061 | + 'itemSource' => $share->getNode()->getId(), |
|
1062 | + 'shareType' => $share->getShareType(), |
|
1063 | + 'shareWith' => $share->getSharedWith(), |
|
1064 | + 'uidOwner' => $share->getSharedBy(), |
|
1065 | + 'permissions' => $share->getPermissions(), |
|
1066 | + 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), |
|
1067 | + ]); |
|
1068 | + } |
|
1069 | + |
|
1070 | + return $share; |
|
1071 | + } |
|
1072 | + |
|
1073 | + /** |
|
1074 | + * Accept a share. |
|
1075 | + * |
|
1076 | + * @param IShare $share |
|
1077 | + * @param string $recipientId |
|
1078 | + * @return IShare The share object |
|
1079 | + * @throws \InvalidArgumentException |
|
1080 | + * @since 9.0.0 |
|
1081 | + */ |
|
1082 | + public function acceptShare(IShare $share, string $recipientId): IShare { |
|
1083 | + [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1084 | + $provider = $this->factory->getProvider($providerId); |
|
1085 | + |
|
1086 | + if (!method_exists($provider, 'acceptShare')) { |
|
1087 | + // TODO FIX ME |
|
1088 | + throw new \InvalidArgumentException('Share provider does not support accepting'); |
|
1089 | + } |
|
1090 | + $provider->acceptShare($share, $recipientId); |
|
1091 | + $event = new GenericEvent($share); |
|
1092 | + $this->legacyDispatcher->dispatch('OCP\Share::postAcceptShare', $event); |
|
1093 | + |
|
1094 | + return $share; |
|
1095 | + } |
|
1096 | + |
|
1097 | + /** |
|
1098 | + * Updates the password of the given share if it is not the same as the |
|
1099 | + * password of the original share. |
|
1100 | + * |
|
1101 | + * @param IShare $share the share to update its password. |
|
1102 | + * @param IShare $originalShare the original share to compare its |
|
1103 | + * password with. |
|
1104 | + * @return boolean whether the password was updated or not. |
|
1105 | + */ |
|
1106 | + private function updateSharePasswordIfNeeded(IShare $share, IShare $originalShare) { |
|
1107 | + $passwordsAreDifferent = ($share->getPassword() !== $originalShare->getPassword()) && |
|
1108 | + (($share->getPassword() !== null && $originalShare->getPassword() === null) || |
|
1109 | + ($share->getPassword() === null && $originalShare->getPassword() !== null) || |
|
1110 | + ($share->getPassword() !== null && $originalShare->getPassword() !== null && |
|
1111 | + !$this->hasher->verify($share->getPassword(), $originalShare->getPassword()))); |
|
1112 | + |
|
1113 | + // Password updated. |
|
1114 | + if ($passwordsAreDifferent) { |
|
1115 | + //Verify the password |
|
1116 | + $this->verifyPassword($share->getPassword()); |
|
1117 | + |
|
1118 | + // If a password is set. Hash it! |
|
1119 | + if ($share->getPassword() !== null) { |
|
1120 | + $share->setPassword($this->hasher->hash($share->getPassword())); |
|
1121 | + |
|
1122 | + return true; |
|
1123 | + } |
|
1124 | + } else { |
|
1125 | + // Reset the password to the original one, as it is either the same |
|
1126 | + // as the "new" password or a hashed version of it. |
|
1127 | + $share->setPassword($originalShare->getPassword()); |
|
1128 | + } |
|
1129 | + |
|
1130 | + return false; |
|
1131 | + } |
|
1132 | + |
|
1133 | + /** |
|
1134 | + * Delete all the children of this share |
|
1135 | + * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in |
|
1136 | + * |
|
1137 | + * @param IShare $share |
|
1138 | + * @return IShare[] List of deleted shares |
|
1139 | + */ |
|
1140 | + protected function deleteChildren(IShare $share) { |
|
1141 | + $deletedShares = []; |
|
1142 | + |
|
1143 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1144 | + |
|
1145 | + foreach ($provider->getChildren($share) as $child) { |
|
1146 | + $deletedChildren = $this->deleteChildren($child); |
|
1147 | + $deletedShares = array_merge($deletedShares, $deletedChildren); |
|
1148 | + |
|
1149 | + $provider->delete($child); |
|
1150 | + $this->dispatcher->dispatchTyped(new Share\Events\ShareDeletedEvent($child)); |
|
1151 | + $deletedShares[] = $child; |
|
1152 | + } |
|
1153 | + |
|
1154 | + return $deletedShares; |
|
1155 | + } |
|
1156 | + |
|
1157 | + /** |
|
1158 | + * Delete a share |
|
1159 | + * |
|
1160 | + * @param IShare $share |
|
1161 | + * @throws ShareNotFound |
|
1162 | + * @throws \InvalidArgumentException |
|
1163 | + */ |
|
1164 | + public function deleteShare(IShare $share) { |
|
1165 | + try { |
|
1166 | + $share->getFullId(); |
|
1167 | + } catch (\UnexpectedValueException $e) { |
|
1168 | + throw new \InvalidArgumentException('Share does not have a full id'); |
|
1169 | + } |
|
1170 | + |
|
1171 | + $event = new GenericEvent($share); |
|
1172 | + $this->legacyDispatcher->dispatch('OCP\Share::preUnshare', $event); |
|
1173 | + |
|
1174 | + // Get all children and delete them as well |
|
1175 | + $deletedShares = $this->deleteChildren($share); |
|
1176 | + |
|
1177 | + // Do the actual delete |
|
1178 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1179 | + $provider->delete($share); |
|
1180 | + |
|
1181 | + $this->dispatcher->dispatchTyped(new Share\Events\ShareDeletedEvent($share)); |
|
1182 | + |
|
1183 | + // All the deleted shares caused by this delete |
|
1184 | + $deletedShares[] = $share; |
|
1185 | + |
|
1186 | + // Emit post hook |
|
1187 | + $event->setArgument('deletedShares', $deletedShares); |
|
1188 | + $this->legacyDispatcher->dispatch('OCP\Share::postUnshare', $event); |
|
1189 | + } |
|
1190 | + |
|
1191 | + |
|
1192 | + /** |
|
1193 | + * Unshare a file as the recipient. |
|
1194 | + * This can be different from a regular delete for example when one of |
|
1195 | + * the users in a groups deletes that share. But the provider should |
|
1196 | + * handle this. |
|
1197 | + * |
|
1198 | + * @param IShare $share |
|
1199 | + * @param string $recipientId |
|
1200 | + */ |
|
1201 | + public function deleteFromSelf(IShare $share, $recipientId) { |
|
1202 | + [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1203 | + $provider = $this->factory->getProvider($providerId); |
|
1204 | + |
|
1205 | + $provider->deleteFromSelf($share, $recipientId); |
|
1206 | + $event = new GenericEvent($share); |
|
1207 | + $this->legacyDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event); |
|
1208 | + } |
|
1209 | + |
|
1210 | + public function restoreShare(IShare $share, string $recipientId): IShare { |
|
1211 | + [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1212 | + $provider = $this->factory->getProvider($providerId); |
|
1213 | + |
|
1214 | + return $provider->restore($share, $recipientId); |
|
1215 | + } |
|
1216 | + |
|
1217 | + /** |
|
1218 | + * @inheritdoc |
|
1219 | + */ |
|
1220 | + public function moveShare(IShare $share, $recipientId) { |
|
1221 | + if ($share->getShareType() === IShare::TYPE_LINK) { |
|
1222 | + throw new \InvalidArgumentException('Can’t change target of link share'); |
|
1223 | + } |
|
1224 | + |
|
1225 | + if ($share->getShareType() === IShare::TYPE_USER && $share->getSharedWith() !== $recipientId) { |
|
1226 | + throw new \InvalidArgumentException('Invalid recipient'); |
|
1227 | + } |
|
1228 | + |
|
1229 | + if ($share->getShareType() === IShare::TYPE_GROUP) { |
|
1230 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
1231 | + if (is_null($sharedWith)) { |
|
1232 | + throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); |
|
1233 | + } |
|
1234 | + $recipient = $this->userManager->get($recipientId); |
|
1235 | + if (!$sharedWith->inGroup($recipient)) { |
|
1236 | + throw new \InvalidArgumentException('Invalid recipient'); |
|
1237 | + } |
|
1238 | + } |
|
1239 | + |
|
1240 | + [$providerId, ] = $this->splitFullId($share->getFullId()); |
|
1241 | + $provider = $this->factory->getProvider($providerId); |
|
1242 | + |
|
1243 | + return $provider->move($share, $recipientId); |
|
1244 | + } |
|
1245 | + |
|
1246 | + public function getSharesInFolder($userId, Folder $node, $reshares = false) { |
|
1247 | + $providers = $this->factory->getAllProviders(); |
|
1248 | + |
|
1249 | + return array_reduce($providers, function ($shares, IShareProvider $provider) use ($userId, $node, $reshares) { |
|
1250 | + $newShares = $provider->getSharesInFolder($userId, $node, $reshares); |
|
1251 | + foreach ($newShares as $fid => $data) { |
|
1252 | + if (!isset($shares[$fid])) { |
|
1253 | + $shares[$fid] = []; |
|
1254 | + } |
|
1255 | + |
|
1256 | + $shares[$fid] = array_merge($shares[$fid], $data); |
|
1257 | + } |
|
1258 | + return $shares; |
|
1259 | + }, []); |
|
1260 | + } |
|
1261 | + |
|
1262 | + /** |
|
1263 | + * @inheritdoc |
|
1264 | + */ |
|
1265 | + public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { |
|
1266 | + if ($path !== null && |
|
1267 | + !($path instanceof \OCP\Files\File) && |
|
1268 | + !($path instanceof \OCP\Files\Folder)) { |
|
1269 | + throw new \InvalidArgumentException('invalid path'); |
|
1270 | + } |
|
1271 | + |
|
1272 | + try { |
|
1273 | + $provider = $this->factory->getProviderForType($shareType); |
|
1274 | + } catch (ProviderException $e) { |
|
1275 | + return []; |
|
1276 | + } |
|
1277 | + |
|
1278 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
1279 | + |
|
1280 | + /* |
|
1281 | 1281 | * Work around so we don't return expired shares but still follow |
1282 | 1282 | * proper pagination. |
1283 | 1283 | */ |
1284 | 1284 | |
1285 | - $shares2 = []; |
|
1286 | - |
|
1287 | - while (true) { |
|
1288 | - $added = 0; |
|
1289 | - foreach ($shares as $share) { |
|
1290 | - try { |
|
1291 | - $this->checkExpireDate($share); |
|
1292 | - } catch (ShareNotFound $e) { |
|
1293 | - //Ignore since this basically means the share is deleted |
|
1294 | - continue; |
|
1295 | - } |
|
1296 | - |
|
1297 | - $added++; |
|
1298 | - $shares2[] = $share; |
|
1299 | - |
|
1300 | - if (count($shares2) === $limit) { |
|
1301 | - break; |
|
1302 | - } |
|
1303 | - } |
|
1304 | - |
|
1305 | - // If we did not fetch more shares than the limit then there are no more shares |
|
1306 | - if (count($shares) < $limit) { |
|
1307 | - break; |
|
1308 | - } |
|
1309 | - |
|
1310 | - if (count($shares2) === $limit) { |
|
1311 | - break; |
|
1312 | - } |
|
1313 | - |
|
1314 | - // If there was no limit on the select we are done |
|
1315 | - if ($limit === -1) { |
|
1316 | - break; |
|
1317 | - } |
|
1318 | - |
|
1319 | - $offset += $added; |
|
1320 | - |
|
1321 | - // Fetch again $limit shares |
|
1322 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
1323 | - |
|
1324 | - // No more shares means we are done |
|
1325 | - if (empty($shares)) { |
|
1326 | - break; |
|
1327 | - } |
|
1328 | - } |
|
1329 | - |
|
1330 | - $shares = $shares2; |
|
1331 | - |
|
1332 | - return $shares; |
|
1333 | - } |
|
1334 | - |
|
1335 | - /** |
|
1336 | - * @inheritdoc |
|
1337 | - */ |
|
1338 | - public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
1339 | - try { |
|
1340 | - $provider = $this->factory->getProviderForType($shareType); |
|
1341 | - } catch (ProviderException $e) { |
|
1342 | - return []; |
|
1343 | - } |
|
1344 | - |
|
1345 | - $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
1346 | - |
|
1347 | - // remove all shares which are already expired |
|
1348 | - foreach ($shares as $key => $share) { |
|
1349 | - try { |
|
1350 | - $this->checkExpireDate($share); |
|
1351 | - } catch (ShareNotFound $e) { |
|
1352 | - unset($shares[$key]); |
|
1353 | - } |
|
1354 | - } |
|
1355 | - |
|
1356 | - return $shares; |
|
1357 | - } |
|
1358 | - |
|
1359 | - /** |
|
1360 | - * @inheritdoc |
|
1361 | - */ |
|
1362 | - public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
1363 | - $shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
1364 | - |
|
1365 | - // Only get deleted shares |
|
1366 | - $shares = array_filter($shares, function (IShare $share) { |
|
1367 | - return $share->getPermissions() === 0; |
|
1368 | - }); |
|
1369 | - |
|
1370 | - // Only get shares where the owner still exists |
|
1371 | - $shares = array_filter($shares, function (IShare $share) { |
|
1372 | - return $this->userManager->userExists($share->getShareOwner()); |
|
1373 | - }); |
|
1374 | - |
|
1375 | - return $shares; |
|
1376 | - } |
|
1377 | - |
|
1378 | - /** |
|
1379 | - * @inheritdoc |
|
1380 | - */ |
|
1381 | - public function getShareById($id, $recipient = null) { |
|
1382 | - if ($id === null) { |
|
1383 | - throw new ShareNotFound(); |
|
1384 | - } |
|
1385 | - |
|
1386 | - [$providerId, $id] = $this->splitFullId($id); |
|
1387 | - |
|
1388 | - try { |
|
1389 | - $provider = $this->factory->getProvider($providerId); |
|
1390 | - } catch (ProviderException $e) { |
|
1391 | - throw new ShareNotFound(); |
|
1392 | - } |
|
1393 | - |
|
1394 | - $share = $provider->getShareById($id, $recipient); |
|
1395 | - |
|
1396 | - $this->checkExpireDate($share); |
|
1397 | - |
|
1398 | - return $share; |
|
1399 | - } |
|
1400 | - |
|
1401 | - /** |
|
1402 | - * Get all the shares for a given path |
|
1403 | - * |
|
1404 | - * @param \OCP\Files\Node $path |
|
1405 | - * @param int $page |
|
1406 | - * @param int $perPage |
|
1407 | - * |
|
1408 | - * @return Share[] |
|
1409 | - */ |
|
1410 | - public function getSharesByPath(\OCP\Files\Node $path, $page = 0, $perPage = 50) { |
|
1411 | - return []; |
|
1412 | - } |
|
1413 | - |
|
1414 | - /** |
|
1415 | - * Get the share by token possible with password |
|
1416 | - * |
|
1417 | - * @param string $token |
|
1418 | - * @return IShare |
|
1419 | - * |
|
1420 | - * @throws ShareNotFound |
|
1421 | - */ |
|
1422 | - public function getShareByToken($token) { |
|
1423 | - // tokens can't be valid local user names |
|
1424 | - if ($this->userManager->userExists($token)) { |
|
1425 | - throw new ShareNotFound(); |
|
1426 | - } |
|
1427 | - $share = null; |
|
1428 | - try { |
|
1429 | - if ($this->shareApiAllowLinks()) { |
|
1430 | - $provider = $this->factory->getProviderForType(IShare::TYPE_LINK); |
|
1431 | - $share = $provider->getShareByToken($token); |
|
1432 | - } |
|
1433 | - } catch (ProviderException $e) { |
|
1434 | - } catch (ShareNotFound $e) { |
|
1435 | - } |
|
1436 | - |
|
1437 | - |
|
1438 | - // If it is not a link share try to fetch a federated share by token |
|
1439 | - if ($share === null) { |
|
1440 | - try { |
|
1441 | - $provider = $this->factory->getProviderForType(IShare::TYPE_REMOTE); |
|
1442 | - $share = $provider->getShareByToken($token); |
|
1443 | - } catch (ProviderException $e) { |
|
1444 | - } catch (ShareNotFound $e) { |
|
1445 | - } |
|
1446 | - } |
|
1447 | - |
|
1448 | - // If it is not a link share try to fetch a mail share by token |
|
1449 | - if ($share === null && $this->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
1450 | - try { |
|
1451 | - $provider = $this->factory->getProviderForType(IShare::TYPE_EMAIL); |
|
1452 | - $share = $provider->getShareByToken($token); |
|
1453 | - } catch (ProviderException $e) { |
|
1454 | - } catch (ShareNotFound $e) { |
|
1455 | - } |
|
1456 | - } |
|
1457 | - |
|
1458 | - if ($share === null && $this->shareProviderExists(IShare::TYPE_CIRCLE)) { |
|
1459 | - try { |
|
1460 | - $provider = $this->factory->getProviderForType(IShare::TYPE_CIRCLE); |
|
1461 | - $share = $provider->getShareByToken($token); |
|
1462 | - } catch (ProviderException $e) { |
|
1463 | - } catch (ShareNotFound $e) { |
|
1464 | - } |
|
1465 | - } |
|
1466 | - |
|
1467 | - if ($share === null && $this->shareProviderExists(IShare::TYPE_ROOM)) { |
|
1468 | - try { |
|
1469 | - $provider = $this->factory->getProviderForType(IShare::TYPE_ROOM); |
|
1470 | - $share = $provider->getShareByToken($token); |
|
1471 | - } catch (ProviderException $e) { |
|
1472 | - } catch (ShareNotFound $e) { |
|
1473 | - } |
|
1474 | - } |
|
1475 | - |
|
1476 | - if ($share === null) { |
|
1477 | - throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
1478 | - } |
|
1479 | - |
|
1480 | - $this->checkExpireDate($share); |
|
1481 | - |
|
1482 | - /* |
|
1285 | + $shares2 = []; |
|
1286 | + |
|
1287 | + while (true) { |
|
1288 | + $added = 0; |
|
1289 | + foreach ($shares as $share) { |
|
1290 | + try { |
|
1291 | + $this->checkExpireDate($share); |
|
1292 | + } catch (ShareNotFound $e) { |
|
1293 | + //Ignore since this basically means the share is deleted |
|
1294 | + continue; |
|
1295 | + } |
|
1296 | + |
|
1297 | + $added++; |
|
1298 | + $shares2[] = $share; |
|
1299 | + |
|
1300 | + if (count($shares2) === $limit) { |
|
1301 | + break; |
|
1302 | + } |
|
1303 | + } |
|
1304 | + |
|
1305 | + // If we did not fetch more shares than the limit then there are no more shares |
|
1306 | + if (count($shares) < $limit) { |
|
1307 | + break; |
|
1308 | + } |
|
1309 | + |
|
1310 | + if (count($shares2) === $limit) { |
|
1311 | + break; |
|
1312 | + } |
|
1313 | + |
|
1314 | + // If there was no limit on the select we are done |
|
1315 | + if ($limit === -1) { |
|
1316 | + break; |
|
1317 | + } |
|
1318 | + |
|
1319 | + $offset += $added; |
|
1320 | + |
|
1321 | + // Fetch again $limit shares |
|
1322 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); |
|
1323 | + |
|
1324 | + // No more shares means we are done |
|
1325 | + if (empty($shares)) { |
|
1326 | + break; |
|
1327 | + } |
|
1328 | + } |
|
1329 | + |
|
1330 | + $shares = $shares2; |
|
1331 | + |
|
1332 | + return $shares; |
|
1333 | + } |
|
1334 | + |
|
1335 | + /** |
|
1336 | + * @inheritdoc |
|
1337 | + */ |
|
1338 | + public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
1339 | + try { |
|
1340 | + $provider = $this->factory->getProviderForType($shareType); |
|
1341 | + } catch (ProviderException $e) { |
|
1342 | + return []; |
|
1343 | + } |
|
1344 | + |
|
1345 | + $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
1346 | + |
|
1347 | + // remove all shares which are already expired |
|
1348 | + foreach ($shares as $key => $share) { |
|
1349 | + try { |
|
1350 | + $this->checkExpireDate($share); |
|
1351 | + } catch (ShareNotFound $e) { |
|
1352 | + unset($shares[$key]); |
|
1353 | + } |
|
1354 | + } |
|
1355 | + |
|
1356 | + return $shares; |
|
1357 | + } |
|
1358 | + |
|
1359 | + /** |
|
1360 | + * @inheritdoc |
|
1361 | + */ |
|
1362 | + public function getDeletedSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { |
|
1363 | + $shares = $this->getSharedWith($userId, $shareType, $node, $limit, $offset); |
|
1364 | + |
|
1365 | + // Only get deleted shares |
|
1366 | + $shares = array_filter($shares, function (IShare $share) { |
|
1367 | + return $share->getPermissions() === 0; |
|
1368 | + }); |
|
1369 | + |
|
1370 | + // Only get shares where the owner still exists |
|
1371 | + $shares = array_filter($shares, function (IShare $share) { |
|
1372 | + return $this->userManager->userExists($share->getShareOwner()); |
|
1373 | + }); |
|
1374 | + |
|
1375 | + return $shares; |
|
1376 | + } |
|
1377 | + |
|
1378 | + /** |
|
1379 | + * @inheritdoc |
|
1380 | + */ |
|
1381 | + public function getShareById($id, $recipient = null) { |
|
1382 | + if ($id === null) { |
|
1383 | + throw new ShareNotFound(); |
|
1384 | + } |
|
1385 | + |
|
1386 | + [$providerId, $id] = $this->splitFullId($id); |
|
1387 | + |
|
1388 | + try { |
|
1389 | + $provider = $this->factory->getProvider($providerId); |
|
1390 | + } catch (ProviderException $e) { |
|
1391 | + throw new ShareNotFound(); |
|
1392 | + } |
|
1393 | + |
|
1394 | + $share = $provider->getShareById($id, $recipient); |
|
1395 | + |
|
1396 | + $this->checkExpireDate($share); |
|
1397 | + |
|
1398 | + return $share; |
|
1399 | + } |
|
1400 | + |
|
1401 | + /** |
|
1402 | + * Get all the shares for a given path |
|
1403 | + * |
|
1404 | + * @param \OCP\Files\Node $path |
|
1405 | + * @param int $page |
|
1406 | + * @param int $perPage |
|
1407 | + * |
|
1408 | + * @return Share[] |
|
1409 | + */ |
|
1410 | + public function getSharesByPath(\OCP\Files\Node $path, $page = 0, $perPage = 50) { |
|
1411 | + return []; |
|
1412 | + } |
|
1413 | + |
|
1414 | + /** |
|
1415 | + * Get the share by token possible with password |
|
1416 | + * |
|
1417 | + * @param string $token |
|
1418 | + * @return IShare |
|
1419 | + * |
|
1420 | + * @throws ShareNotFound |
|
1421 | + */ |
|
1422 | + public function getShareByToken($token) { |
|
1423 | + // tokens can't be valid local user names |
|
1424 | + if ($this->userManager->userExists($token)) { |
|
1425 | + throw new ShareNotFound(); |
|
1426 | + } |
|
1427 | + $share = null; |
|
1428 | + try { |
|
1429 | + if ($this->shareApiAllowLinks()) { |
|
1430 | + $provider = $this->factory->getProviderForType(IShare::TYPE_LINK); |
|
1431 | + $share = $provider->getShareByToken($token); |
|
1432 | + } |
|
1433 | + } catch (ProviderException $e) { |
|
1434 | + } catch (ShareNotFound $e) { |
|
1435 | + } |
|
1436 | + |
|
1437 | + |
|
1438 | + // If it is not a link share try to fetch a federated share by token |
|
1439 | + if ($share === null) { |
|
1440 | + try { |
|
1441 | + $provider = $this->factory->getProviderForType(IShare::TYPE_REMOTE); |
|
1442 | + $share = $provider->getShareByToken($token); |
|
1443 | + } catch (ProviderException $e) { |
|
1444 | + } catch (ShareNotFound $e) { |
|
1445 | + } |
|
1446 | + } |
|
1447 | + |
|
1448 | + // If it is not a link share try to fetch a mail share by token |
|
1449 | + if ($share === null && $this->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
1450 | + try { |
|
1451 | + $provider = $this->factory->getProviderForType(IShare::TYPE_EMAIL); |
|
1452 | + $share = $provider->getShareByToken($token); |
|
1453 | + } catch (ProviderException $e) { |
|
1454 | + } catch (ShareNotFound $e) { |
|
1455 | + } |
|
1456 | + } |
|
1457 | + |
|
1458 | + if ($share === null && $this->shareProviderExists(IShare::TYPE_CIRCLE)) { |
|
1459 | + try { |
|
1460 | + $provider = $this->factory->getProviderForType(IShare::TYPE_CIRCLE); |
|
1461 | + $share = $provider->getShareByToken($token); |
|
1462 | + } catch (ProviderException $e) { |
|
1463 | + } catch (ShareNotFound $e) { |
|
1464 | + } |
|
1465 | + } |
|
1466 | + |
|
1467 | + if ($share === null && $this->shareProviderExists(IShare::TYPE_ROOM)) { |
|
1468 | + try { |
|
1469 | + $provider = $this->factory->getProviderForType(IShare::TYPE_ROOM); |
|
1470 | + $share = $provider->getShareByToken($token); |
|
1471 | + } catch (ProviderException $e) { |
|
1472 | + } catch (ShareNotFound $e) { |
|
1473 | + } |
|
1474 | + } |
|
1475 | + |
|
1476 | + if ($share === null) { |
|
1477 | + throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
1478 | + } |
|
1479 | + |
|
1480 | + $this->checkExpireDate($share); |
|
1481 | + |
|
1482 | + /* |
|
1483 | 1483 | * Reduce the permissions for link shares if public upload is not enabled |
1484 | 1484 | */ |
1485 | - if ($share->getShareType() === IShare::TYPE_LINK && |
|
1486 | - !$this->shareApiLinkAllowPublicUpload()) { |
|
1487 | - $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); |
|
1488 | - } |
|
1489 | - |
|
1490 | - return $share; |
|
1491 | - } |
|
1492 | - |
|
1493 | - protected function checkExpireDate($share) { |
|
1494 | - if ($share->isExpired()) { |
|
1495 | - $this->deleteShare($share); |
|
1496 | - throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
1497 | - } |
|
1498 | - } |
|
1499 | - |
|
1500 | - /** |
|
1501 | - * Verify the password of a public share |
|
1502 | - * |
|
1503 | - * @param IShare $share |
|
1504 | - * @param string $password |
|
1505 | - * @return bool |
|
1506 | - */ |
|
1507 | - public function checkPassword(IShare $share, $password) { |
|
1508 | - $passwordProtected = $share->getShareType() !== IShare::TYPE_LINK |
|
1509 | - || $share->getShareType() !== IShare::TYPE_EMAIL |
|
1510 | - || $share->getShareType() !== IShare::TYPE_CIRCLE; |
|
1511 | - if (!$passwordProtected) { |
|
1512 | - //TODO maybe exception? |
|
1513 | - return false; |
|
1514 | - } |
|
1515 | - |
|
1516 | - if ($password === null || $share->getPassword() === null) { |
|
1517 | - return false; |
|
1518 | - } |
|
1519 | - |
|
1520 | - $newHash = ''; |
|
1521 | - if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { |
|
1522 | - return false; |
|
1523 | - } |
|
1524 | - |
|
1525 | - if (!empty($newHash)) { |
|
1526 | - $share->setPassword($newHash); |
|
1527 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1528 | - $provider->update($share); |
|
1529 | - } |
|
1530 | - |
|
1531 | - return true; |
|
1532 | - } |
|
1533 | - |
|
1534 | - /** |
|
1535 | - * @inheritdoc |
|
1536 | - */ |
|
1537 | - public function userDeleted($uid) { |
|
1538 | - $types = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK, IShare::TYPE_REMOTE, IShare::TYPE_EMAIL]; |
|
1539 | - |
|
1540 | - foreach ($types as $type) { |
|
1541 | - try { |
|
1542 | - $provider = $this->factory->getProviderForType($type); |
|
1543 | - } catch (ProviderException $e) { |
|
1544 | - continue; |
|
1545 | - } |
|
1546 | - $provider->userDeleted($uid, $type); |
|
1547 | - } |
|
1548 | - } |
|
1549 | - |
|
1550 | - /** |
|
1551 | - * @inheritdoc |
|
1552 | - */ |
|
1553 | - public function groupDeleted($gid) { |
|
1554 | - $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP); |
|
1555 | - $provider->groupDeleted($gid); |
|
1556 | - |
|
1557 | - $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
1558 | - if ($excludedGroups === '') { |
|
1559 | - return; |
|
1560 | - } |
|
1561 | - |
|
1562 | - $excludedGroups = json_decode($excludedGroups, true); |
|
1563 | - if (json_last_error() !== JSON_ERROR_NONE) { |
|
1564 | - return; |
|
1565 | - } |
|
1566 | - |
|
1567 | - $excludedGroups = array_diff($excludedGroups, [$gid]); |
|
1568 | - $this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode($excludedGroups)); |
|
1569 | - } |
|
1570 | - |
|
1571 | - /** |
|
1572 | - * @inheritdoc |
|
1573 | - */ |
|
1574 | - public function userDeletedFromGroup($uid, $gid) { |
|
1575 | - $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP); |
|
1576 | - $provider->userDeletedFromGroup($uid, $gid); |
|
1577 | - } |
|
1578 | - |
|
1579 | - /** |
|
1580 | - * Get access list to a path. This means |
|
1581 | - * all the users that can access a given path. |
|
1582 | - * |
|
1583 | - * Consider: |
|
1584 | - * -root |
|
1585 | - * |-folder1 (23) |
|
1586 | - * |-folder2 (32) |
|
1587 | - * |-fileA (42) |
|
1588 | - * |
|
1589 | - * fileA is shared with user1 and user1@server1 |
|
1590 | - * folder2 is shared with group2 (user4 is a member of group2) |
|
1591 | - * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 |
|
1592 | - * |
|
1593 | - * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: |
|
1594 | - * [ |
|
1595 | - * users => [ |
|
1596 | - * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], |
|
1597 | - * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], |
|
1598 | - * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], |
|
1599 | - * ], |
|
1600 | - * remote => [ |
|
1601 | - * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], |
|
1602 | - * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], |
|
1603 | - * ], |
|
1604 | - * public => bool |
|
1605 | - * mail => bool |
|
1606 | - * ] |
|
1607 | - * |
|
1608 | - * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: |
|
1609 | - * [ |
|
1610 | - * users => ['user1', 'user2', 'user4'], |
|
1611 | - * remote => bool, |
|
1612 | - * public => bool |
|
1613 | - * mail => bool |
|
1614 | - * ] |
|
1615 | - * |
|
1616 | - * This is required for encryption/activity |
|
1617 | - * |
|
1618 | - * @param \OCP\Files\Node $path |
|
1619 | - * @param bool $recursive Should we check all parent folders as well |
|
1620 | - * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it) |
|
1621 | - * @return array |
|
1622 | - */ |
|
1623 | - public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { |
|
1624 | - $owner = $path->getOwner(); |
|
1625 | - |
|
1626 | - if ($owner === null) { |
|
1627 | - return []; |
|
1628 | - } |
|
1629 | - |
|
1630 | - $owner = $owner->getUID(); |
|
1631 | - |
|
1632 | - if ($currentAccess) { |
|
1633 | - $al = ['users' => [], 'remote' => [], 'public' => false]; |
|
1634 | - } else { |
|
1635 | - $al = ['users' => [], 'remote' => false, 'public' => false]; |
|
1636 | - } |
|
1637 | - if (!$this->userManager->userExists($owner)) { |
|
1638 | - return $al; |
|
1639 | - } |
|
1640 | - |
|
1641 | - //Get node for the owner and correct the owner in case of external storages |
|
1642 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
1643 | - if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { |
|
1644 | - $nodes = $userFolder->getById($path->getId()); |
|
1645 | - $path = array_shift($nodes); |
|
1646 | - if ($path->getOwner() === null) { |
|
1647 | - return []; |
|
1648 | - } |
|
1649 | - $owner = $path->getOwner()->getUID(); |
|
1650 | - } |
|
1651 | - |
|
1652 | - $providers = $this->factory->getAllProviders(); |
|
1653 | - |
|
1654 | - /** @var Node[] $nodes */ |
|
1655 | - $nodes = []; |
|
1656 | - |
|
1657 | - |
|
1658 | - if ($currentAccess) { |
|
1659 | - $ownerPath = $path->getPath(); |
|
1660 | - $ownerPath = explode('/', $ownerPath, 4); |
|
1661 | - if (count($ownerPath) < 4) { |
|
1662 | - $ownerPath = ''; |
|
1663 | - } else { |
|
1664 | - $ownerPath = $ownerPath[3]; |
|
1665 | - } |
|
1666 | - $al['users'][$owner] = [ |
|
1667 | - 'node_id' => $path->getId(), |
|
1668 | - 'node_path' => '/' . $ownerPath, |
|
1669 | - ]; |
|
1670 | - } else { |
|
1671 | - $al['users'][] = $owner; |
|
1672 | - } |
|
1673 | - |
|
1674 | - // Collect all the shares |
|
1675 | - while ($path->getPath() !== $userFolder->getPath()) { |
|
1676 | - $nodes[] = $path; |
|
1677 | - if (!$recursive) { |
|
1678 | - break; |
|
1679 | - } |
|
1680 | - $path = $path->getParent(); |
|
1681 | - } |
|
1682 | - |
|
1683 | - foreach ($providers as $provider) { |
|
1684 | - $tmp = $provider->getAccessList($nodes, $currentAccess); |
|
1685 | - |
|
1686 | - foreach ($tmp as $k => $v) { |
|
1687 | - if (isset($al[$k])) { |
|
1688 | - if (is_array($al[$k])) { |
|
1689 | - if ($currentAccess) { |
|
1690 | - $al[$k] += $v; |
|
1691 | - } else { |
|
1692 | - $al[$k] = array_merge($al[$k], $v); |
|
1693 | - $al[$k] = array_unique($al[$k]); |
|
1694 | - $al[$k] = array_values($al[$k]); |
|
1695 | - } |
|
1696 | - } else { |
|
1697 | - $al[$k] = $al[$k] || $v; |
|
1698 | - } |
|
1699 | - } else { |
|
1700 | - $al[$k] = $v; |
|
1701 | - } |
|
1702 | - } |
|
1703 | - } |
|
1704 | - |
|
1705 | - return $al; |
|
1706 | - } |
|
1707 | - |
|
1708 | - /** |
|
1709 | - * Create a new share |
|
1710 | - * |
|
1711 | - * @return IShare |
|
1712 | - */ |
|
1713 | - public function newShare() { |
|
1714 | - return new \OC\Share20\Share($this->rootFolder, $this->userManager); |
|
1715 | - } |
|
1716 | - |
|
1717 | - /** |
|
1718 | - * Is the share API enabled |
|
1719 | - * |
|
1720 | - * @return bool |
|
1721 | - */ |
|
1722 | - public function shareApiEnabled() { |
|
1723 | - return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; |
|
1724 | - } |
|
1725 | - |
|
1726 | - /** |
|
1727 | - * Is public link sharing enabled |
|
1728 | - * |
|
1729 | - * @return bool |
|
1730 | - */ |
|
1731 | - public function shareApiAllowLinks() { |
|
1732 | - return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; |
|
1733 | - } |
|
1734 | - |
|
1735 | - /** |
|
1736 | - * Is password on public link requires |
|
1737 | - * |
|
1738 | - * @return bool |
|
1739 | - */ |
|
1740 | - public function shareApiLinkEnforcePassword() { |
|
1741 | - return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; |
|
1742 | - } |
|
1743 | - |
|
1744 | - /** |
|
1745 | - * Is default link expire date enabled |
|
1746 | - * |
|
1747 | - * @return bool |
|
1748 | - */ |
|
1749 | - public function shareApiLinkDefaultExpireDate() { |
|
1750 | - return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; |
|
1751 | - } |
|
1752 | - |
|
1753 | - /** |
|
1754 | - * Is default link expire date enforced |
|
1755 | - *` |
|
1756 | - * @return bool |
|
1757 | - */ |
|
1758 | - public function shareApiLinkDefaultExpireDateEnforced() { |
|
1759 | - return $this->shareApiLinkDefaultExpireDate() && |
|
1760 | - $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; |
|
1761 | - } |
|
1762 | - |
|
1763 | - |
|
1764 | - /** |
|
1765 | - * Number of default link expire days |
|
1766 | - * @return int |
|
1767 | - */ |
|
1768 | - public function shareApiLinkDefaultExpireDays() { |
|
1769 | - return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
1770 | - } |
|
1771 | - |
|
1772 | - /** |
|
1773 | - * Is default internal expire date enabled |
|
1774 | - * |
|
1775 | - * @return bool |
|
1776 | - */ |
|
1777 | - public function shareApiInternalDefaultExpireDate(): bool { |
|
1778 | - return $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no') === 'yes'; |
|
1779 | - } |
|
1780 | - |
|
1781 | - /** |
|
1782 | - * Is default expire date enforced |
|
1783 | - *` |
|
1784 | - * @return bool |
|
1785 | - */ |
|
1786 | - public function shareApiInternalDefaultExpireDateEnforced(): bool { |
|
1787 | - return $this->shareApiInternalDefaultExpireDate() && |
|
1788 | - $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes'; |
|
1789 | - } |
|
1790 | - |
|
1791 | - |
|
1792 | - /** |
|
1793 | - * Number of default expire days |
|
1794 | - * @return int |
|
1795 | - */ |
|
1796 | - public function shareApiInternalDefaultExpireDays(): int { |
|
1797 | - return (int)$this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7'); |
|
1798 | - } |
|
1799 | - |
|
1800 | - /** |
|
1801 | - * Allow public upload on link shares |
|
1802 | - * |
|
1803 | - * @return bool |
|
1804 | - */ |
|
1805 | - public function shareApiLinkAllowPublicUpload() { |
|
1806 | - return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; |
|
1807 | - } |
|
1808 | - |
|
1809 | - /** |
|
1810 | - * check if user can only share with group members |
|
1811 | - * @return bool |
|
1812 | - */ |
|
1813 | - public function shareWithGroupMembersOnly() { |
|
1814 | - return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
1815 | - } |
|
1816 | - |
|
1817 | - /** |
|
1818 | - * Check if users can share with groups |
|
1819 | - * @return bool |
|
1820 | - */ |
|
1821 | - public function allowGroupSharing() { |
|
1822 | - return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; |
|
1823 | - } |
|
1824 | - |
|
1825 | - public function allowEnumeration(): bool { |
|
1826 | - return $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
1827 | - } |
|
1828 | - |
|
1829 | - public function limitEnumerationToGroups(): bool { |
|
1830 | - return $this->allowEnumeration() && |
|
1831 | - $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; |
|
1832 | - } |
|
1833 | - |
|
1834 | - public function limitEnumerationToPhone(): bool { |
|
1835 | - return $this->allowEnumeration() && |
|
1836 | - $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes'; |
|
1837 | - } |
|
1838 | - |
|
1839 | - public function allowEnumerationFullMatch(): bool { |
|
1840 | - return $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes'; |
|
1841 | - } |
|
1842 | - |
|
1843 | - /** |
|
1844 | - * Copied from \OC_Util::isSharingDisabledForUser |
|
1845 | - * |
|
1846 | - * TODO: Deprecate fuction from OC_Util |
|
1847 | - * |
|
1848 | - * @param string $userId |
|
1849 | - * @return bool |
|
1850 | - */ |
|
1851 | - public function sharingDisabledForUser($userId) { |
|
1852 | - if ($userId === null) { |
|
1853 | - return false; |
|
1854 | - } |
|
1855 | - |
|
1856 | - if (isset($this->sharingDisabledForUsersCache[$userId])) { |
|
1857 | - return $this->sharingDisabledForUsersCache[$userId]; |
|
1858 | - } |
|
1859 | - |
|
1860 | - if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
1861 | - $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
1862 | - $excludedGroups = json_decode($groupsList); |
|
1863 | - if (is_null($excludedGroups)) { |
|
1864 | - $excludedGroups = explode(',', $groupsList); |
|
1865 | - $newValue = json_encode($excludedGroups); |
|
1866 | - $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
1867 | - } |
|
1868 | - $user = $this->userManager->get($userId); |
|
1869 | - $usersGroups = $this->groupManager->getUserGroupIds($user); |
|
1870 | - if (!empty($usersGroups)) { |
|
1871 | - $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
1872 | - // if the user is only in groups which are disabled for sharing then |
|
1873 | - // sharing is also disabled for the user |
|
1874 | - if (empty($remainingGroups)) { |
|
1875 | - $this->sharingDisabledForUsersCache[$userId] = true; |
|
1876 | - return true; |
|
1877 | - } |
|
1878 | - } |
|
1879 | - } |
|
1880 | - |
|
1881 | - $this->sharingDisabledForUsersCache[$userId] = false; |
|
1882 | - return false; |
|
1883 | - } |
|
1884 | - |
|
1885 | - /** |
|
1886 | - * @inheritdoc |
|
1887 | - */ |
|
1888 | - public function outgoingServer2ServerSharesAllowed() { |
|
1889 | - return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; |
|
1890 | - } |
|
1891 | - |
|
1892 | - /** |
|
1893 | - * @inheritdoc |
|
1894 | - */ |
|
1895 | - public function outgoingServer2ServerGroupSharesAllowed() { |
|
1896 | - return $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no') === 'yes'; |
|
1897 | - } |
|
1898 | - |
|
1899 | - /** |
|
1900 | - * @inheritdoc |
|
1901 | - */ |
|
1902 | - public function shareProviderExists($shareType) { |
|
1903 | - try { |
|
1904 | - $this->factory->getProviderForType($shareType); |
|
1905 | - } catch (ProviderException $e) { |
|
1906 | - return false; |
|
1907 | - } |
|
1908 | - |
|
1909 | - return true; |
|
1910 | - } |
|
1911 | - |
|
1912 | - public function registerShareProvider(string $shareProviderClass): void { |
|
1913 | - $this->factory->registerProvider($shareProviderClass); |
|
1914 | - } |
|
1915 | - |
|
1916 | - public function getAllShares(): iterable { |
|
1917 | - $providers = $this->factory->getAllProviders(); |
|
1918 | - |
|
1919 | - foreach ($providers as $provider) { |
|
1920 | - yield from $provider->getAllShares(); |
|
1921 | - } |
|
1922 | - } |
|
1485 | + if ($share->getShareType() === IShare::TYPE_LINK && |
|
1486 | + !$this->shareApiLinkAllowPublicUpload()) { |
|
1487 | + $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); |
|
1488 | + } |
|
1489 | + |
|
1490 | + return $share; |
|
1491 | + } |
|
1492 | + |
|
1493 | + protected function checkExpireDate($share) { |
|
1494 | + if ($share->isExpired()) { |
|
1495 | + $this->deleteShare($share); |
|
1496 | + throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); |
|
1497 | + } |
|
1498 | + } |
|
1499 | + |
|
1500 | + /** |
|
1501 | + * Verify the password of a public share |
|
1502 | + * |
|
1503 | + * @param IShare $share |
|
1504 | + * @param string $password |
|
1505 | + * @return bool |
|
1506 | + */ |
|
1507 | + public function checkPassword(IShare $share, $password) { |
|
1508 | + $passwordProtected = $share->getShareType() !== IShare::TYPE_LINK |
|
1509 | + || $share->getShareType() !== IShare::TYPE_EMAIL |
|
1510 | + || $share->getShareType() !== IShare::TYPE_CIRCLE; |
|
1511 | + if (!$passwordProtected) { |
|
1512 | + //TODO maybe exception? |
|
1513 | + return false; |
|
1514 | + } |
|
1515 | + |
|
1516 | + if ($password === null || $share->getPassword() === null) { |
|
1517 | + return false; |
|
1518 | + } |
|
1519 | + |
|
1520 | + $newHash = ''; |
|
1521 | + if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { |
|
1522 | + return false; |
|
1523 | + } |
|
1524 | + |
|
1525 | + if (!empty($newHash)) { |
|
1526 | + $share->setPassword($newHash); |
|
1527 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
1528 | + $provider->update($share); |
|
1529 | + } |
|
1530 | + |
|
1531 | + return true; |
|
1532 | + } |
|
1533 | + |
|
1534 | + /** |
|
1535 | + * @inheritdoc |
|
1536 | + */ |
|
1537 | + public function userDeleted($uid) { |
|
1538 | + $types = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_LINK, IShare::TYPE_REMOTE, IShare::TYPE_EMAIL]; |
|
1539 | + |
|
1540 | + foreach ($types as $type) { |
|
1541 | + try { |
|
1542 | + $provider = $this->factory->getProviderForType($type); |
|
1543 | + } catch (ProviderException $e) { |
|
1544 | + continue; |
|
1545 | + } |
|
1546 | + $provider->userDeleted($uid, $type); |
|
1547 | + } |
|
1548 | + } |
|
1549 | + |
|
1550 | + /** |
|
1551 | + * @inheritdoc |
|
1552 | + */ |
|
1553 | + public function groupDeleted($gid) { |
|
1554 | + $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP); |
|
1555 | + $provider->groupDeleted($gid); |
|
1556 | + |
|
1557 | + $excludedGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
1558 | + if ($excludedGroups === '') { |
|
1559 | + return; |
|
1560 | + } |
|
1561 | + |
|
1562 | + $excludedGroups = json_decode($excludedGroups, true); |
|
1563 | + if (json_last_error() !== JSON_ERROR_NONE) { |
|
1564 | + return; |
|
1565 | + } |
|
1566 | + |
|
1567 | + $excludedGroups = array_diff($excludedGroups, [$gid]); |
|
1568 | + $this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode($excludedGroups)); |
|
1569 | + } |
|
1570 | + |
|
1571 | + /** |
|
1572 | + * @inheritdoc |
|
1573 | + */ |
|
1574 | + public function userDeletedFromGroup($uid, $gid) { |
|
1575 | + $provider = $this->factory->getProviderForType(IShare::TYPE_GROUP); |
|
1576 | + $provider->userDeletedFromGroup($uid, $gid); |
|
1577 | + } |
|
1578 | + |
|
1579 | + /** |
|
1580 | + * Get access list to a path. This means |
|
1581 | + * all the users that can access a given path. |
|
1582 | + * |
|
1583 | + * Consider: |
|
1584 | + * -root |
|
1585 | + * |-folder1 (23) |
|
1586 | + * |-folder2 (32) |
|
1587 | + * |-fileA (42) |
|
1588 | + * |
|
1589 | + * fileA is shared with user1 and user1@server1 |
|
1590 | + * folder2 is shared with group2 (user4 is a member of group2) |
|
1591 | + * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 |
|
1592 | + * |
|
1593 | + * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: |
|
1594 | + * [ |
|
1595 | + * users => [ |
|
1596 | + * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], |
|
1597 | + * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], |
|
1598 | + * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], |
|
1599 | + * ], |
|
1600 | + * remote => [ |
|
1601 | + * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], |
|
1602 | + * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], |
|
1603 | + * ], |
|
1604 | + * public => bool |
|
1605 | + * mail => bool |
|
1606 | + * ] |
|
1607 | + * |
|
1608 | + * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: |
|
1609 | + * [ |
|
1610 | + * users => ['user1', 'user2', 'user4'], |
|
1611 | + * remote => bool, |
|
1612 | + * public => bool |
|
1613 | + * mail => bool |
|
1614 | + * ] |
|
1615 | + * |
|
1616 | + * This is required for encryption/activity |
|
1617 | + * |
|
1618 | + * @param \OCP\Files\Node $path |
|
1619 | + * @param bool $recursive Should we check all parent folders as well |
|
1620 | + * @param bool $currentAccess Ensure the recipient has access to the file (e.g. did not unshare it) |
|
1621 | + * @return array |
|
1622 | + */ |
|
1623 | + public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { |
|
1624 | + $owner = $path->getOwner(); |
|
1625 | + |
|
1626 | + if ($owner === null) { |
|
1627 | + return []; |
|
1628 | + } |
|
1629 | + |
|
1630 | + $owner = $owner->getUID(); |
|
1631 | + |
|
1632 | + if ($currentAccess) { |
|
1633 | + $al = ['users' => [], 'remote' => [], 'public' => false]; |
|
1634 | + } else { |
|
1635 | + $al = ['users' => [], 'remote' => false, 'public' => false]; |
|
1636 | + } |
|
1637 | + if (!$this->userManager->userExists($owner)) { |
|
1638 | + return $al; |
|
1639 | + } |
|
1640 | + |
|
1641 | + //Get node for the owner and correct the owner in case of external storages |
|
1642 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
1643 | + if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { |
|
1644 | + $nodes = $userFolder->getById($path->getId()); |
|
1645 | + $path = array_shift($nodes); |
|
1646 | + if ($path->getOwner() === null) { |
|
1647 | + return []; |
|
1648 | + } |
|
1649 | + $owner = $path->getOwner()->getUID(); |
|
1650 | + } |
|
1651 | + |
|
1652 | + $providers = $this->factory->getAllProviders(); |
|
1653 | + |
|
1654 | + /** @var Node[] $nodes */ |
|
1655 | + $nodes = []; |
|
1656 | + |
|
1657 | + |
|
1658 | + if ($currentAccess) { |
|
1659 | + $ownerPath = $path->getPath(); |
|
1660 | + $ownerPath = explode('/', $ownerPath, 4); |
|
1661 | + if (count($ownerPath) < 4) { |
|
1662 | + $ownerPath = ''; |
|
1663 | + } else { |
|
1664 | + $ownerPath = $ownerPath[3]; |
|
1665 | + } |
|
1666 | + $al['users'][$owner] = [ |
|
1667 | + 'node_id' => $path->getId(), |
|
1668 | + 'node_path' => '/' . $ownerPath, |
|
1669 | + ]; |
|
1670 | + } else { |
|
1671 | + $al['users'][] = $owner; |
|
1672 | + } |
|
1673 | + |
|
1674 | + // Collect all the shares |
|
1675 | + while ($path->getPath() !== $userFolder->getPath()) { |
|
1676 | + $nodes[] = $path; |
|
1677 | + if (!$recursive) { |
|
1678 | + break; |
|
1679 | + } |
|
1680 | + $path = $path->getParent(); |
|
1681 | + } |
|
1682 | + |
|
1683 | + foreach ($providers as $provider) { |
|
1684 | + $tmp = $provider->getAccessList($nodes, $currentAccess); |
|
1685 | + |
|
1686 | + foreach ($tmp as $k => $v) { |
|
1687 | + if (isset($al[$k])) { |
|
1688 | + if (is_array($al[$k])) { |
|
1689 | + if ($currentAccess) { |
|
1690 | + $al[$k] += $v; |
|
1691 | + } else { |
|
1692 | + $al[$k] = array_merge($al[$k], $v); |
|
1693 | + $al[$k] = array_unique($al[$k]); |
|
1694 | + $al[$k] = array_values($al[$k]); |
|
1695 | + } |
|
1696 | + } else { |
|
1697 | + $al[$k] = $al[$k] || $v; |
|
1698 | + } |
|
1699 | + } else { |
|
1700 | + $al[$k] = $v; |
|
1701 | + } |
|
1702 | + } |
|
1703 | + } |
|
1704 | + |
|
1705 | + return $al; |
|
1706 | + } |
|
1707 | + |
|
1708 | + /** |
|
1709 | + * Create a new share |
|
1710 | + * |
|
1711 | + * @return IShare |
|
1712 | + */ |
|
1713 | + public function newShare() { |
|
1714 | + return new \OC\Share20\Share($this->rootFolder, $this->userManager); |
|
1715 | + } |
|
1716 | + |
|
1717 | + /** |
|
1718 | + * Is the share API enabled |
|
1719 | + * |
|
1720 | + * @return bool |
|
1721 | + */ |
|
1722 | + public function shareApiEnabled() { |
|
1723 | + return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; |
|
1724 | + } |
|
1725 | + |
|
1726 | + /** |
|
1727 | + * Is public link sharing enabled |
|
1728 | + * |
|
1729 | + * @return bool |
|
1730 | + */ |
|
1731 | + public function shareApiAllowLinks() { |
|
1732 | + return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; |
|
1733 | + } |
|
1734 | + |
|
1735 | + /** |
|
1736 | + * Is password on public link requires |
|
1737 | + * |
|
1738 | + * @return bool |
|
1739 | + */ |
|
1740 | + public function shareApiLinkEnforcePassword() { |
|
1741 | + return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; |
|
1742 | + } |
|
1743 | + |
|
1744 | + /** |
|
1745 | + * Is default link expire date enabled |
|
1746 | + * |
|
1747 | + * @return bool |
|
1748 | + */ |
|
1749 | + public function shareApiLinkDefaultExpireDate() { |
|
1750 | + return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; |
|
1751 | + } |
|
1752 | + |
|
1753 | + /** |
|
1754 | + * Is default link expire date enforced |
|
1755 | + *` |
|
1756 | + * @return bool |
|
1757 | + */ |
|
1758 | + public function shareApiLinkDefaultExpireDateEnforced() { |
|
1759 | + return $this->shareApiLinkDefaultExpireDate() && |
|
1760 | + $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; |
|
1761 | + } |
|
1762 | + |
|
1763 | + |
|
1764 | + /** |
|
1765 | + * Number of default link expire days |
|
1766 | + * @return int |
|
1767 | + */ |
|
1768 | + public function shareApiLinkDefaultExpireDays() { |
|
1769 | + return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
1770 | + } |
|
1771 | + |
|
1772 | + /** |
|
1773 | + * Is default internal expire date enabled |
|
1774 | + * |
|
1775 | + * @return bool |
|
1776 | + */ |
|
1777 | + public function shareApiInternalDefaultExpireDate(): bool { |
|
1778 | + return $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no') === 'yes'; |
|
1779 | + } |
|
1780 | + |
|
1781 | + /** |
|
1782 | + * Is default expire date enforced |
|
1783 | + *` |
|
1784 | + * @return bool |
|
1785 | + */ |
|
1786 | + public function shareApiInternalDefaultExpireDateEnforced(): bool { |
|
1787 | + return $this->shareApiInternalDefaultExpireDate() && |
|
1788 | + $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes'; |
|
1789 | + } |
|
1790 | + |
|
1791 | + |
|
1792 | + /** |
|
1793 | + * Number of default expire days |
|
1794 | + * @return int |
|
1795 | + */ |
|
1796 | + public function shareApiInternalDefaultExpireDays(): int { |
|
1797 | + return (int)$this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7'); |
|
1798 | + } |
|
1799 | + |
|
1800 | + /** |
|
1801 | + * Allow public upload on link shares |
|
1802 | + * |
|
1803 | + * @return bool |
|
1804 | + */ |
|
1805 | + public function shareApiLinkAllowPublicUpload() { |
|
1806 | + return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; |
|
1807 | + } |
|
1808 | + |
|
1809 | + /** |
|
1810 | + * check if user can only share with group members |
|
1811 | + * @return bool |
|
1812 | + */ |
|
1813 | + public function shareWithGroupMembersOnly() { |
|
1814 | + return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
|
1815 | + } |
|
1816 | + |
|
1817 | + /** |
|
1818 | + * Check if users can share with groups |
|
1819 | + * @return bool |
|
1820 | + */ |
|
1821 | + public function allowGroupSharing() { |
|
1822 | + return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; |
|
1823 | + } |
|
1824 | + |
|
1825 | + public function allowEnumeration(): bool { |
|
1826 | + return $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
|
1827 | + } |
|
1828 | + |
|
1829 | + public function limitEnumerationToGroups(): bool { |
|
1830 | + return $this->allowEnumeration() && |
|
1831 | + $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes'; |
|
1832 | + } |
|
1833 | + |
|
1834 | + public function limitEnumerationToPhone(): bool { |
|
1835 | + return $this->allowEnumeration() && |
|
1836 | + $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes'; |
|
1837 | + } |
|
1838 | + |
|
1839 | + public function allowEnumerationFullMatch(): bool { |
|
1840 | + return $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes'; |
|
1841 | + } |
|
1842 | + |
|
1843 | + /** |
|
1844 | + * Copied from \OC_Util::isSharingDisabledForUser |
|
1845 | + * |
|
1846 | + * TODO: Deprecate fuction from OC_Util |
|
1847 | + * |
|
1848 | + * @param string $userId |
|
1849 | + * @return bool |
|
1850 | + */ |
|
1851 | + public function sharingDisabledForUser($userId) { |
|
1852 | + if ($userId === null) { |
|
1853 | + return false; |
|
1854 | + } |
|
1855 | + |
|
1856 | + if (isset($this->sharingDisabledForUsersCache[$userId])) { |
|
1857 | + return $this->sharingDisabledForUsersCache[$userId]; |
|
1858 | + } |
|
1859 | + |
|
1860 | + if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { |
|
1861 | + $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); |
|
1862 | + $excludedGroups = json_decode($groupsList); |
|
1863 | + if (is_null($excludedGroups)) { |
|
1864 | + $excludedGroups = explode(',', $groupsList); |
|
1865 | + $newValue = json_encode($excludedGroups); |
|
1866 | + $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); |
|
1867 | + } |
|
1868 | + $user = $this->userManager->get($userId); |
|
1869 | + $usersGroups = $this->groupManager->getUserGroupIds($user); |
|
1870 | + if (!empty($usersGroups)) { |
|
1871 | + $remainingGroups = array_diff($usersGroups, $excludedGroups); |
|
1872 | + // if the user is only in groups which are disabled for sharing then |
|
1873 | + // sharing is also disabled for the user |
|
1874 | + if (empty($remainingGroups)) { |
|
1875 | + $this->sharingDisabledForUsersCache[$userId] = true; |
|
1876 | + return true; |
|
1877 | + } |
|
1878 | + } |
|
1879 | + } |
|
1880 | + |
|
1881 | + $this->sharingDisabledForUsersCache[$userId] = false; |
|
1882 | + return false; |
|
1883 | + } |
|
1884 | + |
|
1885 | + /** |
|
1886 | + * @inheritdoc |
|
1887 | + */ |
|
1888 | + public function outgoingServer2ServerSharesAllowed() { |
|
1889 | + return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; |
|
1890 | + } |
|
1891 | + |
|
1892 | + /** |
|
1893 | + * @inheritdoc |
|
1894 | + */ |
|
1895 | + public function outgoingServer2ServerGroupSharesAllowed() { |
|
1896 | + return $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no') === 'yes'; |
|
1897 | + } |
|
1898 | + |
|
1899 | + /** |
|
1900 | + * @inheritdoc |
|
1901 | + */ |
|
1902 | + public function shareProviderExists($shareType) { |
|
1903 | + try { |
|
1904 | + $this->factory->getProviderForType($shareType); |
|
1905 | + } catch (ProviderException $e) { |
|
1906 | + return false; |
|
1907 | + } |
|
1908 | + |
|
1909 | + return true; |
|
1910 | + } |
|
1911 | + |
|
1912 | + public function registerShareProvider(string $shareProviderClass): void { |
|
1913 | + $this->factory->registerProvider($shareProviderClass); |
|
1914 | + } |
|
1915 | + |
|
1916 | + public function getAllShares(): iterable { |
|
1917 | + $providers = $this->factory->getAllProviders(); |
|
1918 | + |
|
1919 | + foreach ($providers as $provider) { |
|
1920 | + yield from $provider->getAllShares(); |
|
1921 | + } |
|
1922 | + } |
|
1923 | 1923 | } |