From 1fe72163b7e55ad1fedbca0708c0fb601abbdf54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Wed, 18 Nov 2020 08:37:25 +0100 Subject: [PATCH 1/2] Adds attribute mutation if suspended. Also refactored to newer extenders. --- extend.php | 15 ++++++++------ src/Listener/PreventAttributesMutations.php | 18 +++++++++++++++++ .../RevokeAccessFromSuspendedUsers.php | 20 ++++--------------- 3 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 src/Listener/PreventAttributesMutations.php diff --git a/extend.php b/extend.php index eba4ede..e8fa9d1 100644 --- a/extend.php +++ b/extend.php @@ -39,14 +39,17 @@ ->type(UserSuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']) ->type(UserUnsuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']), - function (Dispatcher $events) { - $events->subscribe(Listener\AddUserSuspendAttributes::class); - $events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class); + (new Extend\Event()) + ->listen(Saving::class, Listener\SaveSuspensionToDatabase::class) + ->listen(Saving::class, Listener\PreventAttributesMutations::class) + ->listen(Suspended::class, Listener\SendNotificationWhenUserIsSuspended::class) + ->listen(Unsuspended::class, Listener\SendNotificationWhenUserIsUnsuspended::class), - $events->listen(Saving::class, Listener\SaveSuspensionToDatabase::class); + (new Extend\User) + ->permissionGroups(Listener\RevokeAccessFromSuspendedUsers::class), - $events->listen(Suspended::class, Listener\SendNotificationWhenUserIsSuspended::class); - $events->listen(Unsuspended::class, Listener\SendNotificationWhenUserIsUnsuspended::class); + function (Dispatcher $events) { + $events->subscribe(Listener\AddUserSuspendAttributes::class); $events->subscribe(Access\UserPolicy::class); diff --git a/src/Listener/PreventAttributesMutations.php b/src/Listener/PreventAttributesMutations.php new file mode 100644 index 0000000..f0f67f7 --- /dev/null +++ b/src/Listener/PreventAttributesMutations.php @@ -0,0 +1,18 @@ +user->suspended_until) return; + + if ($event->user->id === $event->actor->id) { + throw new PermissionDeniedException; + } + } +} diff --git a/src/Listener/RevokeAccessFromSuspendedUsers.php b/src/Listener/RevokeAccessFromSuspendedUsers.php index a8a2b30..53fc3b0 100755 --- a/src/Listener/RevokeAccessFromSuspendedUsers.php +++ b/src/Listener/RevokeAccessFromSuspendedUsers.php @@ -10,29 +10,17 @@ namespace Flarum\Suspend\Listener; use Carbon\Carbon; -use Flarum\Event\PrepareUserGroups; use Flarum\Group\Group; -use Illuminate\Contracts\Events\Dispatcher; +use Flarum\User\User; class RevokeAccessFromSuspendedUsers { - /** - * @param Dispatcher $events - */ - public function subscribe(Dispatcher $events) + public function __invoke(User $user, array $groupIds) { - $events->listen(PrepareUserGroups::class, [$this, 'prepareUserGroups']); - } - - /** - * @param PrepareUserGroups $event - */ - public function prepareUserGroups(PrepareUserGroups $event) - { - $suspendedUntil = $event->user->suspended_until; + $suspendedUntil = $user->suspended_until; if ($suspendedUntil && $suspendedUntil->gt(Carbon::now())) { - $event->groupIds = [Group::GUEST_ID]; + return [Group::GUEST_ID]; } } } From f93270dfb7846e3b920c4b4bd6ef8830d7bec187 Mon Sep 17 00:00:00 2001 From: luceos Date: Wed, 18 Nov 2020 07:38:11 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Listener/PreventAttributesMutations.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Listener/PreventAttributesMutations.php b/src/Listener/PreventAttributesMutations.php index f0f67f7..249f59a 100644 --- a/src/Listener/PreventAttributesMutations.php +++ b/src/Listener/PreventAttributesMutations.php @@ -1,5 +1,12 @@ user->suspended_until) return; + if (! $event->user->suspended_until) { + return; + } if ($event->user->id === $event->actor->id) { throw new PermissionDeniedException;