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

127 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-07-28 08:55:26 +00:00
use App\Notifications\Test;
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-07-28 08:55:26 +00:00
public string $emails;
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-08-10 19:00:02 +00:00
'model.smtp_notifications_database_backups' => '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
];
public function mount()
{
$this->decrypt();
$this->emails = auth()->user()->email;
}
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) {
}
}
}
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) {
2023-07-28 09:49:25 +00:00
$team = auth()->user()->currentTeam();
2023-07-28 09:49:57 +00: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 09:49:25 +00:00
$this->decrypt();
if (is_a($team, Team::class)) {
2023-07-28 09:54:36 +00:00
session(['currentTeam' => $team]);
2023-07-28 09:49:25 +00:00
}
2023-07-28 10:01:22 +00:00
$this->model = $team;
2023-07-28 09:49:25 +00:00
$this->emit('success', 'Settings saved.');
2023-06-20 18:40:19 +00:00
} else {
$this->emit('error', 'Instance SMTP settings are not enabled.');
}
2023-06-07 15:24:37 +00:00
}
public function sendTestNotification()
{
$this->model->notify(new Test($this->emails));
$this->emit('success', 'Test Email sent successfully.');
}
public function instantSave()
{
try {
$this->submit();
} catch (\Exception $e) {
$this->model->smtp_enabled = false;
$this->validate();
}
}
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
}
}