mirror of
https://github.com/flarum/framework.git
synced 2025-05-21 22:36:01 +08:00
Add delete avatar action
This commit is contained in:
47
src/Api/Actions/Users/DeleteAvatarAction.php
Normal file
47
src/Api/Actions/Users/DeleteAvatarAction.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php namespace Flarum\Api\Actions\Users;
|
||||||
|
|
||||||
|
use Flarum\Core\Commands\DeleteAvatarCommand;
|
||||||
|
use Flarum\Api\Actions\SerializeResourceAction;
|
||||||
|
use Flarum\Api\JsonApiRequest;
|
||||||
|
use Flarum\Api\JsonApiResponse;
|
||||||
|
use Illuminate\Contracts\Bus\Dispatcher;
|
||||||
|
|
||||||
|
class DeleteAvatarAction extends SerializeResourceAction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \Illuminate\Contracts\Bus\Dispatcher
|
||||||
|
*/
|
||||||
|
protected $bus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the serializer class to output results with.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public static $serializer = 'Flarum\Api\Serializers\UserSerializer';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate the action.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
|
||||||
|
*/
|
||||||
|
public function __construct(Dispatcher $bus)
|
||||||
|
{
|
||||||
|
$this->bus = $bus;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a user's avatar, and return the user ready to be serialized and
|
||||||
|
* assigned to the JsonApi response.
|
||||||
|
*
|
||||||
|
* @param \Flarum\Api\JsonApiRequest $request
|
||||||
|
* @param \Flarum\Api\JsonApiResponse $response
|
||||||
|
* @return \Flarum\Core\Models\User
|
||||||
|
*/
|
||||||
|
protected function data(JsonApiRequest $request, JsonApiResponse $response)
|
||||||
|
{
|
||||||
|
return $this->bus->dispatch(
|
||||||
|
new DeleteAvatarCommand($request->get('id'), $this->actor->getUser())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +0,0 @@
|
|||||||
<?php namespace Flarum\Api\Events;
|
|
||||||
|
|
||||||
class CommandWillBeDispatched
|
|
||||||
{
|
|
||||||
public $command;
|
|
||||||
|
|
||||||
public $params;
|
|
||||||
|
|
||||||
public function __construct($command, $params)
|
|
||||||
{
|
|
||||||
$this->command = $command;
|
|
||||||
$this->params = $params;
|
|
||||||
}
|
|
||||||
}
|
|
@ -78,6 +78,11 @@ Route::group(['prefix' => 'api', 'middleware' => 'Flarum\Api\Middleware\LoginWit
|
|||||||
'uses' => $action('Flarum\Api\Actions\Users\UploadAvatarAction')
|
'uses' => $action('Flarum\Api\Actions\Users\UploadAvatarAction')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Route::delete('users/{id}/avatar', [
|
||||||
|
'as' => 'flarum.api.users.avatar.delete',
|
||||||
|
'uses' => $action('Flarum\Api\Actions\Users\DeleteAvatarAction')
|
||||||
|
]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Activity
|
| Activity
|
||||||
|
20
src/Core/Commands/DeleteAvatarCommand.php
Normal file
20
src/Core/Commands/DeleteAvatarCommand.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php namespace Flarum\Core\Commands;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class DeleteAvatarCommand
|
||||||
|
{
|
||||||
|
public $userId;
|
||||||
|
|
||||||
|
public $actor;
|
||||||
|
|
||||||
|
public function __construct($userId, $actor)
|
||||||
|
{
|
||||||
|
if (empty($userId) || !intval($userId)) {
|
||||||
|
throw new RuntimeException('No valid user ID specified.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->userId = $userId;
|
||||||
|
$this->actor = $actor;
|
||||||
|
}
|
||||||
|
}
|
16
src/Core/Events/AvatarWillBeDeleted.php
Normal file
16
src/Core/Events/AvatarWillBeDeleted.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php namespace Flarum\Core\Events;
|
||||||
|
|
||||||
|
use Flarum\Core\Models\User;
|
||||||
|
|
||||||
|
class AvatarWillBeDeleted
|
||||||
|
{
|
||||||
|
public $user;
|
||||||
|
|
||||||
|
public $command;
|
||||||
|
|
||||||
|
public function __construct(User $user, $command)
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
$this->command = $command;
|
||||||
|
}
|
||||||
|
}
|
53
src/Core/Handlers/Commands/DeleteAvatarCommandHandler.php
Normal file
53
src/Core/Handlers/Commands/DeleteAvatarCommandHandler.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php namespace Flarum\Core\Handlers\Commands;
|
||||||
|
|
||||||
|
use Flarum\Core\Commands\DeleteAvatarCommand;
|
||||||
|
use Flarum\Core\Events\AvatarWillBeDeleted;
|
||||||
|
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||||
|
use Flarum\Core\Support\DispatchesEvents;
|
||||||
|
use League\Flysystem\Adapter\Local;
|
||||||
|
use League\Flysystem\Filesystem;
|
||||||
|
use League\Flysystem\FilesystemInterface;
|
||||||
|
use League\Flysystem\MountManager;
|
||||||
|
|
||||||
|
class DeleteAvatarCommandHandler
|
||||||
|
{
|
||||||
|
use DispatchesEvents;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var UserRepositoryInterface
|
||||||
|
*/
|
||||||
|
protected $users;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var FilesystemInterface
|
||||||
|
*/
|
||||||
|
protected $uploadDir;
|
||||||
|
|
||||||
|
public function __construct(UserRepositoryInterface $users, FilesystemInterface $uploadDir)
|
||||||
|
{
|
||||||
|
$this->users = $users;
|
||||||
|
$this->uploadDir = $uploadDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(DeleteAvatarCommand $command)
|
||||||
|
{
|
||||||
|
$user = $this->users->findOrFail($command->userId);
|
||||||
|
|
||||||
|
// Make sure the current user is allowed to edit the user profile.
|
||||||
|
// This will let admins and the user themselves pass through, and
|
||||||
|
// throw an exception otherwise.
|
||||||
|
$user->assertCan($command->actor, 'edit');
|
||||||
|
|
||||||
|
$avatarPath = $user->avatar_path;
|
||||||
|
$user->changeAvatarPath(null);
|
||||||
|
|
||||||
|
event(new AvatarWillBeDeleted($user, $command));
|
||||||
|
|
||||||
|
$this->uploadDir->delete($avatarPath);
|
||||||
|
|
||||||
|
$user->save();
|
||||||
|
$this->dispatchEventsFor($user);
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
}
|
@ -47,6 +47,8 @@ class UploadAvatarCommandHandler
|
|||||||
'target' => $this->uploadDir,
|
'target' => $this->uploadDir,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// @todo delete old avatar
|
||||||
|
|
||||||
$user->changeAvatarPath($uploadName);
|
$user->changeAvatarPath($uploadName);
|
||||||
|
|
||||||
event(new AvatarWillBeUploaded($user, $command));
|
event(new AvatarWillBeUploaded($user, $command));
|
||||||
|
Reference in New Issue
Block a user