From 050d6bdc9ed49edf43c1b5fa0d2043e2c9f0b78c 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. --- .../core/src/Discussion/DiscussionPolicy.php | 2 +- framework/core/src/Group/GroupPolicy.php | 2 +- framework/core/src/Post/PostPolicy.php | 2 +- framework/core/src/User/AbstractPolicy.php | 26 +++++++++---------- framework/core/src/User/UserPolicy.php | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/framework/core/src/Discussion/DiscussionPolicy.php b/framework/core/src/Discussion/DiscussionPolicy.php index 2839f239f..2040bf09a 100644 --- a/framework/core/src/Discussion/DiscussionPolicy.php +++ b/framework/core/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/framework/core/src/Group/GroupPolicy.php b/framework/core/src/Group/GroupPolicy.php index 91af563a8..26efd8895 100644 --- a/framework/core/src/Group/GroupPolicy.php +++ b/framework/core/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/framework/core/src/Post/PostPolicy.php b/framework/core/src/Post/PostPolicy.php index bc63b7ebc..a4a550daf 100644 --- a/framework/core/src/Post/PostPolicy.php +++ b/framework/core/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/framework/core/src/User/AbstractPolicy.php b/framework/core/src/User/AbstractPolicy.php index 745b6642b..4d559e6c1 100644 --- a/framework/core/src/User/AbstractPolicy.php +++ b/framework/core/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/framework/core/src/User/UserPolicy.php b/framework/core/src/User/UserPolicy.php index 73754f0b8..1cf6694b9 100644 --- a/framework/core/src/User/UserPolicy.php +++ b/framework/core/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;