Files
librenms-librenms/app/Http/Controllers/AlertController.php
Tony Murray 0b8b97bb68 Push Notifications (Mobile and PC) (#13277)
* Update manifest and add service worker
cleanup icons a bit

* Push notifications WIP

* navigate working

* cleanup

* acknowledge wired up

* Set VAPID keys on composer install

* Component to control notification permissions.

* Allow all user option to validate

* Enable on browser load if transport exists.

* Check for transport before showing user permissions
translations

* Documentation

* style fixes

* access via the attribute model

* fix alerting test

* update schema

* cleanup subscription on disable

* non-configurable db and table for webpush subscriptions (respect system connection)

* revert AlertTransport change
hopefully phpstan can figure it out

* phpstan fixes

* Support custom details display

* Match transport names to brand's preferred display

* less duplicate id errors

* Tests are done in Laravel code now so
remove legacy function usage... could be better, but ok

* Style fixes

* Style fixes 2

* Fix alert test

* Doc updates requires HTTPS and GMP

* unregister subscription when permission is set to denied

* cleanup after user deletion

* delete the right thing

* fix whitespace

* update install docs to include php-gmp

* suggest ext-gmp

* update javascript

* Update functions.php

Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>
2021-10-06 07:29:47 -05:00

59 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Alert;
use Illuminate\Http\Request;
use LibreNMS\Config;
use Log;
class AlertController extends Controller
{
public function ack(Request $request, Alert $alert): \Illuminate\Http\JsonResponse
{
$this->validate($request, [
'state' => 'required|int',
'ack_msg' => 'nullable|string',
'ack_until_clear' => 'nullable|in:0,1,true,false',
]);
$state = $request->get('state');
$state_description = '';
if ($state == 2) {
$alert->state = 1;
$state_description = 'UnAck';
$alert->open = 1;
} elseif ($state >= 1) {
$alert->state = 2;
$state_description = 'Ack';
$alert->open = 1;
}
$info = $alert->info;
$info['until_clear'] = filter_var($request->get('ack_until_clear'), FILTER_VALIDATE_BOOLEAN);
$alert->info = $info;
$timestamp = date(Config::get('dateformat.long'));
$username = $request->user()->username;
$ack_msg = $request->get('ack_msg');
$alert->note = trim($alert->note . PHP_EOL . "$timestamp - $state_description ($username) " . $ack_msg);
if ($alert->save()) {
if (in_array($state, [2, 22])) {
$rule_name = $alert->rule->name;
Log::event("$username acknowledged alert $rule_name note: $ack_msg", $alert->device_id, 'alert', 2, $alert->id);
}
return response()->json([
'message' => "Alert {$state_description}nowledged.",
'status' => 'ok',
]);
}
return response()->json([
'message' => 'Alert has not been acknowledged.',
'status' => 'error',
]);
}
}