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

108 lines
3.6 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 = [
'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 16:27:52 +00: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 16:27:52 +00:00
];
2023-06-20 19:18:14 +00:00
private function decrypt()
{
if (data_get($this->model, 'smtp_password')) {
2023-06-20 19:18:14 +00:00
try {
$this->model->smtp_password = decrypt($this->model->smtp_password);
2023-06-20 19:18:14 +00:00
} 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();
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;
2023-06-20 18:40:19 +00:00
$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);
2023-06-20 19:18:14 +00:00
} else {
$this->model->smtp_password = null;
2023-06-20 19:18:14 +00:00
}
$this->model->smtp_recipients = str_replace(' ', '', $this->model->smtp_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) {
$this->model->smtp_enabled = false;
2023-06-01 11:24:20 +00:00
$this->validate();
}
2023-05-25 16:27:52 +00:00
}
}