-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathAuditNotification.php
More file actions
154 lines (136 loc) · 5.64 KB
/
Copy pathAuditNotification.php
File metadata and controls
154 lines (136 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace App\Notifications;
use AllowDynamicProperties;
use App\Models\Setting;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
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\GoogleChat\Card;
use NotificationChannels\GoogleChat\GoogleChatChannel;
use NotificationChannels\GoogleChat\GoogleChatMessage;
use NotificationChannels\GoogleChat\Section;
use NotificationChannels\GoogleChat\Widgets\KeyValue;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;
#[AllowDynamicProperties]
class AuditNotification extends Notification implements ShouldQueue
{
use Queueable;
private $params;
/**
* Create a new notification instance.
*/
public function __construct($params)
{
//
$this->settings = Setting::getSettings();
$this->params = $params;
$item = $params['item'];
if (! $item || ! is_object($item)) {
throw new \InvalidArgumentException('Notification requires a valid item.');
}
}
/**
* Get the notification's delivery channels.
*
* @return array
*/
public function via()
{
$notifyBy = [];
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;
}
if (Setting::getSettings()->webhook_selected == 'google' && Setting::getSettings()->webhook_endpoint) {
Log::debug('using google webhook');
$notifyBy[] = GoogleChatChannel::class;
}
return $notifyBy;
}
public function toSlack()
{
$channel = ($this->settings->webhook_channel) ? $this->settings->webhook_channel : '';
return (new SlackMessage)
->success()
->content(class_basename(get_class($this->params['item'])).' '.trans('general.audited'))
->from(($this->settings->webhook_botname) ? $this->settings->webhook_botname : 'Snipe-Bot')
->to($channel)
->attachment(function ($attachment) {
$item = $this->params['item'] ?? null;
$admin_user = $this->params['admin'];
$fields = [
'By' => '<'.$admin_user->present()->viewUrl().'|'.$admin_user->display_name.'>',
];
array_key_exists('note', $this->params) && $fields['Notes'] = $this->params['note'];
array_key_exists('location', $this->params) && $fields['Location'] = $this->params['location'];
$attachment->title($item->present()->name, $item->present()->viewUrl())
->fields($fields);
});
}
public static function toMicrosoftTeams($params)
{
$item = $params['item'] ?? null;
$admin_user = $params['admin'] ?? null;
$note = $params['note'] ?? '';
$location = $params['location'] ?? '';
$setting = Setting::getSettings();
// if somehow a notification triggers without an item, bail out.
if (! $item || ! is_object($item)) {
return null;
}
if (! Str::contains($setting->webhook_endpoint, 'workflows')) {
return MicrosoftTeamsMessage::create()
->to($setting->webhook_endpoint)
->type('success')
->title(class_basename($item).' '.trans('general.audited'))
->addStartGroupToSection('activityText')
->fact(trans('mail.asset'), $item)
->fact(trans('general.administrator'), $admin_user->present()->viewUrl().'|'.$admin_user->display_name);
}
$message = class_basename(get_class($params['item'])).trans('general.audited_by').' '.$admin_user->display_name;
$details = [
trans('mail.asset') => htmlspecialchars_decode($item->display_name),
trans('mail.notes') => $note ?: '',
trans('general.location') => $location ?: '',
];
return [$message, $details];
}
public function toGoogleChat()
{
$item = $this->params['item'] ?? null;
$admin_user = $this->params['admin'] ?? null;
$note = $this->params['note'] ?? '';
$setting = $this->settings ?? Setting::getSettings();
$title = '<strong>'.class_basename($item).' '.trans('general.audited').'</strong>';
$subtitle = htmlspecialchars_decode($item->display_name ?? '');
\Log::debug('Google Chat audit payload', [
'title' => $title,
'subtitle' => $subtitle,
'admin' => $admin_user->display_name,
'note' => $note,
]);
return GoogleChatMessage::create()
->to($setting->webhook_endpoint)
->card(
Card::create()
->header($title, $subtitle)
->section(
Section::create(
KeyValue::create(
trans('general.audited_by'),
$admin_user?->display_name ?? '',
$note ?? ''
)->onClick(route('hardware.show', $item->id))
)
)
);
}
}