diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4e197e4..603fdab 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,7 @@ Changelog --------------------------- - Fix #201: Fix replaced method `friendship\Module::isEnabled()` - Fix #203: Refresh name of a downloading renamed file +- Fix #195: Allow to edit and delete own files 0.16.3 - November 16, 2023 --------------------------- diff --git a/models/File.php b/models/File.php index 899e99c..59b2739 100644 --- a/models/File.php +++ b/models/File.php @@ -3,20 +3,18 @@ namespace humhub\modules\cfiles\models; use humhub\modules\cfiles\libs\FileUtils; +use humhub\modules\comment\models\Comment; use humhub\modules\content\components\ContentContainerActiveRecord; +use humhub\modules\content\models\Content; use humhub\modules\content\widgets\richtext\RichText; -use humhub\modules\file\handler\DownloadFileHandler; use humhub\modules\file\libs\FileHelper; use humhub\modules\file\models\File as BaseFile; use humhub\modules\file\models\FileUpload; use humhub\modules\search\events\SearchAddEvent; use humhub\modules\topic\models\Topic; -use Yii; use humhub\modules\user\models\User; -use humhub\modules\comment\models\Comment; -use humhub\modules\content\models\Content; +use Yii; use yii\db\ActiveQuery; -use yii\helpers\Url; use yii\web\UploadedFile; /** @@ -484,7 +482,7 @@ public function getVersionsUrl(int $versionId = 0): ?string */ public function getDeleteVersionUrl(int $versionId): ?string { - if (!$this->canEdit()) { + if (!$this->canManage()) { return null; } diff --git a/models/FileSystemItem.php b/models/FileSystemItem.php index b33e3a9..f7ad7bb 100644 --- a/models/FileSystemItem.php +++ b/models/FileSystemItem.php @@ -4,6 +4,7 @@ use humhub\modules\cfiles\Module; use humhub\modules\cfiles\permissions\ManageFiles; +use humhub\modules\cfiles\permissions\WriteAccess; use humhub\modules\content\components\ContentContainerActiveRecord; use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\content\models\Content; @@ -319,18 +320,28 @@ public static function getItemById($itemId) return null; } - public function canEdit(): bool + public function canManage(): bool { // Fixes race condition on newly created files (import vs. onlyoffice) if ($this->content->container === null && $this->content->isNewRecord) { return true; } - if ($this->content->container->permissionManager->can(new ManageFiles())) { + return $this->content->container->permissionManager->can(ManageFiles::class); + } + + public function canEdit(): bool + { + if ($this->canManage()) { return true; } - return false; + if (Yii::$app->user->isGuest || $this->isNewRecord) { + return false; + } + + return $this->content->created_by === Yii::$app->user->id && + $this->content->container->permissionManager->can(WriteAccess::class); } } diff --git a/models/Folder.php b/models/Folder.php index 1c375bf..ded1ba2 100644 --- a/models/Folder.php +++ b/models/Folder.php @@ -851,7 +851,7 @@ public function moveItem(FileSystemItem $item) return false; } - if (!$item->canEdit()) { + if (!$item->canManage()) { if ($item instanceof File) { $item->addError($item->getTitle(), Yii::t('CfilesModule.base', 'You cannot move the file "{name}"!', ['name' => $item->getTitle()])); } else { diff --git a/widgets/FileListContextMenu.php b/widgets/FileListContextMenu.php index 65b9c57..4cd846a 100644 --- a/widgets/FileListContextMenu.php +++ b/widgets/FileListContextMenu.php @@ -112,14 +112,18 @@ private function initMenuFile() $this->addMenu(Yii::t('CfilesModule.base', 'Show Post'), 'show-post', 'window-maximize', 20); $this->addMenu(Yii::t('CfilesModule.base', 'Display Url'), 'show-url', 'link', 30); - if (!$this->folder->isAllPostedFiles() && $this->isEditableRow()) { - $this->addEntry(new DropdownDivider(['sortOrder' => 35])); - $this->addMenu(Yii::t('CfilesModule.base', 'Edit'), 'edit-file', 'pencil', 40); - $this->addMenu(Yii::t('CfilesModule.base', 'Delete'), 'delete', 'trash', 50); + if (!$this->folder->isAllPostedFiles()) { + if ($this->isEditableRow()) { + $this->addEntry(new DropdownDivider(['sortOrder' => 35])); + $this->addMenu(Yii::t('CfilesModule.base', 'Edit'), 'edit-file', 'pencil', 40); + $this->addMenu(Yii::t('CfilesModule.base', 'Delete'), 'delete', 'trash', 50); + } if ($this->canWrite()) { $this->addMenu(Yii::t('CfilesModule.base', 'Move'), 'move-files', 'arrows', 60); } - $this->addMenu(Yii::t('CfilesModule.base', 'Versions'), 'versions', 'history', 70); + if ($this->isManageableRow()) { + $this->addMenu(Yii::t('CfilesModule.base', 'Versions'), 'versions', 'history', 70); + } } } @@ -134,6 +138,11 @@ private function initMenuAllPostedFiles() $this->addMenu(Yii::t('CfilesModule.base', 'Display Url'), 'show-url', 'link', 20); } + private function isManageableRow(): bool + { + return $this->row->item->canManage(); + } + private function isEditableRow(): bool { return $this->row->item->canEdit(); @@ -141,7 +150,7 @@ private function isEditableRow(): bool private function canWrite(): bool { - return $this->isEditableRow() && $this->folder->content->container->can(ManageFiles::class); + return $this->isManageableRow() && $this->folder->content->container->can(ManageFiles::class); } private function zipEnabled(): bool @@ -160,4 +169,4 @@ private function addMenu(string $label, string $action, string $icon, int $sortO ])); } -} \ No newline at end of file +}