|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Audit\Notifications\Logs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
|
6
|
|
|
use Illuminate\Notifications\Notification; |
|
7
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
8
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
|
9
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
|
10
|
|
|
use Audit\Models\Logs\Finger; |
|
11
|
|
|
|
|
12
|
|
|
class LogWritten extends Notification implements ShouldQueue |
|
13
|
|
|
{ |
|
14
|
|
|
use Queueable; |
|
15
|
|
|
|
|
16
|
|
|
private $laraLogsLog; |
|
17
|
|
|
|
|
18
|
|
|
private $requestInfo; |
|
19
|
|
|
|
|
20
|
|
|
private $notificationLevels; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Create a new notification instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
|
|
|
|
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Finger $laraLogsLog) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->laraLogsLog = $laraLogsLog; |
|
30
|
|
|
|
|
31
|
|
|
$this->requestInfo = array( |
|
32
|
|
|
'id' => $laraLogsLog->id, |
|
33
|
|
|
'level' => $laraLogsLog->level, |
|
34
|
|
|
'message' => $laraLogsLog->message, |
|
35
|
|
|
'user_id' => $laraLogsLog->user_id ? $laraLogsLog->user_id : null, |
|
36
|
|
|
'email' => $laraLogsLog->email ? $laraLogsLog->email : null |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
$this->notificationLevels = array( |
|
40
|
|
|
'error' => [ |
|
41
|
|
|
'emergency', |
|
42
|
|
|
'alert', |
|
43
|
|
|
'critical', |
|
44
|
|
|
'error' |
|
45
|
|
|
], |
|
46
|
|
|
'warning' => [ |
|
47
|
|
|
'warning', |
|
48
|
|
|
'notice' |
|
49
|
|
|
], |
|
50
|
|
|
'success' => [ |
|
51
|
|
|
'info', |
|
52
|
|
|
'debug' |
|
53
|
|
|
] |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the notification's delivery channels. |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $notifiable |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public function via($notifiable) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
switch($notifiable->notify_by) { |
|
66
|
|
|
case 'email': |
|
67
|
|
|
return ['mail']; |
|
68
|
|
|
break; |
|
|
|
|
|
|
69
|
|
|
case 'slack': |
|
70
|
|
|
return ['slack']; |
|
71
|
|
|
break; |
|
|
|
|
|
|
72
|
|
|
case 'email_slack': |
|
73
|
|
|
return ['mail', 'slack']; |
|
74
|
|
|
break; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the mail representation of the notification. |
|
80
|
|
|
* |
|
81
|
|
|
* @param mixed $notifiable |
|
82
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage |
|
83
|
|
|
*/ |
|
84
|
|
|
public function toMail($notifiable) |
|
85
|
|
|
{ |
|
86
|
|
|
$content = 'A log on ' . url('/') . ' has been written to.'; |
|
87
|
|
|
if($notifiable->filter !== '*') { |
|
88
|
|
|
$content .= " You're being notified because the log contains `" . $notifiable->filter . "`"; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$status = 'success'; |
|
92
|
|
View Code Duplication |
foreach($this->notificationLevels as $triggerLevel => $childLevels) { |
|
|
|
|
|
|
93
|
|
|
if(in_array($this->requestInfo['level'], $childLevels)) { |
|
94
|
|
|
$status = $triggerLevel; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$fields = array( |
|
99
|
|
|
'Level' => $this->requestInfo['level'], |
|
100
|
|
|
'Message' => $this->requestInfo['message'] |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
if($this->requestInfo['user_id']) { |
|
104
|
|
|
$fields['User ID'] = $this->requestInfo['user_id']; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if($this->requestInfo['email']) { |
|
108
|
|
|
$fields['Associated Email'] = $this->requestInfo['email']; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$statusColors = array( |
|
112
|
|
|
'success' => '#00B945', |
|
113
|
|
|
'warning' => '#ff9f00', |
|
114
|
|
|
'error' => '#BC001A' |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
return (new MailMessage) |
|
118
|
|
|
->subject(env('LARALogs_LOG_SUBJECT', '[LaraLogs Alert] The application log has been written to')) |
|
119
|
|
|
->from(env('LARALogs_FROM_EMAIL', '[email protected]'), env('LARALogs_FROM_NAME', 'LaraLogs Alerts')) |
|
120
|
|
|
->view( |
|
121
|
|
|
'laraLogs::emails.log-written', [ |
|
122
|
|
|
'requestInfo' => $this->requestInfo, |
|
123
|
|
|
'content' => $content, |
|
124
|
|
|
'alertColor' => $statusColors[$status] |
|
125
|
|
|
] |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function toSlack($notifiable) |
|
130
|
|
|
{ |
|
131
|
|
|
$content = 'A log on ' . url('/') . ' has been written to.'; |
|
132
|
|
|
if($notifiable->filter !== '*') { |
|
133
|
|
|
$content .= " You're being notified because the log contains `" . $notifiable->filter . "`"; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$status = 'success'; |
|
137
|
|
View Code Duplication |
foreach($this->notificationLevels as $triggerLevel => $childLevels) { |
|
|
|
|
|
|
138
|
|
|
if(in_array($this->requestInfo['level'], $childLevels)) { |
|
139
|
|
|
$status = $triggerLevel; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$fields = array( |
|
144
|
|
|
'Level' => $this->requestInfo['level'], |
|
145
|
|
|
'Message' => $this->requestInfo['message'] |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
if($this->requestInfo['user_id']) { |
|
149
|
|
|
$fields['User ID'] = $this->requestInfo['user_id']; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if($this->requestInfo['email']) { |
|
153
|
|
|
$fields['Associated Email'] = $this->requestInfo['email']; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$requestInfo = $this->requestInfo; |
|
157
|
|
|
|
|
158
|
|
|
return (new SlackMessage) |
|
159
|
|
|
->{ $status }() |
|
160
|
|
|
->attachment( |
|
161
|
|
|
function ($attachment) use ($requestInfo, $fields, $content) { |
|
162
|
|
|
$attachment->title('Log #' . $requestInfo['id'], route('laraLogs::logs.show', $requestInfo['id'])) |
|
163
|
|
|
->content($content) |
|
164
|
|
|
->fields($fields); |
|
165
|
|
|
} |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the array representation of the notification. |
|
171
|
|
|
* |
|
172
|
|
|
* @param mixed $notifiable |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
public function toArray($notifiable) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
|
|
return [ |
|
178
|
|
|
// |
|
179
|
|
|
]; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.