Unify getting the share for attachments

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2022-10-31 11:55:39 +01:00
parent 39c59a3bd6
commit 1f66c66ad3

View File

@@ -138,7 +138,7 @@ class FilesAppService implements IAttachmentService, ICustomAttachmentService {
public function extendData(Attachment $attachment) { public function extendData(Attachment $attachment) {
$userFolder = $this->rootFolder->getUserFolder($this->userId); $userFolder = $this->rootFolder->getUserFolder($this->userId);
$share = $this->shareProvider->getShareById($attachment->getId()); $share = $this->getShareForAttachment($attachment);
$files = $userFolder->getById($share->getNode()->getId()); $files = $userFolder->getById($share->getNode()->getId());
if (count($files) === 0) { if (count($files) === 0) {
return $attachment; return $attachment;
@@ -161,7 +161,7 @@ class FilesAppService implements IAttachmentService, ICustomAttachmentService {
// Problem: Folders // Problem: Folders
/** @psalm-suppress InvalidCatch */ /** @psalm-suppress InvalidCatch */
try { try {
$share = $this->shareProvider->getShareById($attachment->getId()); $share = $this->getShareForAttachment($attachment);
} catch (ShareNotFound $e) { } catch (ShareNotFound $e) {
throw new NotFoundException('File not found'); throw new NotFoundException('File not found');
} }
@@ -241,7 +241,7 @@ class FilesAppService implements IAttachmentService, ICustomAttachmentService {
} }
public function update(Attachment $attachment) { public function update(Attachment $attachment) {
$share = $this->shareProvider->getShareById($attachment->getId()); $share = $this->getShareForAttachment($attachment);
$target = $share->getNode(); $target = $share->getNode();
$file = $this->getUploadedFile(); $file = $this->getUploadedFile();
$fileName = $file['name']; $fileName = $file['name'];
@@ -258,8 +258,13 @@ class FilesAppService implements IAttachmentService, ICustomAttachmentService {
return $attachment; return $attachment;
} }
/**
* @throws NoPermissionException
* @throws NotFoundException
* @throws ShareNotFound
*/
public function delete(Attachment $attachment) { public function delete(Attachment $attachment) {
$share = $this->shareProvider->getShareById($attachment->getId()); $share = $this->getShareForAttachment($attachment);
$file = $share->getNode(); $file = $share->getNode();
$attachment->setData($file->getName()); $attachment->setData($file->getName());
@@ -282,4 +287,21 @@ class FilesAppService implements IAttachmentService, ICustomAttachmentService {
public function markAsDeleted(Attachment $attachment) { public function markAsDeleted(Attachment $attachment) {
throw new \Exception('Not implemented'); throw new \Exception('Not implemented');
} }
/**
* @throws NoPermissionException
*/
private function getShareForAttachment(Attachment $attachment): IShare {
try {
$share = $this->shareProvider->getShareById($attachment->getId());
} catch (ShareNotFound $e) {
throw new NoPermissionException('No permission to access the attachment from the card');
}
if ((int)$share->getSharedWith() !== (int)$attachment->getCardId()) {
throw new NoPermissionException('No permission to access the attachment from the card');
}
return $share;
}
} }