From 0c777b63af9e949ec8463b38a19e96b0e7f67971 Mon Sep 17 00:00:00 2001 From: buddh4 Date: Tue, 31 Jul 2018 14:08:18 +0200 Subject: [PATCH] Enh: Added `PolymorphicRelation::strict` to throw exceptions when accessing invalid polymorphic relations --- .../components/behaviors/PolymorphicRelation.php | 10 ++++++++++ protected/humhub/docs/CHANGELOG.md | 5 +++++ protected/humhub/modules/activity/models/Activity.php | 1 + .../modules/notification/models/Notification.php | 1 + 4 files changed, 17 insertions(+) diff --git a/protected/humhub/components/behaviors/PolymorphicRelation.php b/protected/humhub/components/behaviors/PolymorphicRelation.php index 9be400e544..1ac761eed3 100644 --- a/protected/humhub/components/behaviors/PolymorphicRelation.php +++ b/protected/humhub/components/behaviors/PolymorphicRelation.php @@ -15,6 +15,7 @@ use Yii; use yii\base\Behavior; use yii\db\ActiveRecord; use yii\db\BaseActiveRecord; +use Yii\db\IntegrityException; /** * PolymorphicRelations behavior provides simple support for polymorphic relations in ActiveRecords. @@ -34,6 +35,11 @@ class PolymorphicRelation extends Behavior */ public $pkAttribute = 'object_id'; + /** + * @var boolean if set to true an exception is thrown if `object_model` and `object_id` is set but does not exist + */ + public $strict = false; + /** * @var array the related object needs to be a "instanceof" at least one of these given classnames */ @@ -60,6 +66,10 @@ class PolymorphicRelation extends Behavior $this->owner->getAttribute($this->pkAttribute) ); + if($this->strict && !$object && !empty($this->classAttribute) && !empty($this->pkAttribute)) { + throw new IntegrityException('Call to an inconsistent polymorphic relation detected on '.get_class($this->owner).' ('.$this->classAttribute.' : '.$this->pkAttribute.')'); + } + if ($object !== null && $this->validateUnderlyingObjectType($object)) { $this->_cached = $object; return $object; diff --git a/protected/humhub/docs/CHANGELOG.md b/protected/humhub/docs/CHANGELOG.md index 1eec32d51c..146a3b035f 100644 --- a/protected/humhub/docs/CHANGELOG.md +++ b/protected/humhub/docs/CHANGELOG.md @@ -1,6 +1,11 @@ HumHub Change Log ================= +1.3.0 +----------------------------- + +- Enh: Added `PolymorphicRelation::strict` to throw exceptions when accessing invalid polymorphic relations + 1.3.0-beta.3 (July 30, 2018) ----------------------------- diff --git a/protected/humhub/modules/activity/models/Activity.php b/protected/humhub/modules/activity/models/Activity.php index 86ebd3e536..61be9b7af8 100644 --- a/protected/humhub/modules/activity/models/Activity.php +++ b/protected/humhub/modules/activity/models/Activity.php @@ -54,6 +54,7 @@ class Activity extends ContentActiveRecord return [ [ 'class' => PolymorphicRelation::class, + 'strict' => true, 'mustBeInstanceOf' => [ ActiveRecord::class, ] diff --git a/protected/humhub/modules/notification/models/Notification.php b/protected/humhub/modules/notification/models/Notification.php index a5b7d829c0..29d6cb9430 100644 --- a/protected/humhub/modules/notification/models/Notification.php +++ b/protected/humhub/modules/notification/models/Notification.php @@ -51,6 +51,7 @@ class Notification extends \humhub\components\ActiveRecord 'class' => PolymorphicRelation::class, 'classAttribute' => 'source_class', 'pkAttribute' => 'source_pk', + 'strict' => true, 'mustBeInstanceOf' => [ \yii\db\ActiveRecord::class, ],