1
0
mirror of https://github.com/flarum/core.git synced 2025-10-27 05:31:29 +01:00

Add delete avatar action

This commit is contained in:
Toby Zerner
2015-05-03 12:05:18 +09:30
parent 29be20c91a
commit b38878da80
7 changed files with 143 additions and 14 deletions

View 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;
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -47,6 +47,8 @@ class UploadAvatarCommandHandler
'target' => $this->uploadDir,
]);
// @todo delete old avatar
$user->changeAvatarPath($uploadName);
event(new AvatarWillBeUploaded($user, $command));