coolify/app/Http/Livewire/Notifications/EmailSettings.php

114 lines
4.0 KiB
PHP
Raw Normal View History

2023-05-25 18:27:52 +02:00
<?php
namespace App\Http\Livewire\Notifications;
2023-06-07 17:24:37 +02:00
use App\Models\InstanceSettings;
2023-05-25 18:27:52 +02:00
use App\Models\Team;
2023-06-14 11:54:00 +02:00
use App\Notifications\Notifications\TestNotification;
2023-05-25 18:27:52 +02:00
use Livewire\Component;
class EmailSettings extends Component
{
2023-06-02 12:34:45 +02:00
public Team $model;
2023-05-25 18:27:52 +02:00
protected $rules = [
2023-06-20 19:08:43 +02:00
'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.discord_notifications.test' => 'nullable|boolean',
'model.discord_notifications.deployments' => 'nullable|boolean',
2023-05-25 18:27:52 +02:00
];
protected $validationAttributes = [
2023-06-20 19:08:43 +02:00
'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',
2023-05-25 18:27:52 +02:00
];
2023-06-20 21:18:14 +02:00
private function decrypt()
{
if (data_get($this->model, 'smtp.password')) {
try {
$this->model->smtp->password = decrypt($this->model->smtp->password);
} catch (\Exception $e) {
}
}
}
public function mount()
{
$this->decrypt();
}
2023-06-20 19:08:43 +02:00
public function copyFromInstanceSettings()
2023-06-07 17:24:37 +02:00
{
$settings = InstanceSettings::get();
2023-06-20 20:40:19 +02:00
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;
$this->saveModel();
} else {
$this->emit('error', 'Instance SMTP settings are not enabled.');
}
2023-06-07 17:24:37 +02:00
}
2023-05-25 18:27:52 +02:00
public function submit()
{
$this->resetErrorBag();
$this->validate();
2023-06-20 21:18:14 +02:00
if ($this->model->smtp->password) {
$this->model->smtp->password = encrypt($this->model->smtp->password);
} else {
$this->model->smtp->password = null;
}
2023-06-20 19:08:43 +02:00
$this->model->smtp->recipients = str_replace(' ', '', $this->model->smtp->recipients);
$this->model->smtp->test_recipients = str_replace(' ', '', $this->model->smtp->test_recipients);
2023-06-01 12:15:33 +02:00
$this->saveModel();
}
2023-06-19 15:24:04 +02:00
public function saveModel()
2023-06-01 12:15:33 +02:00
{
2023-05-25 18:27:52 +02:00
$this->model->save();
2023-06-20 21:23:29 +02:00
$this->decrypt();
2023-06-01 12:15:33 +02:00
if (is_a($this->model, Team::class)) {
2023-05-25 18:27:52 +02:00
session(['currentTeam' => $this->model]);
}
2023-06-19 14:31:42 +02:00
$this->emit('success', 'Settings saved.');
2023-05-25 18:27:52 +02:00
}
2023-06-07 17:24:37 +02:00
public function sendTestNotification()
{
2023-06-20 15:04:46 +02:00
$this->model->notify(new TestNotification('smtp'));
2023-06-19 15:24:04 +02:00
$this->emit('success', 'Test notification sent.');
2023-06-19 14:31:42 +02:00
}
2023-06-01 12:15:33 +02:00
public function instantSave()
2023-05-25 18:27:52 +02:00
{
2023-06-01 13:24:20 +02:00
try {
$this->submit();
} catch (\Exception $e) {
2023-06-20 19:08:43 +02:00
$this->model->smtp->enabled = false;
2023-06-01 13:24:20 +02:00
$this->validate();
}
2023-05-25 18:27:52 +02:00
}
}