Test mailgun connection before storing credentials into the app

This commit is contained in:
Čarodej
2022-04-08 08:00:13 +02:00
parent 6ed2efcc4e
commit 86090d5192
14 changed files with 222 additions and 134 deletions

View File

@@ -5,6 +5,7 @@ use Illuminate\Console\Scheduling\Schedule;
use App\Console\Commands\SetupDevEnvironment;
use App\Console\Commands\SetupProdEnvironment;
use Support\Scheduler\Actions\ReportUsageAction;
use Support\Upgrading\Actions\UpdateSystemAction;
use Support\Demo\Actions\ClearHowdyDemoDataAction;
use Support\Scheduler\Actions\DeleteFailedFilesAction;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@@ -12,7 +13,6 @@ use Support\Scheduler\Actions\DeleteUnverifiedUsersAction;
use Support\Scheduler\Actions\DeleteExpiredShareLinksAction;
use App\Console\Commands\GenerateDemoSubscriptionContentCommand;
use Support\Scheduler\Actions\ExpireUnfilledUploadRequestAction;
use Support\Upgrading\Actions\UpdateSystemAction;
class Kernel extends ConsoleKernel
{

View File

@@ -0,0 +1,45 @@
<?php
namespace Domain\Settings\Actions;
use Mail;
use Domain\Settings\Mail\TestMail;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Exception\TransportException;
class TestMailgunConnectionAction
{
/**
* Throw an Exception if connection isn't successful.
*
* @return never
*/
public function __invoke(array $credentials)
{
try {
// Set temporary mail connection
config([
'mail' => [
'driver' => 'mailgun',
],
'services' => [
'mailgun' => [
'domain' => $credentials['domain'],
'secret' => $credentials['secret'],
'endpoint' => $credentials['endpoint'],
],
],
]);
// Send test email
Mail::to('test@hi5ve.digital')->send(new TestMail('example@domain.com'));
} catch (TransportException | LogicException $error) {
abort(
response()->json([
'type' => 'mailer-connection-error',
'title' => 'Mail Connection Error',
'message' => $error->getMessage(),
], 401)
);
}
}
}

View File

@@ -2,31 +2,43 @@
namespace Domain\Settings\Actions;
use Storage;
use Aws\S3\Exception\S3Exception;
use League\Flysystem\UnableToWriteFile;
use Domain\Settings\DTO\S3CredentialsData;
class TestS3ConnectionAction
{
public function __invoke(S3CredentialsData $credentials): void
{
// Set temporary s3 connection
config([
'filesystems.disks.s3' => [
'driver' => 's3',
'key' => $credentials->key,
'secret' => $credentials->secret,
'region' => $credentials->region,
'bucket' => $credentials->bucket,
'endpoint' => $credentials->endpoint,
],
]);
try {
// Set temporary s3 connection
config([
'filesystems.disks.s3' => [
'driver' => 's3',
'key' => $credentials->key,
'secret' => $credentials->secret,
'region' => $credentials->region,
'bucket' => $credentials->bucket,
'endpoint' => $credentials->endpoint,
],
]);
// Try to get files
Storage::disk('s3')->allFiles();
// Try to get files
Storage::disk('s3')->allFiles();
// Try to create folder
Storage::disk('s3')->makeDirectory('s3-test');
// Try to create folder
Storage::disk('s3')->makeDirectory('s3-test');
// Delete test folder
Storage::disk('s3')->deleteDirectory('s3-test');
// Delete test folder
Storage::disk('s3')->deleteDirectory('s3-test');
} catch (S3Exception | UnableToWriteFile $error) {
abort(
response()->json([
'type' => 's3-connection-error',
'title' => 'S3 Connection Error',
'message' => $error->getMessage(),
], 401)
);
}
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Domain\Settings\Actions;
use Mail;
use Domain\Settings\Mail\TestMail;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Exception\TransportException;
class TestSMTPConnectionAction
{
/**
* Throw an Exception if connection isn't successful.
*
* @return never
*/
public function __invoke(array $credentials)
{
try {
// Set temporary mail connection
config(['mail' => [
'driver' => 'smtp',
'host' => $credentials['host'],
'port' => $credentials['port'],
'username' => $credentials['username'],
'password' => $credentials['password'],
'encryption' => $credentials['encryption'] ?? '',
'from' => [
'address' => $credentials['email'] ?? $credentials['username'],
'name' => $credentials['email'] ?? $credentials['username'],
],
]]);
$sender = $credentials['email'] ?? $credentials['username'];
// Send test email
Mail::to('test@hi5ve.digital')->send(new TestMail($sender));
} catch (TransportException | LogicException $error) {
abort(
response()->json([
'type' => 'mailer-connection-error',
'title' => 'Mail Connection Error',
'message' => $error->getMessage(),
], 401)
);
}
}
}

View File

@@ -1,57 +1,45 @@
<?php
namespace Domain\Settings\Controllers;
use Mail;
use Artisan;
use Illuminate\Http\Response;
use Domain\Settings\Mail\TestMail;
use Symfony\Component\Mailer\Exception\LogicException;
use Illuminate\Http\JsonResponse;
use Domain\Settings\Actions\TestSMTPConnectionAction;
use Domain\Settings\Actions\TestMailgunConnectionAction;
use Domain\Settings\Requests\StoreEmailCredentialsRequest;
use Symfony\Component\Mailer\Exception\TransportException;
class StoreEmailCredentialsController
{
public function __construct(
private TestMailgunConnectionAction $testMailgunConnection,
private TestSMTPConnectionAction $testSMTPConnection,
) {
}
/**
* Set new email credentials to .env file
*/
public function __invoke(StoreEmailCredentialsRequest $request): Response
public function __invoke(StoreEmailCredentialsRequest $request): JsonResponse
{
// Abort in demo mode
abort_if(is_demo(), 204, 'Done.');
if (! app()->runningUnitTests()) {
// Test smtp server
if ($request->input('mailDriver') === 'smtp') {
try {
// Get credentials
$credentials = [
'smtp' => [
'driver' => 'smtp',
'host' => $request->input('smtp.host'),
'port' => $request->input('smtp.port'),
'username' => $request->input('smtp.username'),
'password' => $request->input('smtp.password'),
'encryption' => $request->input('smtp.encryption') ?? '',
'from.address' => $request->input('smtp.email') ?? $request->input('smtp.username'),
'from.name' => $request->input('smtp.email') ?? $request->input('smtp.username'),
],
];
// Set temporary mail connection
config(['mail' => $credentials['smtp']]);
$sender = $request->input('smtp.email') ?? $request->input('smtp.username');
// Send test email
Mail::to('test@hi5ve.digital')->send(new TestMail($sender));
} catch (TransportException|LogicException $error) {
return response([
'type' => 'mailer-connection-error',
'title' => 'Mail Connection Error',
'message' => $error->getMessage(),
], 401);
}
}
// Test email connection
match ($request->input('mailDriver')) {
'smtp' => ($this->testSMTPConnection)([
'host' => $request->input('smtp.host'),
'port' => $request->input('smtp.port'),
'username' => $request->input('smtp.username'),
'password' => $request->input('smtp.password'),
'encryption' => $request->input('smtp.encryption') ?? '',
'email' => $request->input('smtp.email'),
]),
'mailgun' => ($this->testMailgunConnection)([
'domain' => $request->input('mailgun.domain'),
'secret' => $request->input('mailgun.secret'),
'endpoint' => $request->input('mailgun.endpoint'),
]),
};
$mail = [
'log' => [
@@ -61,7 +49,7 @@ class StoreEmailCredentialsController
'MAIL_DRIVER' => 'postmark',
'POSTMARK_TOKEN' => $request->input('postmark.token'),
],
'smtp' => [
'smtp' => [
'MAIL_DRIVER' => 'smtp',
'MAIL_HOST' => $request->input('smtp.host'),
'MAIL_PORT' => $request->input('smtp.port'),
@@ -96,6 +84,6 @@ class StoreEmailCredentialsController
Artisan::call('config:cache');
}
return response('Done', 204);
return response()->json('Done', 204);
}
}

View File

@@ -1,23 +1,21 @@
<?php
namespace Domain\SetupWizard\Controllers;
use Mail;
use Artisan;
use Illuminate\Http\Response;
use Aws\S3\Exception\S3Exception;
use Domain\Settings\Mail\TestMail;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use League\Flysystem\UnableToWriteFile;
use Domain\Settings\DTO\S3CredentialsData;
use Domain\Settings\Actions\TestS3ConnectionAction;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Exception\TransportException;
use Domain\Settings\Actions\TestSMTPConnectionAction;
use Domain\Settings\Actions\TestMailgunConnectionAction;
use Domain\SetupWizard\Requests\StoreEnvironmentSetupRequest;
class StoreEnvironmentSettingsController extends Controller
{
public function __construct(
private TestS3ConnectionAction $testS3Connection,
private TestSMTPConnectionAction $testSMTPConnection,
private TestMailgunConnectionAction $testMailgunConnection,
) {
}
@@ -26,54 +24,29 @@ class StoreEnvironmentSettingsController extends Controller
*/
public function __invoke(
StoreEnvironmentSetupRequest $request,
): Response {
): JsonResponse {
if (! app()->runningUnitTests()) {
// Test s3 credentials
if ($request->input('storage.driver') !== 'local') {
try {
// connect to the s3
($this->testS3Connection)(S3CredentialsData::fromRequest($request));
} catch (S3Exception | UnableToWriteFile $error) {
return response([
'type' => 's3-connection-error',
'title' => 'S3 Connection Error',
'message' => $error->getMessage(),
], 401);
}
($this->testS3Connection)(S3CredentialsData::fromRequest($request));
}
// Test smtp server
if ($request->input('mailDriver') === 'smtp') {
try {
// Get credentials
$credentials = [
'smtp' => [
'driver' => 'smtp',
'host' => $request->input('smtp.host'),
'port' => $request->input('smtp.port'),
'username' => $request->input('smtp.username'),
'password' => $request->input('smtp.password'),
'encryption' => $request->input('smtp.encryption') ?? '',
'from.address' => $request->input('smtp.email') ?? $request->input('smtp.username'),
'from.name' => $request->input('smtp.email') ?? $request->input('smtp.username'),
],
];
// Set temporary mail connection
config(['mail' => $credentials['smtp']]);
$sender = $request->input('smtp.email') ?? $request->input('smtp.username');
// Send test email
Mail::to('test@hi5ve.digital')->send(new TestMail($sender));
} catch (TransportException|LogicException $error) {
return response([
'type' => 'mailer-connection-error',
'title' => 'Mail Connection Error',
'message' => $error->getMessage(),
], 401);
}
}
// Test email connection
match ($request->input('mailDriver')) {
'smtp' => ($this->testSMTPConnection)([
'host' => $request->input('smtp.host'),
'port' => $request->input('smtp.port'),
'username' => $request->input('smtp.username'),
'password' => $request->input('smtp.password'),
'encryption' => $request->input('smtp.encryption') ?? '',
'email' => $request->input('smtp.email'),
]),
'mailgun' => ($this->testMailgunConnection)([
'domain' => $request->input('mailgun.domain'),
'secret' => $request->input('mailgun.secret'),
'endpoint' => $request->input('mailgun.endpoint'),
]),
};
$setup = [
'broadcasting' => [
@@ -118,7 +91,7 @@ class StoreEnvironmentSettingsController extends Controller
'MAIL_DRIVER' => 'postmark',
'POSTMARK_TOKEN' => $request->input('postmark.token'),
],
'smtp' => [
'smtp' => [
'MAIL_DRIVER' => 'smtp',
'MAIL_HOST' => $request->input('smtp.host'),
'MAIL_PORT' => $request->input('smtp.port'),
@@ -174,6 +147,6 @@ class StoreEnvironmentSettingsController extends Controller
Artisan::call('config:cache');
}
return response('Done', 204);
return response()->json('Done', 204);
}
}

View File

@@ -2,7 +2,6 @@
namespace Support\Demo\Actions;
use ByteUnits\Metric;
use Domain\Sharing\Resources\ShareResource;
use Illuminate\Support\Str;
use Domain\Files\Requests\UploadRequest;

View File

@@ -121,9 +121,10 @@ class UpgradingVersionsController
public function upgrade_to_2_0_16(): void
{
($this->updateLanguageStrings)([
'write_feedback' => 'Help Us Improve',
'change_password' => 'Security & API',
'shared.empty_shared' => "Nothing Shared Yet",
'write_feedback' => 'Help Us Improve',
'change_password' => 'Security & API',
'shared.empty_shared' => 'Nothing Shared Yet',
'admin_settings.others.default_storage' => 'Default Storage Space for User Accounts (in GB)',
]);
Artisan::call('cache:clear');