Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
WIP #132 Add 'canAddPolls' permission
Browse files Browse the repository at this point in the history
  • Loading branch information
JN-Jones committed May 20, 2015
1 parent bbf6b37 commit 751b427
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 50 deletions.
20 changes: 19 additions & 1 deletion app/Http/Controllers/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use MyBB\Core\Exceptions\PollNoUndoException;
use MyBB\Core\Exceptions\TopicNotFoundException;
use MyBB\Core\Http\Requests\Poll\CreateRequest;
use MyBB\Core\Permissions\PermissionChecker;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class PollController extends AbstractController
Expand Down Expand Up @@ -60,6 +62,11 @@ class PollController extends AbstractController
*/
private $breadcrumbs;

/**
* @var PermissionChecker
*/
private $permissionChecker;

/**
* @param TopicRepositoryInterface $topicRepository Topic repository instance, used to fetch topic details.
* @param PollRepositoryInterface $pollRepository Poll repository instance, used to fetch poll details.
Expand All @@ -68,21 +75,24 @@ class PollController extends AbstractController
* @param ForumRepositoryInterface $forumRepository Forum repository interface, used to fetch forum details.
* @param Guard $guard Guard implementation
* @param Breadcrumbs $breadcrumbs
* @param PermissionChecker $permissionChecker
*/
public function __construct(
TopicRepositoryInterface $topicRepository,
PollRepositoryInterface $pollRepository,
PollVoteRepositoryInterface $pollVoteRepository,
ForumRepositoryInterface $forumRepository,
Guard $guard,
Breadcrumbs $breadcrumbs
Breadcrumbs $breadcrumbs,
PermissionChecker $permissionChecker
) {
$this->topicRepository = $topicRepository;
$this->pollRepository = $pollRepository;
$this->pollVoteRepository = $pollVoteRepository;
$this->forumRepository = $forumRepository;
$this->guard = $guard;
$this->breadcrumbs = $breadcrumbs;
$this->permissionChecker = $permissionChecker;
}

/**
Expand Down Expand Up @@ -143,6 +153,10 @@ public function create($slug, $id)

$this->breadcrumbs->setCurrentRoute('polls.create', $topic);

if (!$this->permissionChecker->hasPermission('forum', $topic->forum_id, 'canAddPolls')) {
throw new AccessDeniedHttpException;
}

return view('polls.create', compact('topic'));
}

Expand All @@ -163,6 +177,10 @@ public function postCreate($slug, $id, CreateRequest $createRequest)

$this->breadcrumbs->setCurrentRoute('polls.create', $topic);

if (!$this->permissionChecker->hasPermission('forum', $topic->forum_id, 'canAddPolls')) {
throw new AccessDeniedHttpException;
}

$poll = [
'topic_id' => $id,
'question' => $createRequest->input('question'),
Expand Down
12 changes: 12 additions & 0 deletions database/seeds/PermissionRoleTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public function run()
'value' => PermissionChecker::NO,
'content_id' => 0
],
[
'permission_id' => $this->perm('canAddPolls'),
'role_id' => $this->role('guest'),
'value' => PermissionChecker::NO,
'content_id' => 0
],
[
'permission_id' => $this->perm('canAddPolls'),
'role_id' => $this->role('banned'),
'value' => PermissionChecker::NO,
'content_id' => 0
],
];

DB::table('permission_role')->insert($permissions_role);
Expand Down
5 changes: 5 additions & 0 deletions database/seeds/PermissionsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function run()
'content_name' => 'forum',
'default_value' => PermissionChecker::YES
],
[
'permission_name' => 'canAddPolls',
'content_name' => 'forum',
'default_value' => PermissionChecker::YES
],
];

DB::table('permissions')->insert($permissions);
Expand Down
100 changes: 52 additions & 48 deletions resources/views/topic/create.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,67 +39,71 @@
{{ captcha() }}
</div>
{% endif %}
<div class="form__row">
<a href="#add-poll" id="add-poll-button" class="button button--secondary"><i
class="fa fa-plus-circle"></i><span class="text">{{ trans('poll.addPoll') }}</span></a>
</div>
{% if forum.hasPermission('canAddPolls') %}
<div class="form__row">
<a href="#add-poll" id="add-poll-button" class="button button--secondary"><i
class="fa fa-plus-circle"></i><span class="text">{{ trans('poll.addPoll') }}</span></a>
</div>
{% endif %}
</div>
<div id="add-poll" style="display: none;">
{{ form_hidden('add-poll', input_get('add-poll'), {'id':'add-poll-input'}) }}
<div class="form__section">
<h2>{{ trans('poll.options') }}</h2>
{% if forum.hasPermission('canAddPolls') %}
<div id="add-poll" style="display: none;">
{{ form_hidden('add-poll', input_get('add-poll'), {'id':'add-poll-input'}) }}
<div class="form__section">
<h2>{{ trans('poll.options') }}</h2>

<div class="form__section__container">
<div class="form__row">
<h3>{{ form_label('question', trans('poll.question')) }}</h3>
{{ form_text('question', input_get('question')) }}
</div>
<div class="form__row">
<h3>{{ form_label('endAt', trans('poll.endAt')) }}</h3>
{{ form_text('endAt', input_get('endAt'), {id: 'poll-end-at'}) }}
</div>
<div class="form__row">
<h3>{{ trans('poll.whoVoted') }}</h3>
<div class="form__section__container">
<div class="form__row">
<h3>{{ form_label('question', trans('poll.question')) }}</h3>
{{ form_text('question', input_get('question')) }}
</div>
<div class="form__row">
<h3>{{ form_label('endAt', trans('poll.endAt')) }}</h3>
{{ form_text('endAt', input_get('endAt'), {id: 'poll-end-at'}) }}
</div>
<div class="form__row">
<h3>{{ trans('poll.whoVoted') }}</h3>

<p class="form__checkbox">
<label>{{ form_checkbox('is_public', '1', input_get('is_public')) }} {{ trans('poll.whoVoted.desc') }}</label>
</p>
</div>
<div class="form__row">
<h3>{{ trans('poll.multipleChoice') }}</h3>
<p class="form__checkbox">
<label>{{ form_checkbox('is_public', '1', input_get('is_public')) }} {{ trans('poll.whoVoted.desc') }}</label>
</p>
</div>
<div class="form__row">
<h3>{{ trans('poll.multipleChoice') }}</h3>

<p class="form__checkbox">
<label>{{ form_checkbox('is_multiple', '1', input_get('is_multiple'), {'id': 'poll-is-multiple'}) }} {{ trans('poll.multipleChoice.desc') }}</label>
</p>
</div>
<div class="form__row" id="poll-maximum-options">
<h3>{{ trans('poll.maxOptions') }}</h3>
{{ form_text('maxoptions', input_get('maxoptions')) }}
<p class="form__checkbox">
<label>{{ form_checkbox('is_multiple', '1', input_get('is_multiple'), {'id': 'poll-is-multiple'}) }} {{ trans('poll.multipleChoice.desc') }}</label>
</p>
</div>
<div class="form__row" id="poll-maximum-options">
<h3>{{ trans('poll.maxOptions') }}</h3>
{{ form_text('maxoptions', input_get('maxoptions')) }}
</div>
</div>
</div>
</div>
<div class="form__section">
<h2>{{ trans('poll.pollOptions') }}</h2>
<div class="form__section">
<h2>{{ trans('poll.pollOptions') }}</h2>

<div class="form__section__container">
{% for i in range(1, max(2, input_old('option')|length, input_get('option')|length)) %}
<div class="form__row poll-option">
<a href="#remove-option" class="remove-option" title="{{ trans('poll.removeOption') }}"><i
class="fa fa-times"></i></a>
{{ form_text('option['~loop.index~']', input_get('option.'~loop.index)) }}
</div>
{% endfor %}
<a href="#newoption" class="button button--secondary" id="new-option"><i
class="fa fa-plus"></i><span class="text">{{ trans('poll.newOption') }}</span></a>

<div class="form__section__container">
{% for i in range(1, max(2, input_old('option')|length, input_get('option')|length)) %}
<div class="form__row poll-option">
<div class="form__row hidden" id="option-simple">
<a href="#remove-option" class="remove-option" title="{{ trans('poll.removeOption') }}"><i
class="fa fa-times"></i></a>
{{ form_text('option['~loop.index~']', input_get('option.'~loop.index)) }}
{{ form_text('option[-1]', input_get('option[-1]')) }}
</div>
{% endfor %}
<a href="#newoption" class="button button--secondary" id="new-option"><i
class="fa fa-plus"></i><span class="text">{{ trans('poll.newOption') }}</span></a>

<div class="form__row hidden" id="option-simple">
<a href="#remove-option" class="remove-option" title="{{ trans('poll.removeOption') }}"><i
class="fa fa-times"></i></a>
{{ form_text('option[-1]', input_get('option[-1]')) }}
</div>
</div>
</div>
</div>
{% endif %}
</section>
{{ form_hidden('forum_id', forum.id) }}
<div class="form__submit">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/topic/show.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{{ posts.render|raw }}
<div class="page-buttons">
<a href="#" class="button button--secondary"><i class="fa fa-plus"></i><span class="text">Follow</span></a>
{% if not topic.has_poll %}
{% if not topic.has_poll and topic.forum.hasPermission('canAddPolls') %}
<a href="{{ url_route('polls.create', {slug:topic.slug, id:topic.id}) }}" class="button button--secondary"><i class="fa fa-check-square-o"></i><span class="text">Add a poll</span></a>
{% endif %}
{% if topic.forum.hasPermission('canReply') %}
Expand Down

0 comments on commit 751b427

Please sign in to comment.