Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of Laravel Gate contract #2181

Merged
merged 4 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
franzliedke marked this conversation as resolved.
Show resolved Hide resolved
];

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)
franzliedke marked this conversation as resolved.
Show resolved Hide resolved
{
$this->settings = $settings;
$this->gate = $gate;
$this->events = $events;
}

Expand Down
Loading