mirror of
				https://github.com/librenms/librenms.git
				synced 2024-10-07 16:52:45 +00:00 
			
		
		
		
	Fallback to default Poller Group on delete (#11278)
* Fallback to default Poller Group on delete * rewrite to eloquent * replace column remove with page reload (refresh everything) * change access verification check method * . * Update PollerGroups.php Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
		
							
								
								
									
										20
									
								
								app/Http/Controllers/PollerGroupController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								app/Http/Controllers/PollerGroupController.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Http\Controllers;
 | 
			
		||||
 | 
			
		||||
use App\Models\Device;
 | 
			
		||||
use App\Models\PollerGroups;
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
 | 
			
		||||
class PollerGroupController extends Controller
 | 
			
		||||
{
 | 
			
		||||
    public function destroy(Request $request, PollerGroups $pollergroup)
 | 
			
		||||
    {
 | 
			
		||||
        if ($request->user()->isAdmin()) {
 | 
			
		||||
            $pollergroup->delete();
 | 
			
		||||
            return response()->json(['status' => 'success']);
 | 
			
		||||
        } else {
 | 
			
		||||
            return response()->json(['status' => 'failure']);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -33,6 +33,20 @@ class PollerGroups extends Model
 | 
			
		||||
    protected $primaryKey = 'id';
 | 
			
		||||
    protected $fillable = ['group_name', 'descr'];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Initialize this class
 | 
			
		||||
     */
 | 
			
		||||
    public static function boot()
 | 
			
		||||
    {
 | 
			
		||||
        parent::boot();
 | 
			
		||||
 | 
			
		||||
        static::deleting(function (PollerGroups $pollergroup) {
 | 
			
		||||
            // handle device pollergroup fallback to default poller
 | 
			
		||||
            $default_poller_id = \LibreNMS\Config::get('distributed_poller_group');
 | 
			
		||||
            $pollergroup->devices()->update(['poller_group' => $default_poller_id]);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function devices()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->hasMany('App\Models\Device', 'poller_group', 'id');
 | 
			
		||||
 
 | 
			
		||||
@@ -1,35 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * LibreNMS
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
 | 
			
		||||
 *
 | 
			
		||||
 * 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.  Please see LICENSE.txt at the top level of
 | 
			
		||||
 * the source code distribution for details.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
header('Content-type: text/plain');
 | 
			
		||||
 | 
			
		||||
if (!Auth::user()->hasGlobalAdmin()) {
 | 
			
		||||
    die('ERROR: You need to be admin');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if (!is_numeric($_POST['group_id'])) {
 | 
			
		||||
    echo 'error with data';
 | 
			
		||||
    exit;
 | 
			
		||||
} else {
 | 
			
		||||
    if ($_POST['confirm'] == 'yes') {
 | 
			
		||||
        $delete = dbDelete('poller_groups', '`id` = ?', array($_POST['group_id']));
 | 
			
		||||
        if ($delete > '0') {
 | 
			
		||||
            echo 'Poller group has been removed';
 | 
			
		||||
            exit;
 | 
			
		||||
        } else {
 | 
			
		||||
            echo 'An error occurred removing the Poller group';
 | 
			
		||||
            exit;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -121,18 +121,17 @@ $('#confirm-delete').on('show.bs.modal', function(e) {
 | 
			
		||||
 | 
			
		||||
$('#group-removal').click('', function(e) {
 | 
			
		||||
    e.preventDefault();
 | 
			
		||||
    group_id = $("#group_id").val();
 | 
			
		||||
    groupId = $("#group_id").val();
 | 
			
		||||
    $.ajax({
 | 
			
		||||
        type: "POST",
 | 
			
		||||
        url: "ajax_form.php",
 | 
			
		||||
        data: $('form.remove_group_form').serialize() ,
 | 
			
		||||
        type: 'DELETE',
 | 
			
		||||
        url: "ajax/pollergroup/" + groupId,
 | 
			
		||||
        success: function(msg) {
 | 
			
		||||
            $("#thanks").html('<div class="alert alert-info">'+msg+'</div>');
 | 
			
		||||
            toastr.success('@lang('Poller Group deleted')');
 | 
			
		||||
            $("#confirm-delete").modal('hide');
 | 
			
		||||
            $("#"+group_id).remove();
 | 
			
		||||
            location.reload(1);
 | 
			
		||||
        },
 | 
			
		||||
        error: function() {
 | 
			
		||||
            $("#thanks").html('<div class="alert alert-info">An error occurred removing the token.</div>');
 | 
			
		||||
        error: function(e) {
 | 
			
		||||
            toastr.error('@lang('Failed to delete Poller Group'): ' + e.statusText)
 | 
			
		||||
            $("#confirm-delete").modal('hide');
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 
 | 
			
		||||
@@ -67,7 +67,7 @@ Route::group(['middleware' => ['auth', '2fa'], 'guard' => 'auth'], function () {
 | 
			
		||||
    Route::group(['prefix' => 'ajax'], function () {
 | 
			
		||||
        // page ajax controllers
 | 
			
		||||
        Route::resource('location', 'LocationController', ['only' => ['update', 'destroy']]);
 | 
			
		||||
 | 
			
		||||
        Route::resource('pollergroup', 'PollerGroupController', ['only' => ['destroy']]);
 | 
			
		||||
        // misc ajax controllers
 | 
			
		||||
        Route::group(['namespace' => 'Ajax'], function () {
 | 
			
		||||
            Route::post('set_map_group', 'AvailabilityMapController@setGroup');
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user