diff --git a/Events.php b/Events.php index 453d5de..a437e29 100644 --- a/Events.php +++ b/Events.php @@ -29,17 +29,17 @@ public static function onWallEntryControlsInit($event) { $object = $event->sender->object; - if(!$object instanceof Poll) { + if (!$object instanceof Poll) { return; } - if($object->content->canEdit()) { + if ($object->content->canEdit()) { $event->sender->addWidget(CloseButton::class, [ 'poll' => $object, ]); } - if($object->isResetAllowed()) { + if ($object->isResetAllowed()) { $event->sender->addWidget(ResetButton::class, [ 'poll' => $object, ]); @@ -54,7 +54,7 @@ public static function onWallEntryControlsInit($event) */ public static function onUserDelete($event) { - foreach (PollAnswerUser::findAll(array('created_by' => $event->sender->id)) as $answer) { + foreach (PollAnswerUser::findAll(['created_by' => $event->sender->id]) as $answer) { $answer->delete(); } diff --git a/controllers/PollController.php b/controllers/PollController.php index 01eb3e8..0c128a4 100644 --- a/controllers/PollController.php +++ b/controllers/PollController.php @@ -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. @@ -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); } @@ -59,7 +59,7 @@ public function actionReload($id) { $model = Poll::findOne(['id' => $id]); - if(!$model) { + if (!$model) { throw new HttpException(404); } @@ -144,7 +144,7 @@ public function actionAnswer() $answers = Yii::$app->request->post('answers'); // Build array of answer ids - $votes = array(); + $votes = []; if (is_array($answers)) { foreach ($answers as $answer_id => $flag) { $votes[] = (int) $answer_id; @@ -196,7 +196,7 @@ public function actionUserListResults() $query->andWhere(['poll_answer_user.poll_answer_id' => $answerId]); $query->orderBy('poll_answer_user.created_at DESC'); - $title = Yii::t('PollsModule.controllers_PollController', "Users voted for: {answer}", array('{answer}' => Html::encode($answer->answer))); + $title = Yii::t('PollsModule.controllers_PollController', "Users voted for: {answer}", ['{answer}' => Html::encode($answer->answer)]); return $this->renderAjaxContent(UserListBox::widget(['query' => $query, 'title' => $title])); } @@ -209,7 +209,7 @@ public function actionUserListResults() */ private function renderPollOut($question) { - $json = array(); + $json = []; $json['output'] = $this->renderAjaxContent($question->getWallOut()); return $this->asJson($json); diff --git a/controllers/rest/PollsController.php b/controllers/rest/PollsController.php index b89701a..33d06e6 100644 --- a/controllers/rest/PollsController.php +++ b/controllers/rest/PollsController.php @@ -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; @@ -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])); } @@ -184,7 +183,7 @@ public function actionVote(int $id): array $answers = Yii::$app->request->post('answers'); - $votes = array(); + $votes = []; if (is_array($answers)) { foreach ($answers as $answerId) { $votes[] = (int)$answerId; diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 99813b7..bee7527 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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) ---------------------- diff --git a/migrations/m131023_165921_initial.php b/migrations/m131023_165921_initial.php index f2dc9a7..ec0e034 100644 --- a/migrations/m131023_165921_initial.php +++ b/migrations/m131023_165921_initial.php @@ -7,7 +7,7 @@ class m131023_165921_initial extends Migration public function up() { - $this->createTable('poll', array( + $this->createTable('poll', [ 'id' => 'pk', 'question' => 'varchar(255) NOT NULL', 'allow_multiple' => 'tinyint(4) NOT NULL', @@ -15,9 +15,9 @@ public function up() 'created_by' => 'int(11) NOT NULL', 'updated_at' => 'datetime NOT NULL', 'updated_by' => 'int(11) NOT NULL', - ), ''); + ], ''); - $this->createTable('poll_answer', array( + $this->createTable('poll_answer', [ 'id' => 'pk', 'poll_id' => 'int(11) NOT NULL', 'answer' => 'varchar(255) NOT NULL', @@ -25,9 +25,9 @@ public function up() 'created_by' => 'int(11) NOT NULL', 'updated_at' => 'datetime NOT NULL', 'updated_by' => 'int(11) NOT NULL', - ), ''); + ], ''); - $this->createTable('poll_answer_user', array( + $this->createTable('poll_answer_user', [ 'id' => 'pk', 'poll_id' => 'int(11) NOT NULL', 'poll_answer_id' => 'int(11) NOT NULL', @@ -35,7 +35,7 @@ public function up() 'created_by' => 'int(11) NOT NULL', 'updated_at' => 'datetime NOT NULL', 'updated_by' => 'int(11) NOT NULL', - ), ''); + ], ''); } public function down() diff --git a/models/Poll.php b/models/Poll.php index 6c6acaa..f0a9f3f 100644 --- a/models/Poll.php +++ b/models/Poll.php @@ -76,7 +76,7 @@ public function scenarios() */ public function rules() { - return array( + return [ [['question'], 'string', 'max' => 255], [['question'], 'required'], [['description'], 'string'], @@ -87,12 +87,12 @@ public function rules() [['question'], 'minTwoAnswers', 'on' => self::SCENARIO_EDIT], [['allow_multiple'], 'integer'], - ); + ]; } public function minTwoNewAnswers($attribute) { - if(count($this->newAnswers) < self::MIN_REQUIRED_ANSWERS) { + if (count($this->newAnswers) < self::MIN_REQUIRED_ANSWERS) { $this->addError($attribute, Yii::t('PollsModule.models_Poll', "Please specify at least {min} answers!", ["{min}" => self::MIN_REQUIRED_ANSWERS])); } } @@ -110,7 +110,7 @@ public function minTwoAnswers($attribute) */ public function attributeLabels() { - return array( + return [ 'newAnswers' => Yii::t('PollsModule.models_Poll', 'Answers'), 'editAnswers' => Yii::t('PollsModule.models_Poll', 'Answers'), 'question' => Yii::t('PollsModule.models_Poll', 'Question'), @@ -119,7 +119,7 @@ public function attributeLabels() 'is_random' => Yii::t('PollsModule.widgets_views_pollForm', 'Display answers in random order?'), 'anonymous' => Yii::t('PollsModule.widgets_views_pollForm', 'Anonymous Votes?'), 'show_result_after_close' => Yii::t('PollsModule.widgets_views_pollForm', 'Hide results until poll is closed?'), - ); + ]; } public function getIcon() @@ -173,7 +173,7 @@ public function afterSave($insert, $changedAttributes) RichText::postProcess($this->description, $this); - if($this->scenario === static::SCENARIO_EDIT || $this->scenario === static::SCENARIO_CREATE) { + if ($this->scenario === static::SCENARIO_EDIT || $this->scenario === static::SCENARIO_CREATE) { if (!$insert) { $this->updateAnswers(); } @@ -272,7 +272,7 @@ public function hasUserVoted($userId = "") $userId = Yii::$app->user->id; } - $answer = PollAnswerUser::findOne(array('created_by' => $userId, 'poll_id' => $this->id)); + $answer = PollAnswerUser::findOne(['created_by' => $userId, 'poll_id' => $this->id]); if ($answer == null) { return false; @@ -281,7 +281,7 @@ public function hasUserVoted($userId = "") return true; } - public function vote($votes = array()) + public function vote($votes = []) { if ($this->hasUserVoted()) { @@ -291,7 +291,7 @@ public function vote($votes = array()) $voted = false; foreach ($votes as $answerId) { - $answer = PollAnswer::findOne(array('id' => $answerId, 'poll_id' => $this->id)); + $answer = PollAnswer::findOne(['id' => $answerId, 'poll_id' => $this->id]); if ($answer) { $userVote = new PollAnswerUser(); $userVote->poll_id = $this->id; @@ -321,7 +321,7 @@ public function vote($votes = array()) public function resetAnswer($userId = "") { - if($this->closed) { + if ($this->closed) { return; } @@ -331,7 +331,7 @@ public function resetAnswer($userId = "") if ($this->hasUserVoted($userId)) { - $answers = PollAnswerUser::findAll(array('created_by' => $userId, 'poll_id' => $this->id)); + $answers = PollAnswerUser::findAll(['created_by' => $userId, 'poll_id' => $this->id]); foreach ($answers as $answer) { $answer->delete(); } diff --git a/models/PollAnswer.php b/models/PollAnswer.php index 3f0df01..0734e41 100644 --- a/models/PollAnswer.php +++ b/models/PollAnswer.php @@ -40,11 +40,11 @@ public static function tableName() */ public function rules() { - return array( - array(['poll_id', 'answer'], 'required'), - array(['poll_id'], 'integer'), - array(['answer'], 'string', 'max' => 255), - ); + return [ + [['poll_id', 'answer'], 'required'], + [['poll_id'], 'integer'], + [['answer'], 'string', 'max' => 255], + ]; } public function getPoll() @@ -74,7 +74,7 @@ public function beforeDelete() */ public function getPercent() { - $total = PollAnswerUser::find()->where(array('poll_id' => $this->poll_id))->count(); + $total = PollAnswerUser::find()->where(['poll_id' => $this->poll_id])->count(); if ($total == 0) { return 0; } @@ -90,7 +90,7 @@ public function getPercent() public function getTotal() { - return PollAnswerUser::find()->where(array('poll_answer_id' => $this->id))->count(); + return PollAnswerUser::find()->where(['poll_answer_id' => $this->id])->count(); } public static function filterValidAnswers($answerArr) @@ -101,7 +101,7 @@ public static function filterValidAnswers($answerArr) $result = []; foreach ($answerArr as $key => $answerText) { - if($answerText != null && $answerText !== '') { + if ($answerText != null && $answerText !== '') { $result[$key] = $answerText; } } diff --git a/models/PollAnswerUser.php b/models/PollAnswerUser.php index 60850c8..f9779e3 100644 --- a/models/PollAnswerUser.php +++ b/models/PollAnswerUser.php @@ -39,10 +39,10 @@ public static function tableName() */ public function rules() { - return array( - array(['poll_answer_id', 'poll_id'], 'required'), - array(['poll_answer_id', 'poll_id'], 'integer'), - ); + return [ + [['poll_answer_id', 'poll_id'], 'required'], + [['poll_answer_id', 'poll_id'], 'integer'], + ]; } public function getPoll() diff --git a/widgets/WallCreateForm.php b/widgets/WallCreateForm.php index 850897e..3d49219 100644 --- a/widgets/WallCreateForm.php +++ b/widgets/WallCreateForm.php @@ -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; @@ -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 ''; } }