Completed
Push — develop ( 1eb861...f7494f )
by Neil
10s
created

UserDeviceController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 6
c 3
b 2
f 0
lcom 0
cbo 6
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 7 1
A create() 0 6 1
A store() 0 16 4
1
<?php
2
/**
3
 * UserDeviceController.php
4
 *
5
 * Controls the user device relationship
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 <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Controllers;
27
28
use App\Http\Requests\AdminOnlyRequest;
29
use App\Models\Device;
30
use App\Models\User;
31
32
class UserDeviceController extends Controller
33
{
34
    public function create(AdminOnlyRequest $request, $user_id)
35
    {
36
        $user = User::with('devices')->find($user_id);
37
38
        return view('users.devices-create')->withUser($user);
39
    }
40
41
    /**
42
     * @param AdminOnlyRequest $request
43
     * @param $user_id
44
     * @return mixed
45
     */
46
    public function store(AdminOnlyRequest $request, $user_id)
47
    {
48
        $user = User::find($user_id);
49
50
        $device_ids = $request->input('devices');
51
52
        if (!is_array($device_ids) || count($device_ids) == 0) {
53
            return redirect()->back();
54
        }
55
56
        foreach ($device_ids as $device_id) {
57
            $device = Device::find($device_id);
58
            $user->devices()->attach($device);
59
        }
60
        return redirect()->back()->with(['type' => 'success', 'message' => trans('user.text.devicesadded')]);
61
    }
62
63
    /**
64
     * @param AdminOnlyRequest $request
65
     * @param $user_id
66
     * @param $device_id
67
     * @return mixed
68
     */
69
    public function destroy(AdminOnlyRequest $request, $user_id, $device_id)
70
    {
71
        $user = User::find($user_id);
72
        $device = Device::find($device_id);
73
        $user->devices()->detach($device);
74
        return redirect()->back()->with(['type' => 'success', 'message' => trans('user.text.deviceremoved', ['hostname' => $device->hostname])]);
75
    }
76
}
77