Skip to content

Commit e686eda

Browse files
committed
fixed slack notifications
1 parent c0bd970 commit e686eda

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

src/Config/firewall.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
'whitelist' => [env('FIREWALL_WHITELIST', '')],
88

99
'models' => [
10-
'user' => '\App\User',
11-
// 'log' => '\App\YourLogModel',
12-
// 'ip' => '\App\YourIpModel',
10+
'user' => '\App\Models\User',
11+
// 'log' => '\App\Models\YourLogModel',
12+
// 'ip' => '\App\Models\YourIpModel',
1313
],
1414

1515
'responses' => [
@@ -29,14 +29,15 @@
2929
'enabled' => env('FIREWALL_EMAIL_ENABLED', false),
3030
'name' => env('FIREWALL_EMAIL_NAME', 'Laravel Firewall'),
3131
'from' => env('FIREWALL_EMAIL_FROM', '[email protected]'),
32-
'to' => [env('FIREWALL_EMAIL_TO', '[email protected]')],
32+
'to' => env('FIREWALL_EMAIL_TO', '[email protected]'),
3333
],
3434

3535
'slack' => [
3636
'enabled' => env('FIREWALL_SLACK_ENABLED', false),
37-
'from' => env('FIREWALL_SLACK_FROM', 'Laravel Firewall'),
38-
'to' => env('FIREWALL_SLACK_TO', '#my-channel'),
3937
'emoji' => env('FIREWALL_SLACK_EMOJI', ':fire:'),
38+
'from' => env('FIREWALL_SLACK_FROM', 'Laravel Firewall'),
39+
'to' => env('FIREWALL_SLACK_TO'), // webhook url
40+
'channel' => env('FIREWALL_SLACK_CHANNEL', null), // set null to use the default channel of webhook
4041
],
4142

4243
],

src/Listeners/NotifyUsers.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Akaunting\Firewall\Events\AttackDetected as Event;
66
use Akaunting\Firewall\Notifications\AttackDetected as Notification;
7+
use Exception;
8+
use GuzzleHttp\Exception\RequestException;
9+
use Illuminate\Notifications\Notifiable;
10+
use Throwable;
711

812
class NotifyUsers
913
{
@@ -16,22 +20,34 @@ class NotifyUsers
1620
*/
1721
public function handle(Event $event)
1822
{
19-
$model = config('firewall.models.user');
23+
$notifiable = $this->getNotifiableClass();
2024

21-
if (!class_exists($model)) {
22-
return;
25+
try {
26+
$notifiable->notify(new Notification($event->log));
27+
} catch (Exception | RequestException | Throwable $e) {
28+
report($e);
2329
}
30+
}
2431

25-
$emails = config('firewall.notifications.mail.to');
32+
protected function getNotifiableClass()
33+
{
34+
return new class() {
35+
use Notifiable;
2636

27-
foreach ($emails as $email) {
28-
$user = $model::where('email', $email)->first();
37+
public function routeNotificationForMail()
38+
{
39+
return config('firewall.notifications.mail.to');
40+
}
2941

30-
if (empty($user)) {
31-
continue;
42+
public function routeNotificationForSlack()
43+
{
44+
return config('firewall.notifications.slack.to');
3245
}
3346

34-
$user->notify(new Notification($event->log));
35-
}
47+
public function getKey()
48+
{
49+
return 1;
50+
}
51+
};
3652
}
3753
}

src/Notifications/AttackDetected.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function via($notifiable)
4848
$channels = [];
4949

5050
foreach ($this->notifications as $channel => $settings) {
51-
if (!$settings['enabled']) {
51+
if (empty($settings['enabled'])) {
5252
continue;
5353
}
5454

@@ -69,7 +69,7 @@ public function toMail($notifiable)
6969
$domain = request()->getHttpHost();
7070

7171
$subject = trans('firewall::notifications.mail.subject', [
72-
'domain' => $domain
72+
'domain' => $domain,
7373
]);
7474

7575
$message = trans('firewall::notifications.mail.message', [
@@ -100,7 +100,7 @@ public function toSlack($notifiable)
100100
return (new SlackMessage)
101101
->error()
102102
->from($this->notifications['slack']['from'], $this->notifications['slack']['emoji'])
103-
->to($this->notifications['slack']['to'])
103+
->to($this->notifications['slack']['channel'])
104104
->content($message)
105105
->attachment(function ($attachment) {
106106
$attachment->fields([

0 commit comments

Comments
 (0)