mirror of
https://github.com/flarum/framework.git
synced 2024-11-30 05:13:37 +08:00
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:
parent
46c1dbfb2a
commit
d1ce9b1fd0
|
@ -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);
|
||||||
|
|
|
@ -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
|
||||||
|
|
36
extensions/tags/src/Access/GlobalPolicy.php
Normal file
36
extensions/tags/src/Access/GlobalPolicy.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user