2017-03-22 09:59:39 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* application-update.php
|
|
|
|
*
|
|
|
|
* Handle application enable disable from ajax
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-03-22 09:59:39 -05:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2017-03-22 09:59:39 -05:00
|
|
|
* @copyright 2017 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
2023-09-06 16:34:39 -05:00
|
|
|
use App\Models\Application;
|
|
|
|
|
2019-08-05 14:16:05 -05:00
|
|
|
if (! Auth::user()->hasGlobalAdmin()) {
|
2017-03-22 09:59:39 -05:00
|
|
|
$status = ['status' => 1, 'message' => 'You need to be admin'];
|
|
|
|
} else {
|
|
|
|
$device_id = $_POST['device_id'];
|
|
|
|
$app = $_POST['application'];
|
|
|
|
|
|
|
|
if (! isset($app) && validate_device_id($device_id) === false) {
|
|
|
|
$status = ['status' => 1, 'message' => 'Error with data'];
|
|
|
|
} else {
|
|
|
|
$status = ['status' => 1, 'message' => 'Database update failed'];
|
2023-09-06 16:34:39 -05:00
|
|
|
$app = Application::withTrashed()->firstOrNew(['device_id' => $device_id, 'app_type' => $app]);
|
2017-03-22 09:59:39 -05:00
|
|
|
if ($_POST['state'] == 'true') {
|
2023-09-06 16:34:39 -05:00
|
|
|
if ($app->trashed()) {
|
|
|
|
$app->restore();
|
|
|
|
}
|
|
|
|
if ($app->save()) {
|
2017-03-22 09:59:39 -05:00
|
|
|
log_event("Application enabled by user: $app", $device_id, 'application', 1);
|
|
|
|
$status = ['status' => 0, 'message' => 'Application enabled'];
|
2023-09-06 16:34:39 -05:00
|
|
|
} else {
|
|
|
|
$status = ['status' => 1, 'message' => 'Database update for enabling the application failed'];
|
2017-03-22 09:59:39 -05:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-06 16:34:39 -05:00
|
|
|
$app->delete();
|
|
|
|
if ($app->save()) {
|
2017-03-22 09:59:39 -05:00
|
|
|
log_event("Application disabled by user: $app", $device_id, 'application', 3);
|
|
|
|
$status = ['status' => 0, 'message' => 'Application disabled'];
|
2023-09-06 16:34:39 -05:00
|
|
|
} else {
|
|
|
|
$status = ['status' => 1, 'message' => 'Database update for disabling the application failed'];
|
2017-03-22 09:59:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header('Content-Type: application/json');
|
2021-03-04 07:55:41 -06:00
|
|
|
echo json_encode($status, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|