diff --git a/CHANGELOG.md b/CHANGELOG.md index 26b478ead1..1a26d18518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ HumHub Changelog - Enh #6734: Trim Base URL on save - Fix #6708: Fix displaying of alert when RichText is changed on refresh a page - Fix #6747: Fix loading of default container permissions from cache +- Fix #6766: Since Humhub 1.15.1, getting content polymorphic relation changes the content object_model to the wrong class 1.15.1 (December 13, 2023) ------------------------- diff --git a/protected/humhub/components/behaviors/PolymorphicRelation.php b/protected/humhub/components/behaviors/PolymorphicRelation.php index e0be6771e3..3fe61819de 100644 --- a/protected/humhub/components/behaviors/PolymorphicRelation.php +++ b/protected/humhub/components/behaviors/PolymorphicRelation.php @@ -101,8 +101,8 @@ class PolymorphicRelation extends Behavior $this->cached = $object; if ($object instanceof ActiveRecord) { - $class = get_class($object); - if ($cached === null || get_class($cached) !== $class) { + $class = self::getObjectModel($object); + if ($cached === null || self::getObjectModel($cached) !== $class) { $this->owner->setAttribute($this->classAttribute, $class); } diff --git a/protected/humhub/modules/comment/widgets/Comments.php b/protected/humhub/modules/comment/widgets/Comments.php index 1c09924998..137210ea76 100644 --- a/protected/humhub/modules/comment/widgets/Comments.php +++ b/protected/humhub/modules/comment/widgets/Comments.php @@ -2,10 +2,11 @@ namespace humhub\modules\comment\widgets; +use humhub\components\behaviors\PolymorphicRelation; +use humhub\components\Widget; use humhub\modules\comment\models\Comment as CommentModel; use humhub\modules\comment\Module; use humhub\modules\content\components\ContentActiveRecord; -use humhub\components\Widget; use humhub\modules\content\widgets\stream\StreamEntryOptions; use humhub\modules\content\widgets\stream\WallStreamEntryOptions; use Yii; @@ -62,7 +63,7 @@ class Comments extends Widget */ public function run() { - $objectModel = get_class($this->object); + $objectModel = PolymorphicRelation::getObjectModel($this->object); $objectId = $this->object->getPrimaryKey(); $streamQuery = Yii::$app->request->getQueryParam('StreamQuery'); diff --git a/protected/humhub/modules/comment/widgets/Form.php b/protected/humhub/modules/comment/widgets/Form.php index 888d959841..faad027ec4 100644 --- a/protected/humhub/modules/comment/widgets/Form.php +++ b/protected/humhub/modules/comment/widgets/Form.php @@ -8,9 +8,10 @@ namespace humhub\modules\comment\widgets; +use humhub\components\behaviors\PolymorphicRelation; use humhub\components\Widget; -use humhub\modules\comment\Module; use humhub\modules\comment\models\Comment as CommentModel; +use humhub\modules\comment\Module; use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\file\handler\FileHandlerCollection; use Yii; @@ -81,7 +82,7 @@ class Form extends Widget } return $this->render('form', [ - 'objectModel' => get_class($this->object), + 'objectModel' => PolymorphicRelation::getObjectModel($this->object), 'objectId' => $this->object->getPrimaryKey(), 'id' => $this->object->getUniqueId(), 'model' => $this->model, diff --git a/protected/humhub/modules/comment/widgets/ShowMore.php b/protected/humhub/modules/comment/widgets/ShowMore.php index ced1c059ef..4395745a28 100644 --- a/protected/humhub/modules/comment/widgets/ShowMore.php +++ b/protected/humhub/modules/comment/widgets/ShowMore.php @@ -2,6 +2,7 @@ namespace humhub\modules\comment\widgets; +use humhub\components\behaviors\PolymorphicRelation; use humhub\modules\comment\models\Comment; use Yii; use yii\base\Widget; @@ -57,7 +58,7 @@ class ShowMore extends Widget return $this->render('showMore', [ 'text' => $this->getText(), 'showMoreUrl' => Url::to(['/comment/comment/show', - 'objectModel' => get_class($this->object), + 'objectModel' => PolymorphicRelation::getObjectModel($this->object), 'objectId' => $this->object->getPrimaryKey(), 'pageSize' => $this->pageSize, 'commentId' => $this->commentId, diff --git a/protected/humhub/modules/content/components/ContentAddonController.php b/protected/humhub/modules/content/components/ContentAddonController.php index 9850f64875..30fb38f28e 100644 --- a/protected/humhub/modules/content/components/ContentAddonController.php +++ b/protected/humhub/modules/content/components/ContentAddonController.php @@ -8,11 +8,12 @@ namespace humhub\modules\content\components; -use Yii; +use humhub\components\behaviors\PolymorphicRelation; use humhub\components\Controller; -use yii\web\HttpException; -use yii\base\Exception; use humhub\libs\Helpers; +use Yii; +use yii\base\Exception; +use yii\web\HttpException; /** @@ -73,12 +74,12 @@ class ContentAddonController extends Controller { $modelClass = Yii::$app->request->get('contentModel'); - $pk = (int) Yii::$app->request->get('contentId'); + $pk = (int)Yii::$app->request->get('contentId'); // Fixme if ($modelClass == '') { $modelClass = Yii::$app->request->post('contentModel'); - $pk = (int) Yii::$app->request->post('contentId'); + $pk = (int)Yii::$app->request->post('contentId'); } @@ -105,7 +106,7 @@ class ContentAddonController extends Controller throw new HttpException(403, 'Access denied!'); } - $this->contentModel = get_class($target); + $this->contentModel = PolymorphicRelation::getObjectModel($target); $this->contentId = $target->getPrimaryKey(); return parent::beforeAction($action); @@ -132,7 +133,7 @@ class ContentAddonController extends Controller throw new HttpException(500, 'Could not find content addon record!'); } - if ($target->object_model != get_class($this->parentContent) && $target->object_id != $this->parentContent->getPrimaryKey()) { + if ($target->object_model !== PolymorphicRelation::getObjectModel($this->parentContent) && $target->object_id !== $this->parentContent->getPrimaryKey()) { throw new HttpException(500, 'Content addon not belongs to given content record!'); } diff --git a/protected/humhub/modules/content/models/Content.php b/protected/humhub/modules/content/models/Content.php index 7c183ade28..94b28a9802 100644 --- a/protected/humhub/modules/content/models/Content.php +++ b/protected/humhub/modules/content/models/Content.php @@ -12,7 +12,6 @@ use humhub\components\ActiveRecord; use humhub\components\behaviors\GUID; use humhub\components\behaviors\PolymorphicRelation; use humhub\components\Module; -use humhub\modules\activity\models\Activity; use humhub\modules\admin\permissions\ManageUsers; use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\content\components\ContentContainerActiveRecord; @@ -28,7 +27,6 @@ use humhub\modules\content\permissions\ManageContent; use humhub\modules\content\services\ContentStateService; use humhub\modules\content\services\ContentTagService; use humhub\modules\notification\models\Notification; -use humhub\modules\post\models\Post; use humhub\modules\search\libs\SearchHelper; use humhub\modules\space\models\Space; use humhub\modules\user\components\PermissionManager; @@ -36,7 +34,6 @@ use humhub\modules\user\helpers\AuthHelper; use humhub\modules\user\models\User; use Yii; use yii\base\Exception; -use yii\base\InvalidArgumentException; use yii\db\IntegrityException; use yii\helpers\Url; @@ -261,7 +258,7 @@ class Content extends ActiveRecord implements Movable, ContentOwner, SoftDeletab $model->afterStateChange($this->state, $previousState); } - if (!$this->getStateService()->wasPublished() && (int) $previousState === self::STATE_PUBLISHED) { + if (!$this->getStateService()->wasPublished() && (int)$previousState === self::STATE_PUBLISHED) { $this->updateAttributes(['was_published' => 1]); } } @@ -308,7 +305,7 @@ class Content extends ActiveRecord implements Movable, ContentOwner, SoftDeletab 'originator' => $this->createdBy->guid, 'contentContainerId' => $this->container->contentContainerRecord->id, 'visibility' => $this->visibility, - 'sourceClass' => get_class($record), + 'sourceClass' => PolymorphicRelation::getObjectModel($record), 'sourceId' => $record->getPrimaryKey(), 'silent' => $this->isMuted(), 'streamChannel' => $this->stream_channel, @@ -406,7 +403,7 @@ class Content extends ActiveRecord implements Movable, ContentOwner, SoftDeletab } Notification::deleteAll([ - 'source_class' => get_class($this), + 'source_class' => PolymorphicRelation::getObjectModel($this), 'source_pk' => $this->getPrimaryKey(), ]); diff --git a/protected/humhub/modules/content/tests/codeception/functional/RichTextToEmailHtmlConverterCest.php b/protected/humhub/modules/content/tests/codeception/functional/RichTextToEmailHtmlConverterCest.php index d48b9ac6c3..1da975cbdc 100644 --- a/protected/humhub/modules/content/tests/codeception/functional/RichTextToEmailHtmlConverterCest.php +++ b/protected/humhub/modules/content/tests/codeception/functional/RichTextToEmailHtmlConverterCest.php @@ -8,6 +8,7 @@ namespace content\functional; use content\FunctionalTester; +use humhub\components\behaviors\PolymorphicRelation; use humhub\modules\comment\models\forms\CommentForm; use humhub\modules\file\models\File; use humhub\modules\post\models\Post; @@ -78,7 +79,7 @@ class RichTextToEmailHtmlConverterCest $post = Post::findOne(['id' => 2]); $commentForm = new CommentForm($post); $commentForm->load([ - 'objectModel' => get_class($post), + 'objectModel' => PolymorphicRelation::getObjectModel($post), 'objectId' => $post->id, 'Comment' => ['message' => 'Test comment with image ![' . $file->file_name . '](file-guid:' . $file->guid . ' "' . $file->title . '") ' . diff --git a/protected/humhub/modules/content/widgets/WallEntry.php b/protected/humhub/modules/content/widgets/WallEntry.php index c653f2ec58..53008d2bfd 100644 --- a/protected/humhub/modules/content/widgets/WallEntry.php +++ b/protected/humhub/modules/content/widgets/WallEntry.php @@ -8,18 +8,16 @@ namespace humhub\modules\content\widgets; -use humhub\modules\content\widgets\stream\StreamEntryWidget; +use humhub\components\Widget; +use humhub\modules\content\components\ContentContainerController; use humhub\modules\content\widgets\stream\StreamEntryOptions; use humhub\modules\dashboard\controllers\DashboardController; -use humhub\modules\stream\actions\Stream; +use humhub\modules\space\models\Space; use humhub\modules\ui\menu\DropdownDivider; use humhub\modules\ui\menu\MenuEntry; -use Yii; use humhub\modules\user\controllers\ProfileController; -use humhub\components\Widget; -use humhub\modules\space\models\Space; use humhub\modules\user\models\User; -use humhub\modules\content\components\ContentContainerController; +use Yii; /** * WallEntry is responsible to show a content inside a stream/wall. diff --git a/protected/humhub/modules/file/components/FileManager.php b/protected/humhub/modules/file/components/FileManager.php index 251b85b941..a102521862 100644 --- a/protected/humhub/modules/file/components/FileManager.php +++ b/protected/humhub/modules/file/components/FileManager.php @@ -8,12 +8,13 @@ namespace humhub\modules\file\components; +use humhub\components\behaviors\PolymorphicRelation; use humhub\modules\comment\models\Comment; use humhub\modules\content\components\ContentActiveRecord; +use humhub\modules\file\models\File; use humhub\modules\search\libs\SearchHelper; use Yii; use yii\base\Component; -use humhub\modules\file\models\File; use yii\helpers\ArrayHelper; /** @@ -69,7 +70,7 @@ class FileManager extends Component } $attributes = [ - 'object_model' => get_class($this->record), + 'object_model' => PolymorphicRelation::getObjectModel($this->record), 'object_id' => $this->record->getPrimaryKey(), ]; @@ -90,7 +91,7 @@ class FileManager extends Component */ public function find() { - return File::find()->andWhere(['object_id' => $this->record->getPrimaryKey(), 'object_model' => get_class($this->record)]); + return File::find()->andWhere(['object_id' => $this->record->getPrimaryKey(), 'object_model' => PolymorphicRelation::getObjectModel($this->record)]); } /** diff --git a/protected/humhub/modules/file/models/File.php b/protected/humhub/modules/file/models/File.php index 818df03413..b9e991a1f5 100644 --- a/protected/humhub/modules/file/models/File.php +++ b/protected/humhub/modules/file/models/File.php @@ -350,7 +350,7 @@ class File extends FileCompat */ public function isAssignedTo(ActiveRecord $record) { - return $this->object_model === get_class($record) && $this->object_id == $record->getPrimaryKey(); + return $this->object_model === PolymorphicRelation::getObjectModel($record) && $this->object_id == $record->getPrimaryKey(); } /** diff --git a/protected/humhub/modules/like/widgets/LikeLink.php b/protected/humhub/modules/like/widgets/LikeLink.php index 71b547562b..10c2aad115 100644 --- a/protected/humhub/modules/like/widgets/LikeLink.php +++ b/protected/humhub/modules/like/widgets/LikeLink.php @@ -2,6 +2,7 @@ namespace humhub\modules\like\widgets; +use humhub\components\behaviors\PolymorphicRelation; use humhub\modules\content\components\ContentActiveRecord; use humhub\modules\like\models\Like as LikeModel; use humhub\modules\like\Module; @@ -50,7 +51,7 @@ class LikeLink extends \yii\base\Widget $module = Yii::$app->getModule('like'); $canLike = $module->canLike($this->object); - $likes = LikeModel::GetLikes(get_class($this->object), $this->object->id); + $likes = LikeModel::GetLikes(PolymorphicRelation::getObjectModel($this->object), $this->object->id); foreach ($likes as $like) { if ($like->user->id == Yii::$app->user->id) { $currentUserLiked = true; @@ -58,15 +59,15 @@ class LikeLink extends \yii\base\Widget } return $this->render('likeLink', [ - 'canLike' => $canLike, - 'object' => $this->object, - 'likes' => $likes, - 'currentUserLiked' => $currentUserLiked, - 'id' => $this->object->getUniqueId(), - 'likeUrl' => Url::to(['/like/like/like', 'contentModel' => get_class($this->object), 'contentId' => $this->object->id]), - 'unlikeUrl' => Url::to(['/like/like/unlike', 'contentModel' => get_class($this->object), 'contentId' => $this->object->id]), - 'userListUrl' => Url::to(['/like/like/user-list', 'contentModel' => get_class($this->object), 'contentId' => $this->object->getPrimaryKey()]), - 'title' => $this->generateLikeTitleText($currentUserLiked, $likes) + 'canLike' => $canLike, + 'object' => $this->object, + 'likes' => $likes, + 'currentUserLiked' => $currentUserLiked, + 'id' => $this->object->getUniqueId(), + 'likeUrl' => Url::to(['/like/like/like', 'contentModel' => PolymorphicRelation::getObjectModel($this->object), 'contentId' => $this->object->id]), + 'unlikeUrl' => Url::to(['/like/like/unlike', 'contentModel' => PolymorphicRelation::getObjectModel($this->object), 'contentId' => $this->object->id]), + 'userListUrl' => Url::to(['/like/like/user-list', 'contentModel' => PolymorphicRelation::getObjectModel($this->object), 'contentId' => $this->object->getPrimaryKey()]), + 'title' => $this->generateLikeTitleText($currentUserLiked, $likes) ]); } @@ -84,7 +85,7 @@ class LikeLink extends \yii\base\Widget return Yii::t('LikeModule.base', 'You like this.'); } else { // output, if more users like this - $userlist .= Yii::t('LikeModule.base', 'You'). "\n"; + $userlist .= Yii::t('LikeModule.base', 'You') . "\n"; $previewUserCount++; } } @@ -102,7 +103,7 @@ class LikeLink extends \yii\base\Widget // check, if you liked if ($likes[$i]->user->guid != Yii::$app->user->guid) { // output, if an other user liked - $userlist .= Html::encode($likes[$i]->user->displayName). "\n"; + $userlist .= Html::encode($likes[$i]->user->displayName) . "\n"; $previewUserCount++; } diff --git a/protected/humhub/modules/search/libs/SearchHelper.php b/protected/humhub/modules/search/libs/SearchHelper.php index 0d02ec8b20..081f71d79a 100644 --- a/protected/humhub/modules/search/libs/SearchHelper.php +++ b/protected/humhub/modules/search/libs/SearchHelper.php @@ -8,6 +8,7 @@ namespace humhub\modules\search\libs; +use humhub\components\behaviors\PolymorphicRelation; use humhub\modules\search\interfaces\Searchable; use humhub\modules\search\jobs\DeleteDocument; use humhub\modules\search\jobs\UpdateDocument; @@ -54,7 +55,7 @@ class SearchHelper extends BaseObject $pk = $record->getPrimaryKey(); if (!empty($pk) && !is_array($pk)) { Yii::$app->queue->push(new UpdateDocument([ - 'activeRecordClass' => get_class($record), + 'activeRecordClass' => PolymorphicRelation::getObjectModel($record), 'primaryKey' => $pk ])); return true; @@ -75,7 +76,7 @@ class SearchHelper extends BaseObject $pk = $record->getPrimaryKey(); if (!empty($pk) && !is_array($pk)) { Yii::$app->queue->push(new DeleteDocument([ - 'activeRecordClass' => get_class($record), + 'activeRecordClass' => PolymorphicRelation::getObjectModel($record), 'primaryKey' => $pk ])); return true; diff --git a/protected/humhub/modules/user/behaviors/Followable.php b/protected/humhub/modules/user/behaviors/Followable.php index 1df6d4aec0..7cf87d0d79 100644 --- a/protected/humhub/modules/user/behaviors/Followable.php +++ b/protected/humhub/modules/user/behaviors/Followable.php @@ -43,7 +43,7 @@ class Followable extends Behavior public function getFollowRecord($userId) { $userId = ($userId instanceof User) ? $userId->id : $userId; - return Yii::$app->runtimeCache->getOrSet(__METHOD__ . $userId, function() use ($userId) { + return Yii::$app->runtimeCache->getOrSet(__METHOD__ . $userId, function () use ($userId) { return Follow::find() ->where([ 'object_model' => get_class($this->owner),