Merge pull request #16414 from Godmartinz/Audit_error_fix

Adds audit notification for MS Teams
This commit is contained in:
snipe 2025-03-04 22:54:32 +00:00 committed by GitHub
commit bc618fcef4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 47 additions and 3 deletions

View file

@ -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,
];
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;
}

View file

@ -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];
}
}