Skip to content

Commit

Permalink
Deprecate AssertPermissionTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed Jul 10, 2020
1 parent 32cf35d commit 95623ea
Show file tree
Hide file tree
Showing 36 changed files with 103 additions and 133 deletions.
5 changes: 1 addition & 4 deletions src/Admin/Middleware/RequireAdministrateAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@

namespace Flarum\Admin\Middleware;

use Flarum\User\AssertPermissionTrait;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Server\RequestHandlerInterface as Handler;

class RequireAdministrateAbility implements Middleware
{
use AssertPermissionTrait;

public function process(Request $request, Handler $handler): Response
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

return $handler->handle($request);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/ClearCacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
namespace Flarum\Api\Controller;

use Flarum\Foundation\Console\CacheClearCommand;
use Flarum\User\AssertPermissionTrait;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

class ClearCacheController extends AbstractDeleteController
{
use AssertPermissionTrait;

/**
* @var CacheClearCommand
*/
Expand All @@ -38,7 +35,7 @@ public function __construct(CacheClearCommand $command)
*/
protected function delete(ServerRequestInterface $request)
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$this->command->run(
new ArrayInput([]),
Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/DeleteFaviconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;

class DeleteFaviconController extends AbstractDeleteController
{
use AssertPermissionTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand All @@ -44,7 +41,7 @@ public function __construct(SettingsRepositoryInterface $settings, FilesystemInt
*/
protected function delete(ServerRequestInterface $request)
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$path = $this->settings->get('favicon_path');

Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/DeleteLogoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;

class DeleteLogoController extends AbstractDeleteController
{
use AssertPermissionTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand All @@ -44,7 +41,7 @@ public function __construct(SettingsRepositoryInterface $settings, FilesystemInt
*/
protected function delete(ServerRequestInterface $request)
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$path = $this->settings->get('logo_path');

Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/ListNotificationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
use Flarum\Discussion\Discussion;
use Flarum\Http\UrlGenerator;
use Flarum\Notification\NotificationRepository;
use Flarum\User\AssertPermissionTrait;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class ListNotificationsController extends AbstractListController
{
use AssertPermissionTrait;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -67,7 +64,7 @@ protected function data(ServerRequestInterface $request, Document $document)
{
$actor = $request->getAttribute('actor');

$this->assertRegistered($actor);
$actor->assertRegistered();

$actor->markNotificationsAsRead()->save();

Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/ListUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria;
use Flarum\User\AssertPermissionTrait;
use Flarum\User\Search\UserSearcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class ListUsersController extends AbstractListController
{
use AssertPermissionTrait;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -70,7 +67,7 @@ protected function data(ServerRequestInterface $request, Document $document)
{
$actor = $request->getAttribute('actor');

$this->assertCan($actor, 'viewUserList');
$actor->assertCan('viewUserList');

$query = Arr::get($this->extractFilter($request), 'q');
$sort = $this->extractSort($request);
Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/SendConfirmationEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Flarum\Http\UrlGenerator;
use Flarum\Mail\Job\SendRawEmailJob;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Flarum\User\EmailToken;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Queue\Queue;
Expand All @@ -25,8 +24,6 @@

class SendConfirmationEmailController implements RequestHandlerInterface
{
use AssertPermissionTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand Down Expand Up @@ -69,7 +66,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$id = Arr::get($request->getQueryParams(), 'id');
$actor = $request->getAttribute('actor');

$this->assertRegistered($actor);
$actor->assertRegistered();

if ($actor->id != $id || $actor->is_email_confirmed) {
throw new PermissionDeniedException;
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Controller/SendTestMailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(Container $container, Mailer $mailer, TranslatorInte
public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = $request->getAttribute('actor');
$this->assertAdmin($actor);
$actor->assertAdmin($actor);

$body = $this->translator->trans('core.email.send_test.body', ['{username}' => $actor->username]);

Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/SetPermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Flarum\Api\Controller;

use Flarum\Group\Permission;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -19,14 +18,12 @@

class SetPermissionController implements RequestHandlerInterface
{
use AssertPermissionTrait;

/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$body = $request->getParsedBody();
$permission = Arr::get($body, 'permission');
Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/SetSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Flarum\Settings\Event;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Contracts\Events\Dispatcher;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -20,8 +19,6 @@

class SetSettingsController implements RequestHandlerInterface
{
use AssertPermissionTrait;

/**
* @var \Flarum\Settings\SettingsRepositoryInterface
*/
Expand All @@ -46,7 +43,7 @@ public function __construct(SettingsRepositoryInterface $settings, Dispatcher $d
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$settings = $request->getParsedBody();

Expand Down
6 changes: 1 addition & 5 deletions src/Api/Controller/ShowMailSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
namespace Flarum\Api\Controller;

use Flarum\Api\Serializer\MailSettingsSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Contracts\Validation\Factory;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class ShowMailSettingsController extends AbstractShowController
{
use AssertPermissionTrait;

/**
* {@inheritdoc}
*/
Expand All @@ -30,7 +26,7 @@ class ShowMailSettingsController extends AbstractShowController
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$drivers = array_map(function ($driver) {
return self::$container->make($driver);
Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/UninstallExtensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
namespace Flarum\Api\Controller;

use Flarum\Extension\ExtensionManager;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;

class UninstallExtensionController extends AbstractDeleteController
{
use AssertPermissionTrait;

/**
* @var ExtensionManager
*/
Expand All @@ -33,7 +30,7 @@ public function __construct(ExtensionManager $extensions)

protected function delete(ServerRequestInterface $request)
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$name = Arr::get($request->getQueryParams(), 'name');

Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/UpdateExtensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Flarum\Api\Controller;

use Flarum\Extension\ExtensionManager;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -19,8 +18,6 @@

class UpdateExtensionController implements RequestHandlerInterface
{
use AssertPermissionTrait;

/**
* @var ExtensionManager
*/
Expand All @@ -39,7 +36,7 @@ public function __construct(ExtensionManager $extensions)
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$enabled = Arr::get($request->getParsedBody(), 'enabled');
$name = Arr::get($request->getQueryParams(), 'name');
Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/UploadFaviconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
Expand All @@ -20,8 +19,6 @@

class UploadFaviconController extends ShowForumController
{
use AssertPermissionTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand All @@ -47,7 +44,7 @@ public function __construct(SettingsRepositoryInterface $settings, FilesystemInt
*/
public function data(ServerRequestInterface $request, Document $document)
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$file = Arr::get($request->getUploadedFiles(), 'favicon');
$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
Expand Down
5 changes: 1 addition & 4 deletions src/Api/Controller/UploadLogoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
Expand All @@ -20,8 +19,6 @@

class UploadLogoController extends ShowForumController
{
use AssertPermissionTrait;

/**
* @var SettingsRepositoryInterface
*/
Expand All @@ -47,7 +44,7 @@ public function __construct(SettingsRepositoryInterface $settings, FilesystemInt
*/
public function data(ServerRequestInterface $request, Document $document)
{
$this->assertAdmin($request->getAttribute('actor'));
$request->getAttribute('actor')->assertAdmin();

$file = Arr::get($request->getUploadedFiles(), 'logo');

Expand Down
4 changes: 1 addition & 3 deletions src/Discussion/Command/DeleteDiscussionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
use Flarum\Discussion\DiscussionRepository;
use Flarum\Discussion\Event\Deleting;
use Flarum\Foundation\DispatchEventsTrait;
use Flarum\User\AssertPermissionTrait;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Events\Dispatcher;

class DeleteDiscussionHandler
{
use DispatchEventsTrait;
use AssertPermissionTrait;

/**
* @var \Flarum\Discussion\DiscussionRepository
Expand Down Expand Up @@ -47,7 +45,7 @@ public function handle(DeleteDiscussion $command)

$discussion = $this->discussions->findOrFail($command->discussionId, $actor);

$this->assertCan($actor, 'delete', $discussion);
$actor->assertCan('delete', $discussion);

$this->events->dispatch(
new Deleting($discussion, $actor, $command->data)
Expand Down
Loading

0 comments on commit 95623ea

Please sign in to comment.