Passed
Push — dev5a ( 352680...01b7fe )
by Ron
07:33
created

UserController::changePassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\Facades\Log;
7
use App\Domains\Roles\GetRoles;
8
use App\Domains\User\GetUserDetails;
9
use App\Domains\User\GetUserList;
10
use App\Domains\User\SetUserDetails;
11
use App\Http\Controllers\Controller;
12
use App\Http\Requests\Admin\ChangeUserPasswordRequest;
13
use App\Http\Requests\Admin\EditUserRequest;
14
use App\Http\Requests\Admin\NewUserRequest;
15
use Illuminate\Http\Request;
16
17
class UserController extends Controller
18
{
19
    //  Check if a username is in use
20 4
    public function checkUser($username, $type)
21
    {
22 4
        $user = (new GetUserDetails)->checkForDuplicate($type, $username);
23
24 4
        if(!$user)
25
        {
26 2
            return response()->json(['duplicate' => false]);
27
        }
28
29 2
        return response()->json([
30 2
            'duplicate' => true,
31 2
            'user'      => $user->full_name,
32 2
            'username'  => $user->username,
33 2
            'active'    => $user->deleted_at == null ? 1 : 0,
34
        ]);
35
    }
36
37
    //  Show the add user form
38 2
    public function create()
39
    {
40 2
        return view('admin.newUser', [
41 2
            'roles' => (new GetRoles)->getRoleList()->makeHidden('allow_edit'),
42
        ]);
43
    }
44
45
    //  Submit the add user form
46 2
    public function store(NewUserRequest $request)
47
    {
48 2
        $newID = (new SetUserDetails)->createUser($request);
49 2
        Log::notice('New user created by '.Auth::user()->full_name.'. New User ID - '.$newID.'. User Data - ', $request->toArray());
50
51 2
        return response()->json(['success' => true]);
52
    }
53
54
    //  List all active users
55 2
    public function listActive()
56
    {
57 2
        return view('admin.userList', [
58 2
            'userList' => (new GetUserList)->getActiveUsers(),
59
            'active'   => true,
60
        ]);
61
    }
62
63
    //  Form to edit an existing user
64 4
    public function edit($userID)
65
    {
66
        //  Before showing user form, verify that the user does not have more permission
67 4
        $user = (new GetUserDetails($userID))->getUserData()->makeVisible(['role_id', 'user_id']);
68 4
        if($user->role_id < Auth::user()->role_id)
69
        {
70 2
            return abort(403, 'You cannot update a user with more permissions than you');
1 ignored issue
show
Bug introduced by
Are you sure the usage of abort(403, 'You cannot u... permissions than you') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        }
72
73 2
        return view('admin.userEdit', [
74 2
            'details' => $user,
75 2
            'roles' => (new GetRoles)->getRoleList()->makeHidden('allow_edit'),
76
        ]);
77
    }
78
79
    //  Submit the edit user form
80 4
    public function update(EditUserRequest $request, $userID)
81
    {
82
        //  Before submitting user form, verify that the user does not have more permission
83 4
        $user = (new GetUserDetails($userID))->getUserData()->makeVisible(['role_id', 'user_id']);
84 4
        if($user->role_id < Auth::user()->role_id)
85
        {
86 2
            return abort(403, 'You cannot update a user with more permissions than you');
1 ignored issue
show
Bug introduced by
Are you sure the usage of abort(403, 'You cannot u... permissions than you') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
87
        }
88
89 2
        (new SetUserDetails)->updateUser($request, $userID);
90 2
        Log::info('User ID '.$userID.' was update by '.Auth::user()->full_name.'.  Details - ', $request->toArray());
91 2
        return response()->json(['success' => true]);
92
    }
93
94
    //  Submit the change password form
95 4
    public function changePassword(ChangeUserPasswordRequest $request)
96
    {
97
        //  Before changing user password, verify that the user does not have more permission
98 4
        $user = (new GetUserDetails($request->user_id))->getUserData();
99 4
        if($user->role_id < Auth::user()->role_id)
100
        {
101 2
            return abort(403, 'You cannot update a user with more permissions than you');
1 ignored issue
show
Bug introduced by
Are you sure the usage of abort(403, 'You cannot u... permissions than you') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
102
        }
103
104 2
        (new SetUserDetails)->updatePassword($request->password, $request->user_id, $request->force_change);
105 2
        Log::info('Password for User ID '.$request->user_id.' has been updated by '.Auth::user()->full_name);
106
107 2
        return response()->json(['success' => true]);
108
    }
109
110
    //  See the login history for the user
111 2
    public function loginHistory($userID, $username)
112
    {
113 2
        return $userID;
114
    }
115
116
    //  Deactivate a user
117 4
    public function destroy($userID)
118
    {
119
        //  Before disabling user, verify that the user does not have more permission
120 4
        $user = (new GetUserDetails($userID))->getUserData();
121 4
        if($user->role_id < Auth::user()->role_id)
122
        {
123 2
            return abort(403, 'You cannot disable a user with more permissions than you');
1 ignored issue
show
Bug introduced by
Are you sure the usage of abort(403, 'You cannot d... permissions than you') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
124
        }
125
126 2
        (new SetUserDetails)->disableUser($userID);
127 2
        Log::notice('User '.$user->full_name.' has been disabled by '.Auth::user()->full_name.'.  Details - ', $user->toArray());
128 2
        return response()->json(['success' => true]);
129
    }
130
}
131