1
0
mirror of https://github.com/flarum/core.git synced 2025-10-18 18:26:07 +02:00

Policy Extender and Tests (#2461)

Policy application has also been refactored, so that policies return one of `allow`, `deny`, `forceAllow`, `forceDeny`. The result of a set of policies is no longer the first non-null result, but rather the highest priority result (forceDeny > forceAllow > deny > allow, so if a single forceDeny is present, that beats out all other returned results). This removes order in which extensions boot as a factor.
This commit is contained in:
Alexander Skvortsov
2020-12-08 19:10:06 -05:00
committed by GitHub
parent 8901073d12
commit d1dfa758e4
15 changed files with 597 additions and 125 deletions

View File

@@ -7,39 +7,27 @@
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Post;
namespace Flarum\Post\Access;
use Carbon\Carbon;
use Flarum\Post\Post;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AbstractPolicy;
use Flarum\User\Access\AbstractPolicy;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
class PostPolicy extends AbstractPolicy
{
/**
* {@inheritdoc}
*/
protected $model = Post::class;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var Dispatcher
*/
protected $events;
/**
* @param SettingsRepositoryInterface $settings
* @param Dispatcher $events
*/
public function __construct(SettingsRepositoryInterface $settings, Dispatcher $events)
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
$this->events = $events;
}
/**
@@ -51,7 +39,7 @@ class PostPolicy extends AbstractPolicy
public function can(User $actor, $ability, Post $post)
{
if ($actor->can($ability.'Posts', $post->discussion)) {
return true;
return $this->allow();
}
}
@@ -71,7 +59,7 @@ class PostPolicy extends AbstractPolicy
if ($allowEditing === '-1'
|| ($allowEditing === 'reply' && $post->number >= $post->discussion->last_post_number)
|| ($post->created_at->diffInMinutes(new Carbon) < $allowEditing)) {
return true;
return $this->allow();
}
}
}

View File

@@ -51,9 +51,6 @@ class PostServiceProvider extends AbstractServiceProvider
$this->setPostTypes();
$events = $this->app->make('events');
$events->subscribe(PostPolicy::class);
Post::registerVisibilityScoper(new ScopePostVisibility(), 'view');
}