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

190 lines
6.5 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;
2023-08-31 13:00:59 +00:00
use Log;
2023-05-25 16:27:52 +00:00
class EmailSettings extends Component
{
2023-08-31 13:00:59 +00:00
public Team $team;
2023-07-28 08:55:26 +00:00
public string $emails;
2023-08-31 13:00:59 +00:00
public bool $sharedEmailEnabled = false;
2023-05-25 16:27:52 +00:00
protected $rules = [
2023-08-31 13:00:59 +00:00
'team.smtp_enabled' => 'nullable|boolean',
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
'team.smtp_recipients' => 'nullable',
'team.smtp_host' => 'required',
'team.smtp_port' => 'required',
'team.smtp_encryption' => 'nullable',
'team.smtp_username' => 'nullable',
'team.smtp_password' => 'nullable',
'team.smtp_timeout' => 'nullable',
'team.smtp_notifications_test' => 'nullable|boolean',
'team.smtp_notifications_deployments' => 'nullable|boolean',
'team.smtp_notifications_status_changes' => 'nullable|boolean',
'team.smtp_notifications_database_backups' => 'nullable|boolean',
'team.use_instance_email_settings' => 'boolean',
'team.resend_enabled' => 'nullable|boolean',
'team.resend_api_key' => 'nullable',
2023-05-25 16:27:52 +00:00
];
protected $validationAttributes = [
2023-08-31 13:00:59 +00:00
'team.smtp_from_address' => 'From Address',
'team.smtp_from_name' => 'From Name',
'team.smtp_recipients' => 'Recipients',
'team.smtp_host' => 'Host',
'team.smtp_port' => 'Port',
'team.smtp_encryption' => 'Encryption',
'team.smtp_username' => 'Username',
'team.smtp_password' => 'Password',
'team.smtp_timeout' => 'Timeout',
'team.resend_enabled' => 'Resend Enabled',
'team.resend_api_key' => 'Resend API Key',
2023-05-25 16:27:52 +00:00
];
public function mount()
{
2023-09-06 12:31:38 +00:00
$this->team = auth()->user()->currentTeam();
2023-08-31 13:00:59 +00:00
['sharedEmailEnabled' => $this->sharedEmailEnabled] = $this->team->limits;
$this->emails = auth()->user()->email;
}
2023-08-31 13:00:59 +00:00
public function submitFromFields()
{
try {
$this->resetErrorBag();
$this->validate([
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
]);
$this->team->save();
$this->emit('success', 'Settings saved successfully.');
} catch (\Exception $e) {
return general_error_handler($e, $this);
}
}
public function sendTestNotification()
{
$this->team->notify(new Test($this->emails));
$this->emit('success', 'Test Email sent successfully.');
}
public function instantSaveInstance()
2023-06-20 19:18:14 +00:00
{
2023-08-31 13:00:59 +00:00
try {
if (!$this->sharedEmailEnabled) {
throw new \Exception('Not allowed to change settings. Please upgrade your subscription.');
2023-06-20 19:18:14 +00:00
}
2023-08-31 13:00:59 +00:00
$this->team->smtp_enabled = false;
$this->team->resend_enabled = false;
$this->team->save();
$this->emit('success', 'Settings saved successfully.');
} catch (\Exception $e) {
return general_error_handler($e, $this);
2023-06-20 19:18:14 +00:00
}
}
2023-08-31 13:00:59 +00:00
public function instantSaveResend()
{
try {
$this->team->smtp_enabled = false;
$this->submitResend();
} catch (\Exception $e) {
$this->team->smtp_enabled = false;
return general_error_handler($e, $this);
}
}
public function instantSave()
{
try {
$this->team->resend_enabled = false;
$this->submit();
} catch (\Exception $e) {
$this->team->smtp_enabled = false;
return general_error_handler($e, $this);
}
}
2023-09-07 06:48:16 +00:00
public function saveModel()
{
$this->team->save();
if (is_a($this->team, Team::class)) {
refreshSession();
}
$this->emit('success', 'Settings saved.');
}
2023-08-31 13:00:59 +00:00
public function submit()
{
try {
$this->resetErrorBag();
$this->validate([
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
'team.smtp_host' => 'required',
'team.smtp_port' => 'required|numeric',
'team.smtp_encryption' => 'nullable',
'team.smtp_username' => 'nullable',
'team.smtp_password' => 'nullable',
'team.smtp_timeout' => 'nullable',
]);
$this->team->save();
$this->emit('success', 'Settings saved successfully.');
} catch (\Exception $e) {
$this->team->smtp_enabled = false;
return general_error_handler($e, $this);
}
}
public function submitResend()
{
try {
$this->resetErrorBag();
$this->validate([
'team.resend_api_key' => 'required'
]);
$this->team->save();
refreshSession();
$this->emit('success', 'Settings saved successfully.');
} catch (\Exception $e) {
$this->team->resend_enabled = false;
return general_error_handler($e, $this);
}
}
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-08-22 15:44:49 +00:00
$team = 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-08-31 13:00:59 +00:00
refreshSession();
$this->team = $team;
2023-07-28 09:49:25 +00:00
$this->emit('success', 'Settings saved.');
2023-08-31 13:00:59 +00:00
return;
2023-06-20 19:18:14 +00:00
}
2023-08-31 13:00:59 +00:00
if ($settings->resend_enabled) {
$team = currentTeam();
$team->update([
'resend_enabled' => $settings->resend_enabled,
'resend_api_key' => $settings->resend_api_key,
]);
2023-08-22 15:44:49 +00:00
refreshSession();
2023-08-31 13:00:59 +00:00
$this->team = $team;
$this->emit('success', 'Settings saved.');
return;
2023-05-25 16:27:52 +00:00
}
2023-08-31 13:00:59 +00:00
$this->emit('error', 'Instance SMTP/Resend settings are not enabled.');
2023-05-25 16:27:52 +00:00
}
}