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 'canEditPolls' and 'canEditOwnPolls' permission
Browse files Browse the repository at this point in the history
  • Loading branch information
JN-Jones committed May 21, 2015
1 parent bb2ce4e commit f7999c8
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/Database/Models/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Illuminate\Database\Eloquent\Model;
use McCool\LaravelAutoPresenter\HasPresenter;

/**
* @property Topic topic
*/
class Poll extends Model implements HasPresenter
{
// @codingStandardsIgnoreStart
Expand Down
3 changes: 3 additions & 0 deletions app/Database/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use McCool\LaravelAutoPresenter\HasPresenter;
use MyBB\Core\Likes\Traits\LikeableTrait;

/**
* @property Topic topic
*/
class Post extends Model implements HasPresenter
{
use SoftDeletes;
Expand Down
4 changes: 4 additions & 0 deletions app/Database/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;

/**
* @property int forum_id
* @property Forum forum
*/
class Topic extends Model implements HasPresenter
{
use SoftDeletes;
Expand Down
18 changes: 18 additions & 0 deletions app/Http/Controllers/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ public function remove($topicSlug, $topicId)

$poll = $topic->poll;

/** @var \MyBB\Core\Presenters\Poll $decoratedPoll */
$decoratedPoll = app()->make('MyBB\\Core\\Presenters\\Poll', [$poll]);

if (!$decoratedPoll->canEdit()) {
throw new AccessDeniedHttpException;
}

$this->pollRepository->remove($poll);

$topic->has_poll = false;
Expand Down Expand Up @@ -382,6 +389,13 @@ public function edit($topicSlug, $topicId)

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

/** @var \MyBB\Core\Presenters\Poll $decoratedPoll */
$decoratedPoll = app()->make('MyBB\\Core\\Presenters\\Poll', [$poll]);

if (!$decoratedPoll->canEdit()) {
throw new AccessDeniedHttpException;
}

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

Expand All @@ -404,8 +418,12 @@ public function postEdit($topicSlug, $topicId, CreateRequest $createRequest)
}

$poll = $topic->poll;
/** @var \MyBB\Core\Presenters\Poll $pollPresenter */
$pollPresenter = app()->make('MyBB\Core\Presenters\Poll', [$poll]);

if (!$pollPresenter->canEdit()) {
throw new AccessDeniedHttpException;
}

$options = [];
$i = 0;
Expand Down
30 changes: 29 additions & 1 deletion app/Presenters/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Auth\Guard;
use MyBB\Core\Database\Models\Poll as PollModel;
use MyBB\Core\Database\Repositories\PollVoteRepositoryInterface;
use MyBB\Core\Permissions\PermissionChecker;

class Poll extends BasePresenter
{
Expand All @@ -34,19 +35,27 @@ class Poll extends BasePresenter
*/
protected $cache = [];

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

/**
* @param PollModel $resource
* @param PollVoteRepositoryInterface $pollVoteRepository
* @param Guard $guard
* @param PermissionChecker $permissionChecker
*/
public function __construct(
PollModel $resource,
PollVoteRepositoryInterface $pollVoteRepository,
Guard $guard
Guard $guard,
PermissionChecker $permissionChecker
) {
$this->wrappedObject = $resource;
$this->pollVoteRepository = $pollVoteRepository;
$this->guard = $guard;
$this->permissionChecker = $permissionChecker;
}

/**
Expand Down Expand Up @@ -141,4 +150,23 @@ public function myVote()

return $this->cache['myVote'];
}

public function canEdit()
{
// User can edit all polls
if ($this->permissionChecker->hasPermission('forum', $this->wrappedObject->topic->forum_id, 'canEditPolls')) {
return true;
}

// Not the author -> not allowed to edit this poll
if ($this->wrappedObject->user_id != $this->guard->user()->id) {
return false;
}

return $this->permissionChecker->hasPermission(
'forum',
$this->wrappedObject->topic->forum_id,
'canEditOwnPolls'
);
}
}
18 changes: 18 additions & 0 deletions database/seeds/PermissionRoleTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ public function run()
'value' => PermissionChecker::NO,
'content_id' => 0
],
[
'permission_id' => $this->perm('canEditPolls'),
'role_id' => $this->role('admin'),
'value' => PermissionChecker::YES,
'content_id' => 0
],
[
'permission_id' => $this->perm('canEditOwnPolls'),
'role_id' => $this->role('guest'),
'value' => PermissionChecker::NO,
'content_id' => 0
],
[
'permission_id' => $this->perm('canEditOwnPolls'),
'role_id' => $this->role('banned'),
'value' => PermissionChecker::NO,
'content_id' => 0
],
[
'permission_id' => $this->perm('canVoteInPolls'),
'role_id' => $this->role('guest'),
Expand Down
10 changes: 10 additions & 0 deletions database/seeds/PermissionsTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public function run()
'content_name' => 'forum',
'default_value' => PermissionChecker::YES
],
[
'permission_name' => 'canEditPolls',
'content_name' => 'forum',
'default_value' => PermissionChecker::NO
],
[
'permission_name' => 'canEditOwnPolls',
'content_name' => 'forum',
'default_value' => PermissionChecker::YES
],
[
'permission_name' => 'canVoteInPolls',
'content_name' => 'forum',
Expand Down
14 changes: 8 additions & 6 deletions resources/views/polls/show.twig
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@
{% endif %}
<div class="poll__vote">
<div class="poll__buttons">
<a class="button button--secondary"
href="{{ url_route('polls.remove', [topic.slug, topic.id]) }}"><i
class="fa fa-trash"></i><span class="text">{{ trans('poll.remove') }}</span></a>
<a class="button button--secondary"
href="{{ url_route('polls.edit', [topic.slug, topic.id]) }}"><i
class="fa fa-pencil"></i><span class="text">{{ trans('poll.edit') }}</span></a>
{% if poll.canEdit() %}
<a class="button button--secondary"
href="{{ url_route('polls.remove', [topic.slug, topic.id]) }}"><i
class="fa fa-trash"></i><span class="text">{{ trans('poll.remove') }}</span></a>
<a class="button button--secondary"
href="{{ url_route('polls.edit', [topic.slug, topic.id]) }}"><i
class="fa fa-pencil"></i><span class="text">{{ trans('poll.edit') }}</span></a>
{% endif %}
</div>
{% if not poll.is_closed and topic.forum.hasPermission('canVoteInPolls') %}
{% if poll.myVote %}
Expand Down
14 changes: 8 additions & 6 deletions resources/views/topic/polls.twig
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
<a class="button button--secondary"
href="{{ url_route('polls.show', [topic.slug, topic.id]) }}"><i
class="fa fa-arrow-right"></i><span class="text">{{ trans('poll.results') }}</span></a>
<a class="button button--secondary"
href="{{ url_route('polls.remove', [topic.slug, topic.id]) }}"><i
class="fa fa-trash"></i><span class="text">{{ trans('poll.remove') }}</span></a>
<a class="button button--secondary"
href="{{ url_route('polls.edit', [topic.slug, topic.id]) }}"><i
class="fa fa-pencil"></i><span class="text">{{ trans('poll.edit') }}</span></a>
{% if poll.canEdit() %}
<a class="button button--secondary"
href="{{ url_route('polls.remove', [topic.slug, topic.id]) }}"><i
class="fa fa-trash"></i><span class="text">{{ trans('poll.remove') }}</span></a>
<a class="button button--secondary"
href="{{ url_route('polls.edit', [topic.slug, topic.id]) }}"><i
class="fa fa-pencil"></i><span class="text">{{ trans('poll.edit') }}</span></a>
{% endif %}
</div>
{% if not poll.is_closed and topic.forum.hasPermission('canVoteInPolls') %}
{% if poll.myVote %}
Expand Down

0 comments on commit f7999c8

Please sign in to comment.