diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index bd12050dc7..1e4a912027 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -6,6 +6,8 @@ use App\Models\Setting; use App\Notifications\AuditNotification; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; +use Osama\LaravelTeamsNotification\TeamsNotification; trait Loggable { @@ -242,7 +244,14 @@ trait Loggable 'location' => ($location) ? $location->name : '', 'note' => $note, ]; - Setting::getSettings()->notify(new AuditNotification($params)); + if(Setting::getSettings()->webhook_selected === 'microsoft' && Str::contains(Setting::getSettings()->webhook_endpoint, 'workflows')){ + $message = AuditNotification::toMicrosoftTeams($params); + $notification = new TeamsNotification(Setting::getSettings()->webhook_endpoint); + $notification->success()->sendMessage($message[0], $message[1]); + } + else { + Setting::getSettings()->notify(new AuditNotification($params)); + } return $log; } diff --git a/app/Notifications/AuditNotification.php b/app/Notifications/AuditNotification.php index 1377c29e7e..4f4638e6cd 100644 --- a/app/Notifications/AuditNotification.php +++ b/app/Notifications/AuditNotification.php @@ -4,8 +4,13 @@ namespace App\Notifications; use App\Models\Setting; use Illuminate\Bus\Queueable; +use Illuminate\Notifications\Channels\SlackWebhookChannel; use Illuminate\Notifications\Messages\SlackMessage; use Illuminate\Notifications\Notification; +use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; +use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel; +use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage; class AuditNotification extends Notification { @@ -35,10 +40,14 @@ class AuditNotification extends Notification public function via() { $notifyBy = []; - if (Setting::getSettings()->webhook_endpoint) { - $notifyBy[] = 'slack'; + if (Setting::getSettings()->webhook_selected == 'slack' || Setting::getSettings()->webhook_selected == 'general' ) { + Log::debug('use webhook'); + $notifyBy[] = SlackWebhookChannel::class; } + if (Setting::getSettings()->webhook_selected == 'microsoft' && Setting::getSettings()->webhook_endpoint) { + $notifyBy[] = MicrosoftTeamsChannel::class; + } return $notifyBy; } @@ -63,4 +72,30 @@ class AuditNotification extends Notification ->fields($fields); }); } + + public static function toMicrosoftTeams($params) + { + $item = $params['item']; + $admin_user = $params['admin']; + $note = $params['note']; + $location = $params['location']; + $setting = Setting::getSettings(); + + if(!Str::contains($setting->webhook_endpoint, 'workflows')) { + return MicrosoftTeamsMessage::create() + ->to($setting->webhook_endpoint) + ->type('success') + ->title(class_basename(get_class($params['item'])) . ' Audited') + ->addStartGroupToSection('activityText') + ->fact(trans('mail.asset'), $item) + ->fact(trans('general.administrator'), $admin_user->present()->viewUrl() . '|' . $admin_user->present()->fullName()); + } + $message = class_basename(get_class($params['item'])) . ' Audited By '.$admin_user->present()->fullName(); + $details = [ + trans('mail.asset') => htmlspecialchars_decode($item->present()->name), + trans('mail.notes') => $note ?: '', + trans('general.location') => $location ?: '', + ]; + return [$message, $details]; + } }