2017-12-15 03:05:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Admin\Settings;
|
|
|
|
|
2018-09-15 23:43:22 +00:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2017-12-15 03:05:26 +00:00
|
|
|
use Illuminate\View\View;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
|
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2018-09-15 23:43:22 +00:00
|
|
|
use Pterodactyl\Notifications\MailTested;
|
2017-12-15 03:05:26 +00:00
|
|
|
use Pterodactyl\Providers\SettingsServiceProvider;
|
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
|
|
|
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
|
|
|
|
use Pterodactyl\Http\Requests\Admin\Settings\MailSettingsFormRequest;
|
|
|
|
|
|
|
|
class MailController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag
|
|
|
|
*/
|
|
|
|
private $alert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Console\Kernel
|
|
|
|
*/
|
|
|
|
private $kernel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\SettingsRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $settings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MailController constructor.
|
|
|
|
*
|
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
|
|
|
* @param \Illuminate\Contracts\Console\Kernel $kernel
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\SettingsRepositoryInterface $settings
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
|
|
|
ConfigRepository $config,
|
|
|
|
Encrypter $encrypter,
|
|
|
|
Kernel $kernel,
|
|
|
|
SettingsRepositoryInterface $settings
|
|
|
|
) {
|
|
|
|
$this->alert = $alert;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
$this->kernel = $kernel;
|
|
|
|
$this->settings = $settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render UI for editing mail settings. This UI should only display if
|
|
|
|
* the server is configured to send mail using SMTP.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function index(): View
|
|
|
|
{
|
|
|
|
return view('admin.settings.mail', [
|
|
|
|
'disabled' => $this->config->get('mail.driver') !== 'smtp',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle request to update SMTP mail settings.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Admin\Settings\MailSettingsFormRequest $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-12-15 03:05:26 +00:00
|
|
|
*/
|
|
|
|
public function update(MailSettingsFormRequest $request): RedirectResponse
|
|
|
|
{
|
|
|
|
if ($this->config->get('mail.driver') !== 'smtp') {
|
2018-09-15 23:43:22 +00:00
|
|
|
throw $this->smtpNotSelectedException();
|
2017-12-15 03:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$values = $request->normalize();
|
|
|
|
if (array_get($values, 'mail:password') === '!e') {
|
|
|
|
$values['mail:password'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
if (in_array($key, SettingsServiceProvider::getEncryptedKeys()) && ! empty($value)) {
|
|
|
|
$value = $this->encrypter->encrypt($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->settings->set('settings::' . $key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->kernel->call('queue:restart');
|
|
|
|
$this->alert->success('Mail settings have been updated successfully and the queue worker was restarted to apply these changes.')->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.settings.mail');
|
|
|
|
}
|
2018-09-15 23:43:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit a request to send a test mail message.
|
|
|
|
*
|
|
|
|
* @throws DisplayException
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function test(Request $request): RedirectResponse
|
|
|
|
{
|
|
|
|
if ($this->config->get('mail.driver') !== 'smtp') {
|
|
|
|
throw $this->smtpNotSelectedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Notification::route('mail', $request->user()->email)
|
|
|
|
->notify(new MailTested($request->user()));
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->alert->danger(trans('base.mail.test_failed'))->flash();
|
|
|
|
return redirect()->route('admin.settings.mail');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->alert->success(trans('base.mail.test_succeeded'))->flash();
|
|
|
|
return redirect()->route('admin.settings.mail');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a display exception for non-SMTP configurations.
|
|
|
|
*
|
|
|
|
* @return DisplayException
|
|
|
|
*/
|
|
|
|
private function smtpNotSelectedException() {
|
|
|
|
return new DisplayException('This feature is only available if SMTP is the selected email driver for the Panel.');
|
|
|
|
}
|
2017-12-15 03:05:26 +00:00
|
|
|
}
|