fix(email notifications): enhance EmailChannel to validate team membership for recipients and handle errors gracefully
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Notifications\Channels;
|
namespace App\Notifications\Channels;
|
||||||
|
|
||||||
|
use App\Models\Team;
|
||||||
|
use Exception;
|
||||||
use Illuminate\Notifications\Notification;
|
use Illuminate\Notifications\Notification;
|
||||||
use Resend;
|
use Resend;
|
||||||
|
|
||||||
@@ -11,21 +13,53 @@ class EmailChannel
|
|||||||
|
|
||||||
public function send(SendsEmail $notifiable, Notification $notification): void
|
public function send(SendsEmail $notifiable, Notification $notification): void
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
|
// Get team and validate membership before proceeding
|
||||||
|
$team = data_get($notifiable, 'id');
|
||||||
|
$members = Team::find($team)->members;
|
||||||
|
|
||||||
$useInstanceEmailSettings = $notifiable->emailNotificationSettings->use_instance_email_settings;
|
$useInstanceEmailSettings = $notifiable->emailNotificationSettings->use_instance_email_settings;
|
||||||
$isTransactionalEmail = data_get($notification, 'isTransactionalEmail', false);
|
$isTransactionalEmail = data_get($notification, 'isTransactionalEmail', false);
|
||||||
$customEmails = data_get($notification, 'emails', null);
|
$customEmails = data_get($notification, 'emails', null);
|
||||||
|
|
||||||
if ($useInstanceEmailSettings || $isTransactionalEmail) {
|
if ($useInstanceEmailSettings || $isTransactionalEmail) {
|
||||||
$settings = instanceSettings();
|
$settings = instanceSettings();
|
||||||
} else {
|
} else {
|
||||||
$settings = $notifiable->emailNotificationSettings;
|
$settings = $notifiable->emailNotificationSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
$isResendEnabled = $settings->resend_enabled;
|
$isResendEnabled = $settings->resend_enabled;
|
||||||
$isSmtpEnabled = $settings->smtp_enabled;
|
$isSmtpEnabled = $settings->smtp_enabled;
|
||||||
|
|
||||||
if ($customEmails) {
|
if ($customEmails) {
|
||||||
$recipients = [$customEmails];
|
$recipients = [$customEmails];
|
||||||
} else {
|
} else {
|
||||||
$recipients = $notifiable->getRecipients();
|
$recipients = $notifiable->getRecipients();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate team membership for all recipients
|
||||||
|
if (count($recipients) === 0) {
|
||||||
|
throw new Exception('No email recipients found');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($recipients as $recipient) {
|
||||||
|
// Check if the recipient is part of the team
|
||||||
|
if (! $members->contains('email', $recipient)) {
|
||||||
|
$emailSettings = $notifiable->emailNotificationSettings;
|
||||||
|
data_set($emailSettings, 'smtp_password', '********');
|
||||||
|
data_set($emailSettings, 'resend_api_key', '********');
|
||||||
|
send_internal_notification(sprintf(
|
||||||
|
"Recipient is not part of the team: %s\nTeam: %s\nNotification: %s\nNotifiable: %s\nEmail Settings:\n%s",
|
||||||
|
$recipient,
|
||||||
|
$team,
|
||||||
|
get_class($notification),
|
||||||
|
get_class($notifiable),
|
||||||
|
json_encode($emailSettings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
|
||||||
|
));
|
||||||
|
throw new Exception('Recipient is not part of the team');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$mailMessage = $notification->toMail($notifiable);
|
$mailMessage = $notification->toMail($notifiable);
|
||||||
|
|
||||||
if ($isResendEnabled) {
|
if ($isResendEnabled) {
|
||||||
@@ -66,5 +100,15 @@ class EmailChannel
|
|||||||
|
|
||||||
$mailer->send($email);
|
$mailer->send($email);
|
||||||
}
|
}
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
\Illuminate\Support\Facades\Log::error('EmailChannel failed: '.$e->getMessage(), [
|
||||||
|
'notification' => get_class($notification),
|
||||||
|
'notifiable' => get_class($notifiable),
|
||||||
|
'team_id' => data_get($notifiable, 'id'),
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
'trace' => $e->getTraceAsString(),
|
||||||
|
]);
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user