Issues (2963)

includes/html/forms/reset-port-state.inc.php (1 issue)

1
<?php
2
3
/**
4
 * reset-port-state.inc.php
5
 *
6
 * LibreNMS form for reseting port state
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 *
21
 * @link       https://www.librenms.org
22
 *
23
 * @copyright  2021 Adam Bishop
24
 * @author     Adam Bishop <[email protected]>
25
 */
26
27
use App\Models\Device;
28
29
if (! Auth::user()->hasGlobalAdmin()) {
0 ignored issues
show
The method hasGlobalAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
if (! Auth::user()->/** @scrutinizer ignore-call */ hasGlobalAdmin()) {
Loading history...
30
    $response = [
31
        'status'  => 'error',
32
        'message' => 'Need to be admin',
33
    ];
34
    echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
35
    exit;
36
}
37
38
if (isset($_POST['device_id'])) {
39
    if (! is_numeric($_POST['device_id'])) {
40
        $status = 'error';
41
        $message = 'Invalid device id ' . $_POST['device_id'];
42
    } else {
43
        $device = Device::find($_POST['device_id']);
44
45
        log_event('Port state history reset by ' . Auth::user()->username, $device);
46
47
        try {
48
            foreach ($device->ports()->get() as $port) {
49
                $port->ifSpeed_prev = null;
50
                $port->ifOperStatus_prev = null;
51
                $port->ifAdminStatus_prev = null;
52
53
                $port->save();
54
            }
55
            $status = 'ok';
56
            $message = 'Port state cleared successfully';
57
        } catch (Exception $e) {
58
            $status = 'error';
59
            $message = 'Clearing port state failed: $e';
60
        }
61
    }
62
} else {
63
    $status = 'Error';
64
    $message = 'Undefined POST keys received';
65
}
66
67
$output = [
68
    'status'  => $status,
69
    'message' => $message,
70
];
71
72
header('Content-type: application/json');
73
echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
74