Skip to content

Commit

Permalink
Get rid of Laravel Gate contract (#2181)
Browse files Browse the repository at this point in the history
* Get rid of unnecessary uses of gate

* Move gate off of Laravel's gate contract
  • Loading branch information
askvortsov1 authored May 28, 2020
1 parent bab084a commit 7b12692
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 494 deletions.
24 changes: 4 additions & 20 deletions src/Api/Serializer/DiscussionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,24 @@
namespace Flarum\Api\Serializer;

use Flarum\Discussion\Discussion;
use Flarum\User\Gate;

class DiscussionSerializer extends BasicDiscussionSerializer
{
/**
* @var \Flarum\User\Gate
*/
protected $gate;

/**
* @param \Flarum\User\Gate $gate
*/
public function __construct(Gate $gate)
{
$this->gate = $gate;
}

/**
* {@inheritdoc}
*/
protected function getDefaultAttributes($discussion)
{
$gate = $this->gate->forUser($this->actor);

$attributes = parent::getDefaultAttributes($discussion) + [
'commentCount' => (int) $discussion->comment_count,
'participantCount' => (int) $discussion->participant_count,
'createdAt' => $this->formatDate($discussion->created_at),
'lastPostedAt' => $this->formatDate($discussion->last_posted_at),
'lastPostNumber' => (int) $discussion->last_post_number,
'canReply' => $gate->allows('reply', $discussion),
'canRename' => $gate->allows('rename', $discussion),
'canDelete' => $gate->allows('delete', $discussion),
'canHide' => $gate->allows('hide', $discussion)
'canReply' => $this->actor->can('reply', $discussion),
'canRename' => $this->actor->can('rename', $discussion),
'canDelete' => $this->actor->can('delete', $discussion),
'canHide' => $this->actor->can('hide', $discussion)
];

if ($discussion->hidden_at) {
Expand Down
24 changes: 4 additions & 20 deletions src/Api/Serializer/PostSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,9 @@
namespace Flarum\Api\Serializer;

use Flarum\Post\CommentPost;
use Flarum\User\Gate;

class PostSerializer extends BasicPostSerializer
{
/**
* @var \Flarum\User\Gate
*/
protected $gate;

/**
* @param \Flarum\User\Gate $gate
*/
public function __construct(Gate $gate)
{
$this->gate = $gate;
}

/**
* {@inheritdoc}
*/
Expand All @@ -36,15 +22,13 @@ protected function getDefaultAttributes($post)

unset($attributes['content']);

$gate = $this->gate->forUser($this->actor);

$canEdit = $gate->allows('edit', $post);
$canEdit = $this->actor->can('edit', $post);

if ($post instanceof CommentPost) {
if ($canEdit) {
$attributes['content'] = $post->content;
}
if ($gate->allows('viewIps', $post)) {
if ($this->actor->can('viewIps', $post)) {
$attributes['ipAddress'] = $post->ip_address;
}
} else {
Expand All @@ -62,8 +46,8 @@ protected function getDefaultAttributes($post)

$attributes += [
'canEdit' => $canEdit,
'canDelete' => $gate->allows('delete', $post),
'canHide' => $gate->allows('hide', $post)
'canDelete' => $this->actor->can('delete', $post),
'canHide' => $this->actor->can('hide', $post)
];

return $attributes;
Expand Down
21 changes: 2 additions & 19 deletions src/Api/Serializer/UserSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,8 @@

namespace Flarum\Api\Serializer;

use Flarum\User\Gate;

class UserSerializer extends BasicUserSerializer
{
/**
* @var \Flarum\User\Gate
*/
protected $gate;

/**
* @param Gate $gate
*/
public function __construct(Gate $gate)
{
$this->gate = $gate;
}

/**
* @param \Flarum\User\User $user
* @return array
Expand All @@ -34,16 +19,14 @@ protected function getDefaultAttributes($user)
{
$attributes = parent::getDefaultAttributes($user);

$gate = $this->gate->forUser($this->actor);

$canEdit = $gate->allows('edit', $user);
$canEdit = $this->actor->can('edit', $user);

$attributes += [
'joinTime' => $this->formatDate($user->joined_at),
'discussionCount' => (int) $user->discussion_count,
'commentCount' => (int) $user->comment_count,
'canEdit' => $canEdit,
'canDelete' => $gate->allows('delete', $user),
'canDelete' => $this->actor->can('delete', $user),
];

if ($user->getPreference('discloseOnline') || $this->actor->can('viewLastSeenAt', $user)) {
Expand Down
10 changes: 1 addition & 9 deletions src/Discussion/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Flarum\Event\ScopeModelVisibility;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AbstractPolicy;
use Flarum\User\Gate;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -29,25 +28,18 @@ class DiscussionPolicy extends AbstractPolicy
*/
protected $settings;

/**
* @var Gate
*/
protected $gate;

/**
* @var Dispatcher
*/
protected $events;

/**
* @param SettingsRepositoryInterface $settings
* @param Gate $gate
* @param Dispatcher $events
*/
public function __construct(SettingsRepositoryInterface $settings, Gate $gate, Dispatcher $events)
public function __construct(SettingsRepositoryInterface $settings, Dispatcher $events)
{
$this->settings = $settings;
$this->gate = $gate;
$this->events = $events;
}

Expand Down
Loading

0 comments on commit 7b12692

Please sign in to comment.