Update CommentController.php (#5940)

It would be great if we could have the `parent::beforeAction($action)` before throwing exceptions.
This would allow to run the `Controller::EVENT_BEFORE_ACTION` before.
This is the way it's done in https://github.com/humhub/humhub/blob/master/protected/humhub/components/Controller.php#L206 and `yii\web\Controller` 
Thanks!
This commit is contained in:
Marc Farré 2022-11-24 17:54:57 +01:00 committed by GitHub
parent 605e9dbe5c
commit f10581f5ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -62,21 +62,25 @@ class CommentController extends Controller
*/
public function beforeAction($action)
{
$modelClass = Yii::$app->request->get('objectModel', Yii::$app->request->post('objectModel'));
$modelPk = (int)Yii::$app->request->get('objectId', Yii::$app->request->post('objectId'));
if (parent::beforeAction($action)) {
$modelClass = Yii::$app->request->get('objectModel', Yii::$app->request->post('objectModel'));
$modelPk = (int)Yii::$app->request->get('objectId', Yii::$app->request->post('objectId'));
Helpers::CheckClassType($modelClass, [Comment::class, ContentActiveRecord::class]);
$this->target = $modelClass::findOne(['id' => $modelPk]);
Helpers::CheckClassType($modelClass, [Comment::class, ContentActiveRecord::class]);
$this->target = $modelClass::findOne(['id' => $modelPk]);
if (!$this->target) {
throw new NotFoundHttpException('Could not find underlying content or content addon record!');
if (!$this->target) {
throw new NotFoundHttpException('Could not find underlying content or content addon record!');
}
if (!$this->target->content->canView()) {
throw new ForbiddenHttpException();
}
return true;
}
if (!$this->target->content->canView()) {
throw new ForbiddenHttpException();
}
return parent::beforeAction($action);
return false;
}