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

114 lines
4.0 KiB
PHP
Raw Normal View History

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