diff --git a/src/Api/Actions/Users/DeleteAvatarAction.php b/src/Api/Actions/Users/DeleteAvatarAction.php new file mode 100644 index 000000000..7d1f534a1 --- /dev/null +++ b/src/Api/Actions/Users/DeleteAvatarAction.php @@ -0,0 +1,47 @@ +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()) + ); + } +} diff --git a/src/Api/Events/CommandWillBeDispatched.php b/src/Api/Events/CommandWillBeDispatched.php deleted file mode 100644 index 0b1d12fc2..000000000 --- a/src/Api/Events/CommandWillBeDispatched.php +++ /dev/null @@ -1,14 +0,0 @@ -command = $command; - $this->params = $params; - } -} diff --git a/src/Api/routes.php b/src/Api/routes.php index 67cd4e46b..16c1d47ab 100644 --- a/src/Api/routes.php +++ b/src/Api/routes.php @@ -78,6 +78,11 @@ Route::group(['prefix' => 'api', 'middleware' => 'Flarum\Api\Middleware\LoginWit '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 diff --git a/src/Core/Commands/DeleteAvatarCommand.php b/src/Core/Commands/DeleteAvatarCommand.php new file mode 100644 index 000000000..3e38ae82a --- /dev/null +++ b/src/Core/Commands/DeleteAvatarCommand.php @@ -0,0 +1,20 @@ +userId = $userId; + $this->actor = $actor; + } +} diff --git a/src/Core/Events/AvatarWillBeDeleted.php b/src/Core/Events/AvatarWillBeDeleted.php new file mode 100644 index 000000000..c5128a65e --- /dev/null +++ b/src/Core/Events/AvatarWillBeDeleted.php @@ -0,0 +1,16 @@ +user = $user; + $this->command = $command; + } +} diff --git a/src/Core/Handlers/Commands/DeleteAvatarCommandHandler.php b/src/Core/Handlers/Commands/DeleteAvatarCommandHandler.php new file mode 100644 index 000000000..c079def54 --- /dev/null +++ b/src/Core/Handlers/Commands/DeleteAvatarCommandHandler.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/src/Core/Handlers/Commands/UploadAvatarCommandHandler.php b/src/Core/Handlers/Commands/UploadAvatarCommandHandler.php index 5e36c6da5..531d74694 100644 --- a/src/Core/Handlers/Commands/UploadAvatarCommandHandler.php +++ b/src/Core/Handlers/Commands/UploadAvatarCommandHandler.php @@ -47,6 +47,8 @@ class UploadAvatarCommandHandler 'target' => $this->uploadDir, ]); + // @todo delete old avatar + $user->changeAvatarPath($uploadName); event(new AvatarWillBeUploaded($user, $command));