From ae2e07e94c673773814a410894e6b26364539a1a Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 11 Jan 2018 14:10:37 +1030 Subject: [PATCH] Remove use of event priorities Event priorities are no longer in Laravel - see https://github.com/laravel/framework/commit/dbbfc62beff1625b0d45bbf39650d047555cf4fa Updated the AbstractPolicy terminology to reflect the new behaviour, which is that there is no guarantee that the catch-all methods will run after all specific methods have run globally. This behaviour is only guaranteed within the policy. --- src/Discussion/DiscussionPolicy.php | 2 +- src/Group/GroupPolicy.php | 2 +- src/Post/PostPolicy.php | 2 +- src/User/AbstractPolicy.php | 26 +++++++++++++------------- src/User/UserPolicy.php | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Discussion/DiscussionPolicy.php b/src/Discussion/DiscussionPolicy.php index 2839f239f..2040bf09a 100644 --- a/src/Discussion/DiscussionPolicy.php +++ b/src/Discussion/DiscussionPolicy.php @@ -60,7 +60,7 @@ class DiscussionPolicy extends AbstractPolicy * @param string $ability * @return bool|null */ - public function after(User $actor, $ability) + public function can(User $actor, $ability) { if ($actor->hasPermission('discussion.'.$ability)) { return true; diff --git a/src/Group/GroupPolicy.php b/src/Group/GroupPolicy.php index 91af563a8..26efd8895 100644 --- a/src/Group/GroupPolicy.php +++ b/src/Group/GroupPolicy.php @@ -26,7 +26,7 @@ class GroupPolicy extends AbstractPolicy * @param string $ability * @return bool|null */ - public function after(User $actor, $ability) + public function can(User $actor, $ability) { if ($actor->hasPermission('group.'.$ability)) { return true; diff --git a/src/Post/PostPolicy.php b/src/Post/PostPolicy.php index bc63b7ebc..a4a550daf 100644 --- a/src/Post/PostPolicy.php +++ b/src/Post/PostPolicy.php @@ -60,7 +60,7 @@ class PostPolicy extends AbstractPolicy * @param \Flarum\Post\Post $post * @return bool|null */ - public function after(User $actor, $ability, Post $post) + public function can(User $actor, $ability, Post $post) { if ($actor->can($ability.'Posts', $post->discussion)) { return true; diff --git a/src/User/AbstractPolicy.php b/src/User/AbstractPolicy.php index 745b6642b..4d559e6c1 100644 --- a/src/User/AbstractPolicy.php +++ b/src/User/AbstractPolicy.php @@ -28,29 +28,29 @@ abstract class AbstractPolicy public function subscribe(Dispatcher $events) { $events->listen(GetPermission::class, [$this, 'getPermission']); - $events->listen(GetPermission::class, [$this, 'getPermissionAfter'], -100); $events->listen(ScopeModelVisibility::class, [$this, 'scopeModelVisibility']); } /** * @param GetPermission $event - * @return bool|null + * @return bool|void */ public function getPermission(GetPermission $event) { - if ($event->model instanceof $this->model && method_exists($this, $event->ability)) { - return call_user_func_array([$this, $event->ability], [$event->actor, $event->model]); + if (! $event->model instanceof $this->model) { + return; } - } - /** - * @param GetPermission $event - * @return bool|null - */ - public function getPermissionAfter(GetPermission $event) - { - if ($event->model instanceof $this->model && method_exists($this, 'after')) { - return call_user_func_array([$this, 'after'], [$event->actor, $event->ability, $event->model]); + if (method_exists($this, $event->ability)) { + $result = call_user_func_array([$this, $event->ability], [$event->actor, $event->model]); + + if (! is_null($result)) { + return $result; + } + } + + if (method_exists($this, 'can')) { + return call_user_func_array([$this, 'can'], [$event->actor, $event->ability, $event->model]); } } diff --git a/src/User/UserPolicy.php b/src/User/UserPolicy.php index 73754f0b8..1cf6694b9 100644 --- a/src/User/UserPolicy.php +++ b/src/User/UserPolicy.php @@ -25,7 +25,7 @@ class UserPolicy extends AbstractPolicy * @param string $ability * @return bool|null */ - public function after(User $actor, $ability) + public function can(User $actor, $ability) { if ($actor->hasPermission('user.'.$ability)) { return true;