From 1ffb5acfad7fa2fc0f4445170063238b34b58e98 Mon Sep 17 00:00:00 2001 From: Stan Date: Sun, 1 Jul 2018 23:34:40 +0200 Subject: [PATCH] Send an email when a server is marked as installed (#1213) Co-authored-by: @stanjg --- CHANGELOG.md | 1 + app/Contracts/Core/ReceivesEvents.php | 15 ++++ app/Events/Server/Installed.php | 27 ++++++++ .../Controllers/Daemon/ActionController.php | 22 ++++++ app/Listeners/.gitkeep | 0 app/Notifications/ServerInstalled.php | 69 +++++++++++++++++++ app/Providers/EventServiceProvider.php | 8 ++- 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 app/Contracts/Core/ReceivesEvents.php create mode 100644 app/Events/Server/Installed.php delete mode 100644 app/Listeners/.gitkeep create mode 100644 app/Notifications/ServerInstalled.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 8138b4021..382ba1bb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. * Fixed `pterodactyl.environment_variables` to be used correctly for global environment variables. The wrong config variable name was being using previously. * Fixes tokens being sent to users when their account is created to actually work. Implements Laravel's internal token creation mechanisms rather than trying to do it custom. * Updates some eggs to ensure they have the correct data and will continue working down the road. Fixes autoupdating on some source servers and MC related download links. +* Emails should send properly now when a server is marked as installed to let the owner know it is ready for action. ### Changed * Attempting to upload a folder via the web file manager will now display a warning telling the user to use SFTP. diff --git a/app/Contracts/Core/ReceivesEvents.php b/app/Contracts/Core/ReceivesEvents.php new file mode 100644 index 000000000..a6c1aa10f --- /dev/null +++ b/app/Contracts/Core/ReceivesEvents.php @@ -0,0 +1,15 @@ +server = $server; + } +} diff --git a/app/Http/Controllers/Daemon/ActionController.php b/app/Http/Controllers/Daemon/ActionController.php index fef0b35b7..ea4c52533 100644 --- a/app/Http/Controllers/Daemon/ActionController.php +++ b/app/Http/Controllers/Daemon/ActionController.php @@ -7,9 +7,26 @@ use Illuminate\Http\Request; use Pterodactyl\Models\Node; use Pterodactyl\Models\Server; use Pterodactyl\Http\Controllers\Controller; +use Pterodactyl\Events\Server\Installed as ServerInstalled; +use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; class ActionController extends Controller { + /** + * @var \Illuminate\Contracts\Events\Dispatcher + */ + private $eventDispatcher; + + /** + * ActionController constructor. + * + * @param \Illuminate\Contracts\Events\Dispatcher $eventDispatcher + */ + public function __construct(EventDispatcher $eventDispatcher) + { + $this->eventDispatcher = $eventDispatcher; + } + /** * Handles install toggle request from daemon. * @@ -37,6 +54,11 @@ class ActionController extends Controller $server->installed = ($status === 'installed') ? 1 : 2; $server->save(); + // Only fire event if server installed successfully. + if ($server->installed === 1) { + $this->eventDispatcher->dispatch(new ServerInstalled($server)); + } + return response()->json([]); } diff --git a/app/Listeners/.gitkeep b/app/Listeners/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/Notifications/ServerInstalled.php b/app/Notifications/ServerInstalled.php new file mode 100644 index 000000000..fe9747223 --- /dev/null +++ b/app/Notifications/ServerInstalled.php @@ -0,0 +1,69 @@ +server->loadMissing('user'); + + $this->server = $event->server; + $this->user = $event->server->user; + + // Since we are calling this notification directly from an event listener we need to fire off the dispatcher + // to send the email now. Don't use send() or you'll end up firing off two different events. + Container::getInstance()->make(Dispatcher::class)->sendNow($this->user, $this); + } + + /** + * Get the notification's delivery channels. + * + * @return array + */ + public function via() + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail() + { + return (new MailMessage) + ->greeting('Hello ' . $this->user->username . '.') + ->line('Your server has finished installing and is now ready for you to use.') + ->line('Server Name: ' . $this->server->name) + ->action('Login and Begin Using', route('index')); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 2ecc663fe..5be9601d6 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,6 +2,8 @@ namespace Pterodactyl\Providers; +use Pterodactyl\Events\Server\Installed as ServerInstalledEvent; +use Pterodactyl\Notifications\ServerInstalled as ServerInstalledNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider @@ -11,5 +13,9 @@ class EventServiceProvider extends ServiceProvider * * @var array */ - protected $listen = []; + protected $listen = [ + ServerInstalledEvent::class => [ + ServerInstalledNotification::class, + ], + ]; }