fix: migrate db schema for smtp + discord

This commit is contained in:
Andras Bacsai
2023-07-27 21:26:15 +02:00
parent d0a85713d2
commit 9c393ff6bc
21 changed files with 382 additions and 238 deletions

View File

@@ -10,26 +10,28 @@ class DiscordSettings extends Component
{
public Team $model;
protected $rules = [
'model.discord.enabled' => 'nullable|boolean',
'model.discord.webhook_url' => 'required|url',
'model.discord_notifications.test' => 'nullable|boolean',
'model.discord_notifications.deployments' => 'nullable|boolean',
'model.discord_notifications.status_changes' => 'nullable|boolean',
'model.discord_enabled' => 'nullable|boolean',
'model.discord_webhook_url' => 'required|url',
'model.discord_notifications_test' => 'nullable|boolean',
'model.discord_notifications_deployments' => 'nullable|boolean',
'model.discord_notifications_status_changes' => 'nullable|boolean',
];
protected $validationAttributes = [
'model.discord.webhook_url' => 'Discord Webhook',
'model.discord_webhook_url' => 'Discord Webhook',
];
public function instantSave()
{
try {
$this->submit();
} catch (\Exception $e) {
$this->model->discord->enabled = false;
ray($e->getMessage());
$this->model->discord_enabled = false;
$this->validate();
}
}
public function saveModel()
{
ray($this->model);
$this->model->save();
if (is_a($this->model, Team::class)) {
session(['currentTeam' => $this->model]);
@@ -47,4 +49,4 @@ class DiscordSettings extends Component
$this->model->notify(new TestNotification('discord'));
$this->emit('success', 'Test notification sent.');
}
}
}

View File

@@ -12,37 +12,35 @@ class EmailSettings extends Component
public Team $model;
protected $rules = [
'model.smtp.enabled' => 'nullable|boolean',
'model.smtp.from_address' => 'required|email',
'model.smtp.from_name' => 'required',
'model.smtp.recipients' => 'nullable',
'model.smtp.host' => 'required',
'model.smtp.port' => 'required',
'model.smtp.encryption' => 'nullable',
'model.smtp.username' => 'nullable',
'model.smtp.password' => 'nullable',
'model.smtp.timeout' => 'nullable',
'model.smtp.test_recipients' => 'nullable',
'model.smtp_notifications.test' => 'nullable|boolean',
'model.smtp_notifications.deployments' => 'nullable|boolean',
'model.smtp_notifications.status_changes' => 'nullable|boolean',
'model.smtp_enabled' => 'nullable|boolean',
'model.smtp_from_address' => 'required|email',
'model.smtp_from_name' => 'required',
'model.smtp_recipients' => 'nullable',
'model.smtp_host' => 'required',
'model.smtp_port' => 'required',
'model.smtp_encryption' => 'nullable',
'model.smtp_username' => 'nullable',
'model.smtp_password' => 'nullable',
'model.smtp_timeout' => 'nullable',
'model.smtp_notifications_test' => 'nullable|boolean',
'model.smtp_notifications_deployments' => 'nullable|boolean',
'model.smtp_notifications_status_changes' => 'nullable|boolean',
];
protected $validationAttributes = [
'model.smtp.from_address' => 'From Address',
'model.smtp.from_name' => 'From Name',
'model.smtp.recipients' => 'Recipients',
'model.smtp.host' => 'Host',
'model.smtp.port' => 'Port',
'model.smtp.encryption' => 'Encryption',
'model.smtp.username' => 'Username',
'model.smtp.password' => 'Password',
'model.smtp.test_recipients' => 'Test Recipients',
'model.smtp_from_address' => 'From Address',
'model.smtp_from_name' => 'From Name',
'model.smtp_recipients' => 'Recipients',
'model.smtp_host' => 'Host',
'model.smtp_port' => 'Port',
'model.smtp_encryption' => 'Encryption',
'model.smtp_username' => 'Username',
'model.smtp_password' => 'Password',
];
private function decrypt()
{
if (data_get($this->model, 'smtp.password')) {
if (data_get($this->model, 'smtp_password')) {
try {
$this->model->smtp->password = decrypt($this->model->smtp->password);
$this->model->smtp_password = decrypt($this->model->smtp_password);
} catch (\Exception $e) {
}
}
@@ -54,18 +52,17 @@ class EmailSettings extends Component
public function copyFromInstanceSettings()
{
$settings = InstanceSettings::get();
if ($settings->smtp->enabled) {
$this->model->smtp->enabled = true;
$this->model->smtp->from_address = $settings->smtp->from_address;
$this->model->smtp->from_name = $settings->smtp->from_name;
$this->model->smtp->recipients = $settings->smtp->recipients;
$this->model->smtp->host = $settings->smtp->host;
$this->model->smtp->port = $settings->smtp->port;
$this->model->smtp->encryption = $settings->smtp->encryption;
$this->model->smtp->username = $settings->smtp->username;
$this->model->smtp->password = $settings->smtp->password;
$this->model->smtp->timeout = $settings->smtp->timeout;
$this->model->smtp->test_recipients = $settings->smtp->test_recipients;
if ($settings->smtp_enabled) {
$this->model->smtp_enabled = true;
$this->model->smtp_from_address = $settings->smtp_from_address;
$this->model->smtp_from_name = $settings->smtp_from_name;
$this->model->smtp_recipients = $settings->smtp_recipients;
$this->model->smtp_host = $settings->smtp_host;
$this->model->smtp_port = $settings->smtp_port;
$this->model->smtp_encryption = $settings->smtp_encryption;
$this->model->smtp_username = $settings->smtp_username;
$this->model->smtp_password = $settings->smtp_password;
$this->model->smtp_timeout = $settings->smtp_timeout;
$this->saveModel();
} else {
$this->emit('error', 'Instance SMTP settings are not enabled.');
@@ -76,14 +73,13 @@ class EmailSettings extends Component
$this->resetErrorBag();
$this->validate();
if ($this->model->smtp->password) {
$this->model->smtp->password = encrypt($this->model->smtp->password);
if ($this->model->smtp_password) {
$this->model->smtp_password = encrypt($this->model->smtp_password);
} else {
$this->model->smtp->password = null;
$this->model->smtp_password = null;
}
$this->model->smtp->recipients = str_replace(' ', '', $this->model->smtp->recipients);
$this->model->smtp->test_recipients = str_replace(' ', '', $this->model->smtp->test_recipients);
$this->model->smtp_recipients = str_replace(' ', '', $this->model->smtp_recipients);
$this->saveModel();
}
public function saveModel()
@@ -105,8 +101,8 @@ class EmailSettings extends Component
try {
$this->submit();
} catch (\Exception $e) {
$this->model->smtp->enabled = false;
$this->model->smtp_enabled = false;
$this->validate();
}
}
}
}

View File

@@ -12,27 +12,27 @@ class Email extends Component
public InstanceSettings $settings;
protected $rules = [
'settings.smtp.enabled' => 'nullable|boolean',
'settings.smtp.host' => 'required',
'settings.smtp.port' => 'required|numeric',
'settings.smtp.encryption' => 'nullable',
'settings.smtp.username' => 'nullable',
'settings.smtp.password' => 'nullable',
'settings.smtp.timeout' => 'nullable',
'settings.smtp.test_recipients' => 'nullable',
'settings.smtp.from_address' => 'required|email',
'settings.smtp.from_name' => 'required',
'settings.smtp_enabled' => 'nullable|boolean',
'settings.smtp_host' => 'required',
'settings.smtp_port' => 'required|numeric',
'settings.smtp_encryption' => 'nullable',
'settings.smtp_username' => 'nullable',
'settings.smtp_password' => 'nullable',
'settings.smtp_timeout' => 'nullable',
'settings.smtp_test_recipients' => 'nullable',
'settings.smtp_from_address' => 'required|email',
'settings.smtp_from_name' => 'required',
];
protected $validationAttributes = [
'settings.smtp.from_address' => 'From Address',
'settings.smtp.from_name' => 'From Name',
'settings.smtp.recipients' => 'Recipients',
'settings.smtp.host' => 'Host',
'settings.smtp.port' => 'Port',
'settings.smtp.encryption' => 'Encryption',
'settings.smtp.username' => 'Username',
'settings.smtp.password' => 'Password',
'settings.smtp.test_recipients' => 'Test Recipients',
'settings.smtp_from_address' => 'From Address',
'settings.smtp_from_name' => 'From Name',
'settings.smtp_recipients' => 'Recipients',
'settings.smtp_host' => 'Host',
'settings.smtp_port' => 'Port',
'settings.smtp_encryption' => 'Encryption',
'settings.smtp_username' => 'Username',
'settings.smtp_password' => 'Password',
'settings.smtp_test_recipients' => 'Test Recipients',
];
public function mount()
{
@@ -44,7 +44,7 @@ class Email extends Component
$this->submit();
$this->emit('success', 'Settings saved successfully.');
} catch (\Exception $e) {
$this->settings->smtp->enabled = false;
$this->settings->smtp_enabled = false;
$this->validate();
}
}
@@ -55,9 +55,9 @@ class Email extends Component
}
private function decrypt()
{
if (data_get($this->settings, 'smtp.password')) {
if (data_get($this->settings, 'smtp_password')) {
try {
$this->settings->smtp->password = decrypt($this->settings->smtp->password);
$this->settings->smtp_password = decrypt($this->settings->smtp_password);
} catch (\Exception $e) {
}
}
@@ -66,15 +66,14 @@ class Email extends Component
{
$this->resetErrorBag();
$this->validate();
if ($this->settings->smtp->password) {
$this->settings->smtp->password = encrypt($this->settings->smtp->password);
if ($this->settings->smtp_password) {
$this->settings->smtp_password = encrypt($this->settings->smtp_password);
} else {
$this->settings->smtp->password = null;
$this->settings->smtp_password = null;
}
$this->settings->smtp->test_recipients = str_replace(' ', '', $this->settings->smtp->test_recipients);
$this->settings->save();
$this->emit('success', 'Transaction email settings updated successfully.');
$this->decrypt();
}
}
}

View File

@@ -7,26 +7,17 @@ use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
class InstanceSettings extends Model implements SendsEmail
{
use Notifiable, SchemalessAttributesTrait;
use Notifiable;
protected $guarded = [];
protected $schemalessAttributes = [
'smtp',
];
protected $casts = [
'smtp' => SchemalessAttributes::class,
'resale_license' => 'encrypted',
];
public function scopeWithSmtp(): Builder
{
return $this->smtp->modelScope();
}
public function routeNotificationForEmail(string $attribute = 'test_recipients')
{
$recipients = $this->smtp->get($attribute, '');
$recipients = data_get($this,'smtp','');
if (is_null($recipients) || $recipients === '') {
return [];
}
@@ -36,4 +27,4 @@ class InstanceSettings extends Model implements SendsEmail
{
return InstanceSettings::findOrFail(0);
}
}
}

View File

@@ -12,60 +12,26 @@ use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
class Team extends Model implements SendsDiscord, SendsEmail
{
use Notifiable, SchemalessAttributesTrait;
use Notifiable;
protected $schemalessAttributes = [
'smtp',
'discord',
'smtp_notifications',
'discord_notifications',
];
protected $guarded = [];
protected $casts = [
'smtp' => SchemalessAttributes::class,
'discord' => SchemalessAttributes::class,
'smtp_notifications' => SchemalessAttributes::class,
'discord_notifications' => SchemalessAttributes::class,
'personal_team' => 'boolean',
];
public function scopeWithSmtp(): Builder
{
return $this->smtp->modelScope();
}
public function scopeWithDiscord(): Builder
{
return $this->discord->modelScope();
}
public function scopeWithSmtpNotifications(): Builder
{
return $this->smtp_notifications->modelScope();
}
public function scopeWithDiscordNotifications(): Builder
{
return $this->discord_notifications->modelScope();
}
protected $fillable = [
'id',
'name',
'description',
'personal_team',
'smtp',
'discord'
];
public function routeNotificationForDiscord()
{
return $this->discord->get('webhook_url');
return data_get($this, 'discord_webhook_url', null);
}
public function routeNotificationForEmail(string $attribute = 'recipients')
{
$recipients = $this->smtp->get($attribute, '');
$recipients = data_get($this, 'smtp_recipients', '');
if (is_null($recipients) || $recipients === '') {
return [];
}
return explode(',', $recipients);
}
public function subscription()
{
return $this->hasOne(Subscription::class);
@@ -113,4 +79,4 @@ class Team extends Model implements SendsDiscord, SendsEmail
}
return false;
}
}
}

View File

@@ -34,20 +34,6 @@ class User extends Authenticatable implements SendsEmail
$team = [
'name' => $user->name . "'s Team",
'personal_team' => true,
'smtp' => [
'enabled' => false,
],
'smtp_notifications' => [
'test' => true,
'deployments' => false,
],
'discord' => [
'enabled' => false,
],
'discord_notifications' => [
'test' => true,
'deployments' => false,
],
];
if ($user->id === 0) {
$team['id'] = 0;
@@ -124,4 +110,4 @@ class User extends Authenticatable implements SendsEmail
$data = Application::where('team_id', $team_id)->get();
return $data;
}
}
}

View File

@@ -24,8 +24,8 @@ class EmailChannel
[],
fn (Message $message) => $message
->from(
data_get($notifiable, 'smtp.from_address'),
data_get($notifiable, 'smtp.from_name'),
data_get($notifiable, 'smtp_from_address'),
data_get($notifiable, 'smtp_from_name'),
)
->bcc($bcc)
->subject($mailMessage->subject)
@@ -35,19 +35,19 @@ class EmailChannel
private function bootConfigs($notifiable): void
{
$password = data_get($notifiable, 'smtp.password');
$password = data_get($notifiable, 'smtp_password');
if ($password) $password = decrypt($password);
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
"transport" => "smtp",
"host" => data_get($notifiable, 'smtp.host'),
"port" => data_get($notifiable, 'smtp.port'),
"encryption" => data_get($notifiable, 'smtp.encryption'),
"username" => data_get($notifiable, 'smtp.username'),
"host" => data_get($notifiable, 'smtp_host'),
"port" => data_get($notifiable, 'smtp_port'),
"encryption" => data_get($notifiable, 'smtp_encryption'),
"username" => data_get($notifiable, 'smtp_username'),
"password" => $password,
"timeout" => data_get($notifiable, 'smtp.timeout'),
"timeout" => data_get($notifiable, 'smtp_timeout'),
"local_domain" => null,
]);
}
}
}

View File

@@ -13,7 +13,7 @@ class TransactionalEmailChannel
public function send(User $notifiable, Notification $notification): void
{
$settings = InstanceSettings::get();
if (data_get($settings, 'smtp.enabled') !== true) {
if (data_get($settings, 'smtp_enabled') !== true) {
return;
}
$email = $notifiable->email;
@@ -27,8 +27,8 @@ class TransactionalEmailChannel
[],
fn (Message $message) => $message
->from(
data_get($settings, 'smtp.from_address'),
data_get($settings, 'smtp.from_name')
data_get($settings, 'smtp_from_address'),
data_get($settings, 'smtp_from_name')
)
->to($email)
->subject($mailMessage->subject)
@@ -40,4 +40,4 @@ class TransactionalEmailChannel
{
set_transanctional_email_settings();
}
}
}

View File

@@ -38,10 +38,10 @@ class ApplicationStoppedNotification extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp.enabled');
$isDiscordEnabled = data_get($notifiable, 'discord.enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications.status_changes');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications.status_changes');
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_status_changes');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_status_changes');
if ($isEmailEnabled && $isSubscribedToEmailEvent) {
$channels[] = EmailChannel::class;
@@ -67,7 +67,7 @@ class ApplicationStoppedNotification extends Notification implements ShouldQueue
public function toDiscord(): string
{
$message = '⛔ ' . $this->application_name . ' has been stopped.
';
$message .= '[Application URL](' . $this->application_url . ')';
return $message;

View File

@@ -43,10 +43,10 @@ class DeployedSuccessfullyNotification extends Notification implements ShouldQue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp.enabled');
$isDiscordEnabled = data_get($notifiable, 'discord.enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications.deployments');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications.deployments');
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_deployments');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_deployments');
if ($isEmailEnabled && $isSubscribedToEmailEvent) {
$channels[] = EmailChannel::class;
@@ -79,16 +79,16 @@ class DeployedSuccessfullyNotification extends Notification implements ShouldQue
public function toDiscord(): string
{
if ($this->preview) {
$message = '✅ New PR' . $this->preview->pull_request_id . ' version successfully deployed of ' . $this->application_name . '
$message = '✅ New PR' . $this->preview->pull_request_id . ' version successfully deployed of ' . $this->application_name . '
';
if ($this->preview->fqdn) {
$message .= '[Open Application](' . $this->preview->fqdn . ') | ';
}
$message .= '[Deployment logs](' . $this->deployment_url . ')';
} else {
$message = '✅ New version successfully deployed of ' . $this->application_name . '
$message = '✅ New version successfully deployed of ' . $this->application_name . '
';
if ($this->fqdn) {
$message .= '[Open Application](' . $this->fqdn . ') | ';

View File

@@ -44,10 +44,10 @@ class DeployedWithErrorNotification extends Notification implements ShouldQueue
public function via(object $notifiable): array
{
$channels = [];
$isEmailEnabled = data_get($notifiable, 'smtp.enabled');
$isDiscordEnabled = data_get($notifiable, 'discord.enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications.deployments');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications.deployments');
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
$isSubscribedToEmailEvent = data_get($notifiable, 'smtp_notifications_deployments');
$isSubscribedToDiscordEvent = data_get($notifiable, 'discord_notifications_deployments');
if ($isEmailEnabled && $isSubscribedToEmailEvent) {
$channels[] = EmailChannel::class;
@@ -89,4 +89,4 @@ class DeployedWithErrorNotification extends Notification implements ShouldQueue
}
return $message;
}
}
}

View File

@@ -23,8 +23,8 @@ class TestNotification extends Notification implements ShouldQueue
$isSmtp = $this->type === 'smtp' || is_null($this->type);
$isDiscord = $this->type === 'discord' || is_null($this->type);
$isEmailEnabled = data_get($notifiable, 'smtp.enabled');
$isDiscordEnabled = data_get($notifiable, 'discord.enabled');
$isEmailEnabled = data_get($notifiable, 'smtp_enabled');
$isDiscordEnabled = data_get($notifiable, 'discord_enabled');
if ($isEmailEnabled && $isSmtp) {
$channels[] = EmailChannel::class;
@@ -50,4 +50,4 @@ class TestNotification extends Notification implements ShouldQueue
$message .= '[Go to your dashboard](' . base_url() . ')';
return $message;
}
}
}