refactor(configuration): centralize configuration management in ConfigurationRepository
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Models\PrivateKey;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use App\Models\Team;
|
||||
use App\Services\ConfigurationRepository;
|
||||
use Illuminate\Support\Collection;
|
||||
use Livewire\Component;
|
||||
use Visus\Cuid2\Cuid2;
|
||||
@@ -266,7 +267,7 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
|
||||
public function validateServer()
|
||||
{
|
||||
try {
|
||||
config()->set('constants.ssh.mux_enabled', false);
|
||||
$this->disableSshMux();
|
||||
|
||||
// EC2 does not have `uptime` command, lol
|
||||
instant_remote_process(['ls /'], $this->createdServer, true);
|
||||
@@ -376,6 +377,12 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
|
||||
['private' => $this->privateKey, 'public' => $this->publicKey] = generateSSHKey();
|
||||
}
|
||||
|
||||
private function disableSshMux(): void
|
||||
{
|
||||
$configRepository = app(ConfigurationRepository::class);
|
||||
$configRepository->disableSshMux();
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.boarding.index')->layout('layouts.boarding');
|
||||
|
@@ -10,6 +10,7 @@ use App\Events\ServerReachabilityChanged;
|
||||
use App\Jobs\CheckAndStartSentinelJob;
|
||||
use App\Notifications\Server\Reachable;
|
||||
use App\Notifications\Server\Unreachable;
|
||||
use App\Services\ConfigurationRepository;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -543,7 +544,7 @@ $schema://$host {
|
||||
$this->settings->save();
|
||||
$sshKeyFileLocation = "id.root@{$this->uuid}";
|
||||
Storage::disk('ssh-keys')->delete($sshKeyFileLocation);
|
||||
Storage::disk('ssh-mux')->delete($this->muxFilename());
|
||||
$this->disableSshMux();
|
||||
}
|
||||
|
||||
public function sentinelHeartbeat(bool $isReset = false)
|
||||
@@ -1103,7 +1104,7 @@ $schema://$host {
|
||||
|
||||
public function validateConnection(bool $justCheckingNewKey = false)
|
||||
{
|
||||
config()->set('constants.ssh.mux_enabled', false);
|
||||
$this->disableSshMux();
|
||||
|
||||
if ($this->skipServer()) {
|
||||
return ['uptime' => false, 'error' => 'Server skipped.'];
|
||||
@@ -1330,4 +1331,10 @@ $schema://$host {
|
||||
$this->databases()->count() == 0 &&
|
||||
$this->services()->count() == 0;
|
||||
}
|
||||
|
||||
private function disableSshMux(): void
|
||||
{
|
||||
$configRepository = app(ConfigurationRepository::class);
|
||||
$configRepository->disableSshMux();
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Notifications\Channels;
|
||||
|
||||
use App\Services\ConfigurationRepository;
|
||||
use Exception;
|
||||
use Illuminate\Mail\Message;
|
||||
use Illuminate\Notifications\Notification;
|
||||
@@ -9,6 +10,13 @@ use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class EmailChannel
|
||||
{
|
||||
private ConfigurationRepository $configRepository;
|
||||
|
||||
public function __construct(ConfigurationRepository $configRepository)
|
||||
{
|
||||
$this->configRepository = $configRepository;
|
||||
}
|
||||
|
||||
public function send(SendsEmail $notifiable, Notification $notification): void
|
||||
{
|
||||
try {
|
||||
@@ -57,34 +65,6 @@ class EmailChannel
|
||||
return;
|
||||
}
|
||||
|
||||
config()->set('mail.from.address', $emailSettings->smtp_from_address ?? 'test@example.com');
|
||||
config()->set('mail.from.name', $emailSettings->smtp_from_name ?? 'Test');
|
||||
|
||||
if ($emailSettings->resend_enabled) {
|
||||
config()->set('mail.default', 'resend');
|
||||
config()->set('resend.api_key', $emailSettings->resend_api_key);
|
||||
}
|
||||
|
||||
if ($emailSettings->smtp_enabled) {
|
||||
$encryption = match (strtolower($emailSettings->smtp_encryption)) {
|
||||
'starttls' => null,
|
||||
'tls' => 'tls',
|
||||
'none' => null,
|
||||
default => null,
|
||||
};
|
||||
|
||||
config()->set('mail.default', 'smtp');
|
||||
config()->set('mail.mailers.smtp', [
|
||||
'transport' => 'smtp',
|
||||
'host' => $emailSettings->smtp_host,
|
||||
'port' => $emailSettings->smtp_port,
|
||||
'encryption' => $encryption,
|
||||
'username' => $emailSettings->smtp_username,
|
||||
'password' => $emailSettings->smtp_password,
|
||||
'timeout' => $emailSettings->smtp_timeout,
|
||||
'local_domain' => null,
|
||||
'auto_tls' => $emailSettings->smtp_encryption === 'none' ? '0' : '', // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
|
||||
]);
|
||||
}
|
||||
$this->configRepository->updateMailConfig($emailSettings);
|
||||
}
|
||||
}
|
||||
|
21
app/Providers/ConfigurationServiceProvider.php
Normal file
21
app/Providers/ConfigurationServiceProvider.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Services\ConfigurationRepository;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ConfigurationServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(ConfigurationRepository::class, function ($app) {
|
||||
return new ConfigurationRepository($app['config']);
|
||||
});
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
56
app/Services/ConfigurationRepository.php
Normal file
56
app/Services/ConfigurationRepository.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Config\Repository;
|
||||
|
||||
class ConfigurationRepository
|
||||
{
|
||||
private Repository $config;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function updateMailConfig($settings): void
|
||||
{
|
||||
if ($settings->resend_enabled) {
|
||||
$this->config->set('mail.default', 'resend');
|
||||
$this->config->set('mail.from.address', $settings->smtp_from_address ?? 'test@example.com');
|
||||
$this->config->set('mail.from.name', $settings->smtp_from_name ?? 'Test');
|
||||
$this->config->set('resend.api_key', $settings->resend_api_key);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($settings->smtp_enabled) {
|
||||
$encryption = match (strtolower($settings->smtp_encryption)) {
|
||||
'starttls' => null,
|
||||
'tls' => 'tls',
|
||||
'none' => null,
|
||||
default => null,
|
||||
};
|
||||
|
||||
$this->config->set('mail.default', 'smtp');
|
||||
$this->config->set('mail.from.address', $settings->smtp_from_address ?? 'test@example.com');
|
||||
$this->config->set('mail.from.name', $settings->smtp_from_name ?? 'Test');
|
||||
$this->config->set('mail.mailers.smtp', [
|
||||
'transport' => 'smtp',
|
||||
'host' => $settings->smtp_host,
|
||||
'port' => $settings->smtp_port,
|
||||
'encryption' => $encryption,
|
||||
'username' => $settings->smtp_username,
|
||||
'password' => $settings->smtp_password,
|
||||
'timeout' => $settings->smtp_timeout,
|
||||
'local_domain' => null,
|
||||
'auto_tls' => $settings->smtp_encryption === 'none' ? '0' : '',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function disableSshMux(): void
|
||||
{
|
||||
$this->config->set('constants.ssh.mux_enabled', false);
|
||||
}
|
||||
}
|
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\Team;
|
||||
use App\Notifications\Internal\GeneralNotification;
|
||||
use Illuminate\Mail\Message;
|
||||
@@ -54,7 +53,7 @@ function send_user_an_email(MailMessage $mail, string $email, ?string $cc = null
|
||||
}
|
||||
}
|
||||
|
||||
function set_transanctional_email_settings(?InstanceSettings $settings = null): ?string // returns null|resend|smtp and defaults to array based on mail.php config
|
||||
function set_transanctional_email_settings($settings = null)
|
||||
{
|
||||
if (! $settings) {
|
||||
$settings = instanceSettings();
|
||||
@@ -63,38 +62,16 @@ function set_transanctional_email_settings(?InstanceSettings $settings = null):
|
||||
return null;
|
||||
}
|
||||
|
||||
if (data_get($settings, 'resend_enabled')) {
|
||||
config()->set('mail.default', 'resend');
|
||||
config()->set('mail.from.address', data_get($settings, 'smtp_from_address'));
|
||||
config()->set('mail.from.name', data_get($settings, 'smtp_from_name'));
|
||||
config()->set('resend.api_key', data_get($settings, 'resend_api_key'));
|
||||
$configRepository = app('App\Services\ConfigurationRepository'::class);
|
||||
$configRepository->updateMailConfig($settings);
|
||||
|
||||
if (data_get($settings, 'resend_enabled')) {
|
||||
return 'resend';
|
||||
}
|
||||
|
||||
$encryption = match (strtolower(data_get($settings, 'smtp_encryption'))) {
|
||||
'starttls' => null,
|
||||
'tls' => 'tls',
|
||||
'none' => null,
|
||||
default => null,
|
||||
};
|
||||
|
||||
if (data_get($settings, 'smtp_enabled')) {
|
||||
config()->set('mail.from.address', data_get($settings, 'smtp_from_address'));
|
||||
config()->set('mail.from.name', data_get($settings, 'smtp_from_name'));
|
||||
config()->set('mail.default', 'smtp');
|
||||
config()->set('mail.mailers.smtp', [
|
||||
'transport' => 'smtp',
|
||||
'host' => data_get($settings, 'smtp_host'),
|
||||
'port' => data_get($settings, 'smtp_port'),
|
||||
'encryption' => $encryption,
|
||||
'username' => data_get($settings, 'smtp_username'),
|
||||
'password' => data_get($settings, 'smtp_password'),
|
||||
'timeout' => data_get($settings, 'smtp_timeout'),
|
||||
'local_domain' => null,
|
||||
'auto_tls' => data_get($settings, 'smtp_encryption') === 'none' ? '0' : '', // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
|
||||
]);
|
||||
|
||||
return 'smtp';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@@ -199,6 +199,7 @@ return [
|
||||
App\Providers\EventServiceProvider::class,
|
||||
App\Providers\HorizonServiceProvider::class,
|
||||
App\Providers\RouteServiceProvider::class,
|
||||
App\Providers\ConfigurationServiceProvider::class,
|
||||
],
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user