Copy Dashboard to other User (#11989)

* Copy Dashboard to other User

* travis fix

* only allow Dashboard Copy to Admins

* rewrite query

* move Controller to Eloquent

* optimize row copy

* travis ...

* move copy field to edit section

* code changes

* code changes

* limit copy feature to admins

* .
This commit is contained in:
SourceDoctor
2021-01-29 15:28:47 +01:00
committed by GitHub
parent 5379176bd8
commit 17f5a3f232
4 changed files with 148 additions and 2 deletions

View File

@@ -0,0 +1,81 @@
<?php
/**
* CopyDashboardController.php
*
* -Description-
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @link http://librenms.org
* @copyright 2020 Thomas Berberich
* @author Thomas Berberich <sourcehhdoctor@gmail.com>
*/
namespace App\Http\Controllers\Form;
use App\Http\Controllers\Controller;
use App\Models\Dashboard;
use App\Models\UserWidget;
use Auth;
use Illuminate\Http\Request;
class CopyDashboardController extends Controller
{
public function store(Request $request)
{
$target_user_id = $request->get('target_user_id');
$dashboard_id = $request->get('dashboard_id');
$dashboard = Dashboard::where(['dashboard_id' => $dashboard_id, 'user_id' => Auth::id()])->first();
$success = true;
if ((empty($dashboard)) || (empty($target_user_id))) {
$success = false;
}
if ($success) {
$dashboard_copy = $dashboard->replicate()->fill([
'user_id' => $target_user_id,
'dashboard_name' => $dashboard['dashboard_name'] .= '_' . Auth::user()->username,
]);
$success = $dashboard_copy->save();
}
if ($success) {
$widgets = UserWidget::where(['dashboard_id' => $dashboard_id, 'user_id' => Auth::id()])->get();
foreach ($widgets as $widget) {
$widget_copy = $widget->replicate()->fill([
'user_id' => $target_user_id,
'dashboard_id' => $dashboard_copy->dashboard_id,
]);
$success &= $widget_copy->save();
}
}
if ($success) {
$status = 'ok';
$message = 'Dashboard copied';
} else {
$status = 'error';
$message = 'ERROR: Could not copy Dashboard';
}
return response()->json([
'status' => $status,
'message' => $message,
]);
}
}

View File

@@ -8,6 +8,7 @@ use App\Models\Device;
use App\Models\Port;
use App\Models\Service;
use App\Models\Syslog;
use App\Models\User;
use App\Models\UserPref;
use App\Models\Widget;
use Auth;
@@ -105,7 +106,15 @@ class OverviewController extends Controller
$hide_dashboard_editor = UserPref::getPref($user, 'hide_dashboard_editor');
$widgets = Widget::select('widget_id', 'widget_title')->orderBy('widget_title')->get();
return view('overview.default', compact('bare', 'dash_config', 'dashboard', 'hide_dashboard_editor', 'user_dashboards', 'shared_dashboards', 'widgets'));
$user_list = [];
if ($user->can('manage', User::class)) {
$user_list = User::select(['username', 'user_id'])
->where('user_id', '!=', $user->user_id)
->orderBy('username')
->get();
}
return view('overview.default', compact('bare', 'dash_config', 'dashboard', 'hide_dashboard_editor', 'user_dashboards', 'shared_dashboards', 'widgets', 'user_list'));
}
public function simple(Request $request)

View File

@@ -94,6 +94,19 @@
</div>
</form>
</div>
@if (count($user_list) and auth()->user()->isAdmin())
<div class="btn-group btn-lg" style="margin-top:5px;position:absolute;right:0px;">
<div class="btn-group">
<select class="form-control" id="dashboard_copy_target" name="dashboard_copy_target" onchange="dashboard_copy_user_select()">
<option value="-1" selected> Copy Dashboard to </option>
@foreach ($user_list as $user)
<option value="{{ $user->user_id }}">{{ $user->username }}</option>
@endforeach
</select>
</div>
<button disabled id="do_copy_dashboard" class="btn btn-primary" onclick="dashboard_copy(this)" data-toggle="tooltip" data-container="body" data-placement="top" title="Copy Dashboard"><i class="fa fa-copy fa-fw"></i></button>
</div>
@endif
</div>
</div>
<!-- End Dashboard-Settings -->
@@ -473,6 +486,49 @@
});
}
@if (auth()->user()->isAdmin())
function dashboard_copy_user_select() {
var button_disabled = true;
if (document.getElementById("dashboard_copy_target").value > 0) {
button_disabled = false;
}
$("#do_copy_dashboard").prop('disabled', button_disabled);
}
function dashboard_copy(data) {
var target_user_id = document.getElementById("dashboard_copy_target").value;
var dashboard_id = {{ $dashboard->dashboard_id }};
var username = $("#dashboard_copy_target option:selected").text().trim();
if (target_user_id == -1) {
toastr.warning('No target selected to copy Dashboard to');
} else {
if (! confirm("Do you really want to copy this Dashboard to User '" + username + "'?")) {
return;
}
$.ajax({
type: 'POST',
url: '{{ url('/ajax/form/copy-dashboard') }}',
data: {target_user_id: target_user_id, dashboard_id: dashboard_id},
dataType: "json",
success: function (data) {
if( data.status == "ok" ) {
toastr.success(data.message);
} else {
toastr.error(data.message);
}
},
error: function(data) {
toastr.error(data.message);
}
});
$("#dashboard_copy_target option:eq(-1)").prop('selected', true);
dashboard_copy_user_select();
}
}
@endif
function widget_dom(data) {
dom = '<li id="'+data.user_widget_id+'" data-type="'+data.widget+'" data-settings="0">'+
'<header class="widget_header"><span id="widget_title_'+data.user_widget_id+'">'+data.title+
@@ -610,6 +666,5 @@
$('#dashboard_name').val('Default');
dashboard_add($('#add_form'));
@endif
</script>
@endpush

View File

@@ -90,6 +90,7 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () {
// form ajax handlers, perhaps should just be page controllers
Route::group(['prefix' => 'form', 'namespace' => 'Form'], function () {
Route::resource('widget-settings', 'WidgetSettingsController');
Route::post('copy-dashboard', 'CopyDashboardController@store');
});
// js select2 data controllers