Send an email when a server is marked as installed (#1213)
Co-authored-by: @stanjg
This commit is contained in:
parent
c42605e495
commit
1ffb5acfad
7 changed files with 141 additions and 1 deletions
|
@ -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.
|
||||
|
|
15
app/Contracts/Core/ReceivesEvents.php
Normal file
15
app/Contracts/Core/ReceivesEvents.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Contracts\Core;
|
||||
|
||||
use Pterodactyl\Events\Event;
|
||||
|
||||
interface ReceivesEvents
|
||||
{
|
||||
/**
|
||||
* Handles receiving an event from the application.
|
||||
*
|
||||
* @param \Pterodactyl\Events\Event $notification
|
||||
*/
|
||||
public function handle(Event $notification): void;
|
||||
}
|
27
app/Events/Server/Installed.php
Normal file
27
app/Events/Server/Installed.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Events\Server;
|
||||
|
||||
use Pterodactyl\Events\Event;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class Installed extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Models\Server
|
||||
*/
|
||||
public $server;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @var \Pterodactyl\Models\Server
|
||||
*/
|
||||
public function __construct(Server $server)
|
||||
{
|
||||
$this->server = $server;
|
||||
}
|
||||
}
|
|
@ -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([]);
|
||||
}
|
||||
|
||||
|
|
69
app/Notifications/ServerInstalled.php
Normal file
69
app/Notifications/ServerInstalled.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Notifications;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Pterodactyl\Events\Event;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Notifications\Notification;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Pterodactyl\Contracts\Core\ReceivesEvents;
|
||||
use Illuminate\Contracts\Notifications\Dispatcher;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
|
||||
class ServerInstalled extends Notification implements ShouldQueue, ReceivesEvents
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Models\Server
|
||||
*/
|
||||
public $server;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Models\User
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* Handle a direct call to this notification from the server installed event. This is configured
|
||||
* in the event service provider.
|
||||
*
|
||||
* @param \Pterodactyl\Events\Event|\Pterodactyl\Events\Server\Installed $event
|
||||
*/
|
||||
public function handle(Event $event): void
|
||||
{
|
||||
$event->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'));
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue