coolify/app/Livewire/Notifications/Email.php

216 lines
6.8 KiB
PHP
Raw Normal View History

2023-05-25 18:27:52 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Notifications;
2023-05-25 18:27:52 +02:00
use App\Models\Team;
2023-07-28 10:55:26 +02:00
use App\Notifications\Test;
2024-06-10 22:43:34 +02:00
use Livewire\Component;
2023-05-25 18:27:52 +02:00
class Email extends Component
2023-05-25 18:27:52 +02:00
{
2023-08-31 15:00:59 +02:00
public Team $team;
2024-06-10 22:43:34 +02:00
2023-07-28 10:55:26 +02:00
public string $emails;
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
public bool $sharedEmailEnabled = false;
2023-05-25 18:27:52 +02:00
protected $rules = [
2023-08-31 15:00:59 +02: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.smtp_notifications_scheduled_tasks' => 'nullable|boolean',
2023-08-31 15:00:59 +02:00
'team.use_instance_email_settings' => 'boolean',
'team.resend_enabled' => 'nullable|boolean',
'team.resend_api_key' => 'nullable',
2023-05-25 18:27:52 +02:00
];
2024-06-10 22:43:34 +02:00
2023-05-25 18:27:52 +02:00
protected $validationAttributes = [
2023-08-31 15:00:59 +02: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 18:27:52 +02:00
];
public function mount()
{
2023-09-06 14:31:38 +02:00
$this->team = auth()->user()->currentTeam();
2023-08-31 15:00:59 +02:00
['sharedEmailEnabled' => $this->sharedEmailEnabled] = $this->team->limits;
$this->emails = auth()->user()->email;
}
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
public function submitFromFields()
{
try {
$this->resetErrorBag();
$this->validate([
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
]);
$this->team->save();
2023-09-15 17:30:26 +02:00
refreshSession();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-08-31 15:00:59 +02:00
}
}
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
public function sendTestNotification()
{
2023-12-13 12:01:27 +01:00
$this->team?->notify(new Test($this->emails));
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Test Email sent.');
2023-08-31 15:00:59 +02:00
}
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
public function instantSaveInstance()
2023-06-20 21:18:14 +02:00
{
2023-08-31 15:00:59 +02:00
try {
2024-06-10 22:43:34 +02:00
if (! $this->sharedEmailEnabled) {
2023-08-31 15:00:59 +02:00
throw new \Exception('Not allowed to change settings. Please upgrade your subscription.');
2023-06-20 21:18:14 +02:00
}
2023-08-31 15:00:59 +02:00
$this->team->smtp_enabled = false;
$this->team->resend_enabled = false;
$this->team->save();
2023-09-15 17:30:26 +02:00
refreshSession();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-06-20 21:18:14 +02:00
}
}
2023-08-31 15:00:59 +02:00
public function instantSaveResend()
{
try {
$this->team->smtp_enabled = false;
$this->submitResend();
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-08-31 15:00:59 +02:00
$this->team->smtp_enabled = false;
2024-06-10 22:43:34 +02:00
return handleError($e, $this);
2023-08-31 15:00:59 +02:00
}
}
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
public function instantSave()
{
try {
$this->team->resend_enabled = false;
$this->submit();
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-08-31 15:00:59 +02:00
$this->team->smtp_enabled = false;
2024-06-10 22:43:34 +02:00
return handleError($e, $this);
2023-08-31 15:00:59 +02:00
}
}
2024-06-10 22:43:34 +02:00
2023-09-07 08:48:16 +02:00
public function saveModel()
{
$this->team->save();
2023-09-15 17:30:26 +02:00
refreshSession();
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Settings saved.');
2023-09-07 08:48:16 +02:00
}
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
public function submit()
{
try {
$this->resetErrorBag();
2024-06-10 22:43:34 +02:00
if (! $this->team->use_instance_email_settings) {
2024-03-20 12:54:06 +01:00
$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',
]);
}
2023-08-31 15:00:59 +02:00
$this->team->save();
2023-09-15 17:30:26 +02:00
refreshSession();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-08-31 15:00:59 +02:00
$this->team->smtp_enabled = false;
2024-06-10 22:43:34 +02:00
return handleError($e, $this);
2023-08-31 15:00:59 +02:00
}
}
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
public function submitResend()
{
try {
$this->resetErrorBag();
$this->validate([
2023-09-15 11:28:44 +02:00
'team.smtp_from_address' => 'required|email',
'team.smtp_from_name' => 'required',
2024-06-10 22:43:34 +02:00
'team.resend_api_key' => 'required',
2023-08-31 15:00:59 +02:00
]);
$this->team->save();
2023-09-15 17:30:26 +02:00
refreshSession();
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Settings saved.');
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
2023-08-31 15:00:59 +02:00
$this->team->resend_enabled = false;
2024-06-10 22:43:34 +02:00
return handleError($e, $this);
2023-08-31 15:00:59 +02:00
}
}
2024-06-10 22:43:34 +02:00
2023-06-20 19:08:43 +02:00
public function copyFromInstanceSettings()
2023-06-07 17:24:37 +02:00
{
2024-07-12 15:45:36 +02:00
$settings = \App\Models\InstanceSettings::get();
if ($settings->smtp_enabled) {
2023-08-22 17:44:49 +02:00
$team = 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-08-31 15:00:59 +02:00
refreshSession();
$this->team = $team;
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Settings saved.');
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
return;
2023-06-20 21:18:14 +02:00
}
2023-08-31 15:00:59 +02:00
if ($settings->resend_enabled) {
$team = currentTeam();
$team->update([
'resend_enabled' => $settings->resend_enabled,
'resend_api_key' => $settings->resend_api_key,
]);
2023-08-22 17:44:49 +02:00
refreshSession();
2023-08-31 15:00:59 +02:00
$this->team = $team;
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Settings saved.');
2024-06-10 22:43:34 +02:00
2023-08-31 15:00:59 +02:00
return;
2023-05-25 18:27:52 +02:00
}
2023-12-07 19:06:32 +01:00
$this->dispatch('error', 'Instance SMTP/Resend settings are not enabled.');
2023-05-25 18:27:52 +02:00
}
2024-06-10 22:43:34 +02:00
public function render()
{
return view('livewire.notifications.email');
}
}