mirror of
https://github.com/flarum/framework.git
synced 2025-03-22 13:05:15 +08:00
Use new extenders (#31)
This commit is contained in:
parent
39c687cbcc
commit
ab8a3e48b2
@ -8,9 +8,11 @@
|
||||
*/
|
||||
|
||||
use Flarum\Api\Serializer\BasicUserSerializer;
|
||||
use Flarum\Api\Serializer\UserSerializer;
|
||||
use Flarum\Event\ConfigureUserGambits;
|
||||
use Flarum\Extend;
|
||||
use Flarum\Suspend\Access;
|
||||
use Flarum\Suspend\AddUserSuspendAttributes;
|
||||
use Flarum\Suspend\Event\Suspended;
|
||||
use Flarum\Suspend\Event\Unsuspended;
|
||||
use Flarum\Suspend\Listener;
|
||||
@ -33,6 +35,9 @@ return [
|
||||
(new Extend\Model(User::class))
|
||||
->dateAttribute('suspended_until'),
|
||||
|
||||
(new Extend\ApiSerializer(UserSerializer::class))
|
||||
->mutate(AddUserSuspendAttributes::class),
|
||||
|
||||
new Extend\Locales(__DIR__.'/locale'),
|
||||
|
||||
(new Extend\Notification())
|
||||
@ -40,7 +45,6 @@ return [
|
||||
->type(UserUnsuspendedBlueprint::class, BasicUserSerializer::class, ['alert', 'email']),
|
||||
|
||||
function (Dispatcher $events) {
|
||||
$events->subscribe(Listener\AddUserSuspendAttributes::class);
|
||||
$events->subscribe(Listener\RevokeAccessFromSuspendedUsers::class);
|
||||
|
||||
$events->listen(Saving::class, Listener\SaveSuspensionToDatabase::class);
|
||||
|
30
extensions/suspend/src/AddUserSuspendAttributes.php
Executable file
30
extensions/suspend/src/AddUserSuspendAttributes.php
Executable file
@ -0,0 +1,30 @@
|
||||
<?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;
|
||||
|
||||
use Flarum\Api\Serializer\UserSerializer;
|
||||
use Flarum\User\User;
|
||||
|
||||
class AddUserSuspendAttributes
|
||||
{
|
||||
public function __invoke(UserSerializer $serializer, User $user)
|
||||
{
|
||||
$attributes = [];
|
||||
$canSuspend = $serializer->getActor()->can('suspend', $user);
|
||||
|
||||
if ($canSuspend) {
|
||||
$attributes['suspendedUntil'] = $serializer->formatDate($user->suspended_until);
|
||||
}
|
||||
|
||||
$attributes['canSuspend'] = $canSuspend;
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
<?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\Api\Event\Serializing;
|
||||
use Flarum\Api\Serializer\UserSerializer;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddUserSuspendAttributes
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(Serializing::class, [$this, 'addAttributes']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Serializing $event
|
||||
*/
|
||||
public function addAttributes(Serializing $event)
|
||||
{
|
||||
if ($event->isSerializer(UserSerializer::class)) {
|
||||
$canSuspend = $event->actor->can('suspend', $event->model);
|
||||
|
||||
if ($canSuspend) {
|
||||
$event->attributes['suspendedUntil'] = $event->formatDate($event->model->suspended_until);
|
||||
}
|
||||
|
||||
$event->attributes['canSuspend'] = $canSuspend;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user