Skip to content

Commit

Permalink
Fix visibility of the creating form for administrator
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Sep 13, 2024
1 parent afcd74c commit 190bcf2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
16 changes: 8 additions & 8 deletions controllers/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

namespace humhub\modules\polls\controllers;

use humhub\modules\polls\permissions\CreatePoll;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\models\PollAnswer;
use humhub\modules\polls\widgets\WallCreateForm;
use humhub\modules\stream\actions\Stream;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\UserListBox;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\helpers\Html;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\UserListBox;
use humhub\modules\content\components\ContentContainerController;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\models\PollAnswer;

/**
* PollController handles all poll related actions.
Expand Down Expand Up @@ -43,11 +42,12 @@ public function actionCreateForm()
*/
public function actionCreate()
{
if (!$this->contentContainer->permissionManager->can(new CreatePoll())) {
$poll = new Poll($this->contentContainer, ['scenario' => Poll::SCENARIO_CREATE]);

if (!$poll->content->canEdit()) {
throw new HttpException(400, 'Access denied!');
}

$poll = new Poll(['scenario' => Poll::SCENARIO_CREATE]);
$poll->load(Yii::$app->request->post());
return WallCreateForm::create($poll, $this->contentContainer);
}
Expand Down
9 changes: 4 additions & 5 deletions controllers/rest/PollsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use humhub\modules\polls\helpers\RestDefinitions;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\models\PollAnswerUser;
use humhub\modules\polls\permissions\CreatePoll;
use humhub\modules\rest\components\BaseContentController;
use Yii;

Expand Down Expand Up @@ -57,13 +56,13 @@ public function actionCreate($containerId)
/* @var ContentContainerActiveRecord $container */
$container = $containerRecord->getPolymorphicRelation();

if (! in_array(get_class($container), Yii::$app->getModule('polls')->getContentContainerTypes()) ||
! $container->permissionManager->can([CreatePoll::class])) {
$poll = new Poll($container, ['scenario' => Poll::SCENARIO_CREATE]);

if (!in_array(get_class($container), Yii::$app->getModule('polls')->getContentContainerTypes()) ||
!$poll->content->canEdit()) {
return $this->returnError(403, 'You are not allowed to create a poll!');
}

$poll = new Poll($container, ['scenario' => Poll::SCENARIO_CREATE]);

if ($this->savePoll($poll)) {
return $this->returnContentDefinition(Poll::findOne(['id' => $poll->id]));
}
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
1.3.6 (Unreleased)
----------------------
- Enh #137: Use PHP CS Fixer
- Fix #141: Fix visibility of the creating form for administrator

1.3.5 (June 18, 2024)
----------------------
Expand Down
3 changes: 1 addition & 2 deletions widgets/WallCreateForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use humhub\modules\content\widgets\WallCreateContentForm;
use humhub\modules\polls\models\Poll;
use humhub\modules\polls\permissions\CreatePoll;
use humhub\modules\space\models\Space;
use humhub\modules\ui\form\widgets\ActiveForm;

Expand Down Expand Up @@ -41,7 +40,7 @@ public function renderActiveForm(ActiveForm $form): string
public function run()
{
if ($this->contentContainer instanceof Space) {
if (!$this->contentContainer->permissionManager->can(new CreatePoll())) {
if (!(new Poll($this->contentContainer))->content->canEdit()) {
return '';
}
}
Expand Down

0 comments on commit 190bcf2

Please sign in to comment.