Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow view file when owner object provides this #6668

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ HumHub Changelog
- Fix #6631: Fix visibility of the method `Controller::getAccessRules()`
- Enh #6650: Add assets GZIP compression with Apache
- Fix #6662: Change the start_url of the PWA from home to base URL
- Enh #6667: Allow view file when owner object provides this
20 changes: 16 additions & 4 deletions protected/humhub/interfaces/ArchiveableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,31 @@

use humhub\modules\user\models\User;

/**
* Deletable Interface
* @since 1.16
*/
interface ArchiveableInterface
{
/**
* Checks if the given user can edit/create this element.
*
* @param User|integer $user user instance or user id
*
* @return bool can edit/create this element
* @since 1.15
* @param User|integer|string|null $user user instance or user id
* @return bool
*/
public function canArchive($user = null): bool;

/**
* Archive this object
*
* @return bool
*/
public function archive(): bool;

/**
* Unarchive this object
*
* @return bool
*/
public function unarchive(): bool;
}
14 changes: 13 additions & 1 deletion protected/humhub/interfaces/DeletableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@

namespace humhub\interfaces;

use humhub\modules\user\models\User;

/**
* Deletable Interface
* @since 1.16
*/
interface DeletableInterface
{
/**
* Checks if given item can be deleted.
*
* @param User|integer|string|null $user user instance or user id
* @return bool
*/
public function canDelete($userId = null);
public function canDelete($user = null): bool;

/**
* Delete this object
*/
public function delete();
}
11 changes: 6 additions & 5 deletions protected/humhub/interfaces/EditableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
namespace humhub\interfaces;

use humhub\modules\user\models\User;
use Throwable;

/**
* Editable Interface
* @since 1.16
*/
interface EditableInterface
{

/**
* Checks if the given user can edit/create this element.
*
* @param User|integer $user user instance or user id
*
* @return bool can edit/create this element
* @since 1.15
* @param User|integer|string|null $user user instance or user id
* @return bool
*/
public function canEdit($user = null): bool;

Expand Down
4 changes: 4 additions & 0 deletions protected/humhub/interfaces/MailerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace humhub\interfaces;

/**
* Mailer Interface
* @since 1.16
*/
interface MailerInterface extends \yii\mail\MailerInterface
{
}
10 changes: 6 additions & 4 deletions protected/humhub/interfaces/ReadableInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
Expand All @@ -10,14 +9,17 @@

use humhub\modules\user\models\User;

/**
* Readable Interface
* @since 1.16
*/
interface ReadableInterface
{
/**
* Checks if given element can be read.
*
* @param string|User $userId
*
* @param User|integer|string|null $user User instance or user id, null - current user
* @return bool
*/
public function canRead($userId = ""): bool;
public function canRead($user = null): bool;
}
14 changes: 7 additions & 7 deletions protected/humhub/interfaces/ViewableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
namespace humhub\interfaces;

use humhub\modules\user\models\User;
use Throwable;

/**
* Viewable Interface
* @since 1.16
*/
interface ViewableInterface
{
/**
* Checks if user can view this element.
*
* @param User|integer $user
*
* @return boolean can view this element
* @throws Throwable
* @since 1.15
* @param User|integer|string|null $user User instance or user id, null - current user
* @return bool
*/
public function canView($user = null);
public function canView($user = null): bool;

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public function beforeSave($insert)
* Checks if the given / or current user can delete this content.
* Currently only the creator can remove.
*
* @param null $userId
* @return boolean
* @inheritdoc
*/
public function canDelete($userId = null): bool
{
Expand All @@ -151,14 +150,11 @@ public function canDelete($userId = null): bool
}

/**
* Check if current user can read this object
*
* @param string $userId
* @return boolean
* @inheritdoc
*/
public function canRead($userId = ""): bool
public function canRead($user = null): bool
{
return $this->content->canView($userId);
return $this->content->canView($user);
}

/**
Expand Down
20 changes: 4 additions & 16 deletions protected/humhub/modules/content/models/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,7 @@ public function isArchived(): bool
* Checks if the current user can archive this content.
* The content owner and the workspace admin can archive contents.
*
* @return boolean
* @throws Exception
* @throws \yii\base\InvalidConfigException
* @inheritdoc
*/
public function canArchive($user = null): bool
{
Expand Down Expand Up @@ -599,9 +597,7 @@ public function canArchive($user = null): bool
}

/**
* Archives the content object
*
* @return bool
* @inheritdoc
*/
public function archive(): bool
{
Expand Down Expand Up @@ -735,9 +731,7 @@ public function afterMove(ContentContainerActiveRecord $container = null)
}

/**
* Unarchives the content object
*
* @return bool
* @inheritdoc
*/
public function unarchive(): bool
{
Expand Down Expand Up @@ -970,13 +964,7 @@ public function can($permission, $params = [], $allowCaching = true)
}

/**
* Checks if user can view this content.
*
* @param User|integer $user
* @return boolean can view this content
* @throws Exception
* @throws Throwable
* @since 1.1
* @inheritdoc
*/
public function canView($user = null): bool
{
Expand Down
25 changes: 10 additions & 15 deletions protected/humhub/modules/file/models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
use humhub\components\ActiveRecord;
use humhub\components\behaviors\GUID;
use humhub\components\behaviors\PolymorphicRelation;
use humhub\interfaces\ViewableInterface;
use humhub\libs\StdClass;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord;
use humhub\modules\file\components\StorageManager;
use humhub\modules\file\components\StorageManagerInterface;
use humhub\modules\file\libs\Metadata;
use humhub\modules\user\models\User;
use Throwable;
use Yii;
use yii\base\InvalidArgumentException;
Expand All @@ -28,7 +28,6 @@
use yii\db\StaleObjectException;
use yii\helpers\Url;
use yii\web\UploadedFile;
use humhub\interfaces\ViewableInterface;

/**
* This is the model class for table "file".
Expand Down Expand Up @@ -282,28 +281,24 @@ public function getHash($length = 0)
}

/**
* Checks if given file can read.
*
* If the file is not an instance of HActiveRecordContent or HActiveRecordContentAddon
* the file is readable for all.
*
* @param string|User $userId
*
* @return bool
* @throws IntegrityException
* @throws Throwable
* @throws \yii\base\Exception
* @inheritdoc
*/
public function canRead($userId = ""): bool
public function canRead($user = null): bool
{
$object = $this->getPolymorphicRelation();
if ($object instanceof ContentActiveRecord || $object instanceof ContentAddonActiveRecord) {
return $object->content->canView($userId);
return $object->content->canView($user);
}
if ($object instanceof ViewableInterface) {
return $object->canView($user);
}

return true;
}

/**
* @inheritdoc
*/
public function canView($user = null): bool
{
return $this->canRead($user);
Expand Down
Loading