Skip to content

Commit

Permalink
Merge pull request #205 from humhub/fix/195-rename-delete-own-files
Browse files Browse the repository at this point in the history
Allow to edit and delete own files
  • Loading branch information
luke- authored Feb 1, 2024
2 parents 9141442 + 5cdb93f commit 5aadeac
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------------
Expand Down
10 changes: 4 additions & 6 deletions models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}

Expand Down
17 changes: 14 additions & 3 deletions models/FileSystemItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}
2 changes: 1 addition & 1 deletion models/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 16 additions & 7 deletions widgets/FileListContextMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand All @@ -134,14 +138,19 @@ 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();
}

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
Expand All @@ -160,4 +169,4 @@ private function addMenu(string $label, string $action, string $icon, int $sortO
]));
}

}
}

0 comments on commit 5aadeac

Please sign in to comment.