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

Adds attribute mutation if suspended. #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need / want to create whole classes for these new listeners? They seem small / simple enough that they could just be closures, and don't need any DI

Consider this to be a comment / question rather than a "changes request". Both options have merits, although from a modularization perspective I think I prefer separate classes as done here. Just raising this as a potential point of discussion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally do like using invokable classes instead, I'm not a big fan of having logic code in the extend.php file.

->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);

Expand Down
27 changes: 27 additions & 0 deletions src/Listener/PreventAttributesMutations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Suspend\Listener;

use Flarum\User\Event\Saving;
use Flarum\User\Exception\PermissionDeniedException;

class PreventAttributesMutations
{
public function handle(Saving $event)
{
if (! $event->user->suspended_until) {
return;
}

if ($event->user->id === $event->actor->id) {
throw new PermissionDeniedException;
}
}
}
20 changes: 4 additions & 16 deletions src/Listener/RevokeAccessFromSuspendedUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}