mirror of
https://github.com/flarum/core.git
synced 2025-10-14 00:15:51 +02:00
77 lines
1.7 KiB
PHP
77 lines
1.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Core\Command;
|
|
|
|
use Flarum\Core\Access\AssertPermissionTrait;
|
|
use Flarum\Core\AvatarUploader;
|
|
use Flarum\Core\Exception\PermissionDeniedException;
|
|
use Flarum\Core\Repository\UserRepository;
|
|
use Flarum\Core\Support\DispatchEventsTrait;
|
|
use Flarum\Event\AvatarWillBeDeleted;
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
|
class DeleteAvatarHandler
|
|
{
|
|
use DispatchEventsTrait;
|
|
use AssertPermissionTrait;
|
|
|
|
/**
|
|
* @var UserRepository
|
|
*/
|
|
protected $users;
|
|
|
|
/**
|
|
* @var AvatarUploader
|
|
*/
|
|
protected $uploader;
|
|
|
|
/**
|
|
* @param Dispatcher $events
|
|
* @param UserRepository $users
|
|
* @param AvatarUploader $uploader
|
|
*/
|
|
public function __construct(Dispatcher $events, UserRepository $users, AvatarUploader $uploader)
|
|
{
|
|
$this->events = $events;
|
|
$this->users = $users;
|
|
$this->uploader = $uploader;
|
|
}
|
|
|
|
/**
|
|
* @param DeleteAvatar $command
|
|
* @return \Flarum\Core\User
|
|
* @throws PermissionDeniedException
|
|
*/
|
|
public function handle(DeleteAvatar $command)
|
|
{
|
|
$actor = $command->actor;
|
|
|
|
$user = $this->users->findOrFail($command->userId);
|
|
|
|
if ($actor->id !== $user->id) {
|
|
$this->assertCan($actor, 'edit', $user);
|
|
}
|
|
|
|
$this->uploader->remove($user);
|
|
|
|
$this->events->fire(
|
|
new AvatarWillBeDeleted($user, $actor)
|
|
);
|
|
|
|
$user->save();
|
|
|
|
$this->dispatchEventsFor($user, $actor);
|
|
|
|
return $user;
|
|
}
|
|
}
|