Issues (2963)

Http/Controllers/Form/CopyDashboardController.php (1 issue)

1
<?php
2
/**
3
 * CopyDashboardController.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2020  Thomas Berberich
23
 * @author     Thomas Berberich <[email protected]>
24
 */
25
26
namespace App\Http\Controllers\Form;
27
28
use App\Http\Controllers\Controller;
29
use App\Models\Dashboard;
30
use App\Models\UserWidget;
31
use Illuminate\Http\Request;
32
use Illuminate\Support\Facades\Auth;
33
34
class CopyDashboardController extends Controller
35
{
36
    public function store(Request $request)
37
    {
38
        $target_user_id = $request->get('target_user_id');
39
        $dashboard_id = $request->get('dashboard_id');
40
41
        $dashboard = Dashboard::where(['dashboard_id' => $dashboard_id, 'user_id' => Auth::id()])->first();
42
43
        $success = true;
44
45
        if ((empty($dashboard)) || (empty($target_user_id))) {
46
            $success = false;
47
        }
48
49
        if ($success) {
50
            $dashboard_copy = $dashboard->replicate()->fill([
51
                'user_id' => $target_user_id,
52
                'dashboard_name' => $dashboard['dashboard_name'] .= '_' . Auth::user()->username,
0 ignored issues
show
Accessing username on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
            ]);
54
            $success = $dashboard_copy->save();
55
        }
56
57
        if ($success && isset($dashboard_copy)) {
58
            $widgets = UserWidget::where(['dashboard_id' => $dashboard_id, 'user_id' => Auth::id()])->get();
59
60
            foreach ($widgets as $widget) {
61
                $widget_copy = $widget->replicate()->fill([
62
                    'user_id' => $target_user_id,
63
                    'dashboard_id' => $dashboard_copy->dashboard_id,
64
                ]);
65
                $success &= $widget_copy->save();
66
            }
67
        }
68
69
        if ($success) {
70
            $status = 'ok';
71
            $message = 'Dashboard copied';
72
        } else {
73
            $status = 'error';
74
            $message = 'ERROR: Could not copy Dashboard';
75
        }
76
77
        return response()->json([
78
            'status' => $status,
79
            'message' => $message,
80
        ]);
81
    }
82
}
83