1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinzar\Firewall\Notifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
9
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
10
|
|
|
|
11
|
|
|
class AttackDetected extends Notification implements ShouldQueue |
12
|
|
|
{ |
13
|
|
|
use Queueable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The log model. |
17
|
|
|
* |
18
|
|
|
* @var object |
19
|
|
|
*/ |
20
|
|
|
public $log; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The notification config. |
24
|
|
|
* |
25
|
|
|
* @var object |
26
|
|
|
*/ |
27
|
|
|
public $notifications; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a notification instance. |
31
|
|
|
* |
32
|
|
|
* @param object $log |
33
|
|
|
*/ |
34
|
|
|
public function __construct($log) |
35
|
|
|
{ |
36
|
|
|
$this->log = $log; |
37
|
|
|
$this->notifications = config('firewall.middleware.' . $log->middleware . '.notifications', config('firewall.notifications')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the notification's channels. |
42
|
|
|
* |
43
|
|
|
* @param mixed $notifiable |
44
|
|
|
* @return array|string |
45
|
|
|
*/ |
46
|
|
|
public function via($notifiable) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$channels = []; |
49
|
|
|
|
50
|
|
|
foreach ($this->notifications as $channel => $settings) { |
51
|
|
|
if (!$settings['enabled']) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$channels[] = $channel; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $channels; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build the mail representation of the notification. |
63
|
|
|
* |
64
|
|
|
* @param mixed $notifiable |
65
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage |
66
|
|
|
*/ |
67
|
|
|
public function toMail($notifiable) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$domain = request()->getHttpHost(); |
70
|
|
|
|
71
|
|
|
$subject = trans('firewall::notifications.mail.subject', [ |
72
|
|
|
'domain' => $domain |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$message = trans('firewall::notifications.mail.message', [ |
76
|
|
|
'domain' => $domain, |
77
|
|
|
'middleware' => ucfirst($this->log->middleware), |
78
|
|
|
'ip' => $this->log->ip, |
79
|
|
|
'url' => $this->log->url, |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
return (new MailMessage) |
83
|
|
|
->from($this->notifications['mail']['from'], $this->notifications['mail']['name']) |
84
|
|
|
->subject($subject) |
85
|
|
|
->line($message); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the Slack representation of the notification. |
90
|
|
|
* |
91
|
|
|
* @param mixed $notifiable |
92
|
|
|
* @return SlackMessage |
93
|
|
|
*/ |
94
|
|
|
public function toSlack($notifiable) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$message = trans('firewall::notifications.slack.message', [ |
97
|
|
|
'domain' => request()->getHttpHost(), |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
return (new SlackMessage) |
101
|
|
|
->error() |
102
|
|
|
->from($this->notifications['slack']['from'], $this->notifications['slack']['emoji']) |
103
|
|
|
->to($this->notifications['slack']['to']) |
104
|
|
|
->content($message) |
105
|
|
|
->attachment(function ($attachment) { |
106
|
|
|
$attachment->fields([ |
107
|
|
|
'IP' => $this->log->ip, |
108
|
|
|
'Type' => ucfirst($this->log->middleware), |
109
|
|
|
'User ID' => $this->log->user_id, |
110
|
|
|
'URL' => $this->log->url, |
111
|
|
|
]); |
112
|
|
|
}); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.