Grant global discussion permissions so that unrestricted tag permissions work

Say you have limited viewing/starting discussions globally to admins, and then have a single tag which allows everyone to view/start discussions. Previously this wouldn't work as expected because the non-admin would be stopped because when the core command checks for the global permission.
This commit is contained in:
Toby Zerner 2016-01-16 14:06:00 +10:30
parent 46c1dbfb2a
commit d1ce9b1fd0
3 changed files with 45 additions and 3 deletions

View File

@ -11,7 +11,6 @@
use Flarum\Tags\Access; use Flarum\Tags\Access;
use Flarum\Tags\Listener; use Flarum\Tags\Listener;
use Flarum\Tags\Tag;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
return function (Dispatcher $events) { return function (Dispatcher $events) {
@ -24,6 +23,7 @@ return function (Dispatcher $events) {
$events->subscribe(Listener\SaveTagsToDatabase::class); $events->subscribe(Listener\SaveTagsToDatabase::class);
$events->subscribe(Listener\UpdateTagMetadata::class); $events->subscribe(Listener\UpdateTagMetadata::class);
$events->subscribe(Access\GlobalPolicy::class);
$events->subscribe(Access\DiscussionPolicy::class); $events->subscribe(Access\DiscussionPolicy::class);
$events->subscribe(Access\TagPolicy::class); $events->subscribe(Access\TagPolicy::class);
$events->subscribe(Access\FlagPolicy::class); $events->subscribe(Access\FlagPolicy::class);

View File

@ -36,7 +36,6 @@ class DiscussionPolicy extends AbstractPolicy
/** /**
* @param SettingsRepositoryInterface $settings * @param SettingsRepositoryInterface $settings
* @param Gate $gate
*/ */
public function __construct(SettingsRepositoryInterface $settings) public function __construct(SettingsRepositoryInterface $settings)
{ {
@ -101,6 +100,12 @@ class DiscussionPolicy extends AbstractPolicy
->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'viewDiscussions')) ->whereIn('tag_id', Tag::getIdsWhereCannot($actor, 'viewDiscussions'))
->where('discussions.id', new Expression('discussion_id')); ->where('discussions.id', new Expression('discussion_id'));
}); });
// Hide discussions with no tags if the user doesn't have that global
// permission.
if (! $actor->hasPermission('viewDiscussions')) {
$query->has('tags');
}
} }
/** /**
@ -123,7 +128,8 @@ class DiscussionPolicy extends AbstractPolicy
/** /**
* This method checks, if the user is still allowed to edit the tags * This method checks, if the user is still allowed to edit the tags
* based on the configuration item * based on the configuration item.
*
* @param User $actor * @param User $actor
* @param Discussion $discussion * @param Discussion $discussion
* @return bool * @return bool

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tags\Access;
use Flarum\Event\GetPermission;
use Illuminate\Contracts\Events\Dispatcher;
class GlobalPolicy
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GetPermission::class, [$this, 'grantGlobalDiscussionPermissions']);
}
/**
* @param GetPermission $event
* @return bool
*/
public function grantGlobalDiscussionPermissions(GetPermission $event)
{
if (in_array($event->ability, ['viewDiscussions', 'startDiscussion']) && empty($event->arguments)) {
return $event->actor->hasPermissionLike($event->ability);
}
}
}