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

118 lines
3.9 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-07-28 10:55:26 +02:00
use App\Notifications\Test;
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-07-28 10:55:26 +02:00
public string $emails;
2023-05-25 18:27:52 +02:00
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_notifications_test' => 'nullable|boolean',
'model.smtp_notifications_deployments' => 'nullable|boolean',
'model.smtp_notifications_status_changes' => 'nullable|boolean',
2023-05-25 18:27:52 +02:00
];
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',
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')) {
2023-06-20 21:18:14 +02:00
try {
$this->model->smtp_password = decrypt($this->model->smtp_password);
2023-06-20 21:18:14 +02:00
} catch (\Exception $e) {
}
}
}
public function mount()
{
$this->decrypt();
2023-07-28 10:55:26 +02:00
$this->emails = auth()->user()->email;
2023-06-20 21:18:14 +02:00
}
2023-06-20 19:08:43 +02:00
public function copyFromInstanceSettings()
2023-06-07 17:24:37 +02:00
{
$settings = InstanceSettings::get();
if ($settings->smtp_enabled) {
2023-07-28 11:49:25 +02:00
$team = auth()->user()->currentTeam();
2023-07-28 11:49:57 +02:00
$team->update([
'smtp_enabled' => $settings->smtp_enabled,
'smtp_from_address' => $settings->smtp_from_address,
'smtp_from_name' => $settings->smtp_from_name,
'smtp_recipients' => $settings->smtp_recipients,
'smtp_host' => $settings->smtp_host,
'smtp_port' => $settings->smtp_port,
'smtp_encryption' => $settings->smtp_encryption,
'smtp_username' => $settings->smtp_username,
'smtp_password' => $settings->smtp_password,
'smtp_timeout' => $settings->smtp_timeout,
]);
2023-07-28 11:49:25 +02:00
$this->decrypt();
if (is_a($team, Team::class)) {
2023-07-28 11:54:36 +02:00
session(['currentTeam' => $team]);
2023-07-28 11:49:25 +02:00
}
2023-07-28 12:01:22 +02:00
$this->model = $team;
2023-07-28 11:49:25 +02:00
$this->emit('success', 'Settings saved.');
2023-06-20 20:40:19 +02:00
} 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);
2023-06-20 21:18:14 +02:00
} else {
$this->model->smtp_password = null;
2023-06-20 21:18:14 +02:00
}
$this->model->smtp_recipients = str_replace(' ', '', $this->model->smtp_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-07-28 10:55:26 +02:00
$this->model->notify(new Test($this->emails));
$this->emit('success', 'Test Email sent successfully.');
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) {
$this->model->smtp_enabled = false;
2023-06-01 13:24:20 +02:00
$this->validate();
}
2023-05-25 18:27:52 +02:00
}
}