Completed
Push — develop ( 22ed41...b37d35 )
by Tony
05:09
created

UserController::preferences()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 34
rs 8.439
cc 5
eloc 21
nc 8
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Dingo\Api\Http;
6
use Dingo\Api\Routing\Helpers;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\RedirectResponse;
9
use Route;
10
use Hash;
11
use App\User;
12
13
class UserController extends Controller
14
{
15
    use Helpers;
16
17
    public function __construct(Request $request) {
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
        $this->middleware('auth');
19
    }
20
21
    public function preferences(Request $request) {
22
23
        $method = $request->method();
24
        $updated = false;
25
        if ($request->user()->hasGlobalRead() === true)
26
        {
27
            $devices = [];
28
            $ports   = [];
29
        }
30
        else {
31
            $devices = User::find($request->user()->user_id)->devices()->count();
32
            $ports   = User::find($request->user()->user_id)->ports()->count();
33
        }
34
35
        if ($method === "POST")
36
        {
37
            $this->validate($request, [
38
                'current_password' => 'required|max:255',
39
                'new_password' => 'required|min:8|max:255',
40
                'repeat_password' => 'required|same:new_password|min:8|max:255',
41
            ]);
42
            if (!Hash::check($request->current_password, $request->user()->password))
43
            {
44
                return back()->withInput()->withErrors(['current_password'=>'Current password is incorrect']);
45
            }
46
            $user = User::where('user_id', $request->user()->user_id)->first();
47
            $user->password = Hash::make($request->new_password);
48
            if ($user->save())
49
            {
50
                $updated = true;
51
            }
52
        }
53
        return view('users.preferences', ['updated' => $updated, 'devices' => $devices, 'ports' => $ports]);
54
    }
55
}
56