Passed
Push — dev5a ( d46f4b...f24c41 )
by Ron
07:14
created

UserController::listInactive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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', 'user_role_permissions']),
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
    //  List all users who have been disabled
64 2
    public function listInactive()
65
    {
66 2
        return view('admin.userList', [
67 2
            'userList' => (new GetUserList)->getInactiveUsers(),
68
            'active'   => false,
69
        ]);
70
    }
71
72
    //  Form to edit an existing user
73 4
    public function edit($userID)
74
    {
75
        //  Before showing user form, verify that the user does not have more permission
76 4
        $user = (new GetUserDetails($userID))->getUserData()->makeVisible(['role_id', 'user_id']);
77 4
        if($user->role_id < Auth::user()->role_id)
78
        {
79 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...
80
        }
81
82 2
        return view('admin.userEdit', [
83 2
            'details' => $user,
84 2
            'roles' => (new GetRoles)->getRoleList()->makeHidden(['allow_edit', 'user_role_permissions']),
85
        ]);
86
    }
87
88
    //  Submit the edit user form
89 4
    public function update(EditUserRequest $request, $userID)
90
    {
91
        //  Before submitting user form, verify that the user does not have more permission
92 4
        $user = (new GetUserDetails($userID))->getUserData()->makeVisible(['role_id', 'user_id']);
93 4
        if($user->role_id < Auth::user()->role_id)
94
        {
95 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...
96
        }
97
98 2
        (new SetUserDetails)->updateUser($request, $userID);
99 2
        Log::info('User ID '.$userID.' was update by '.Auth::user()->full_name.'.  Details - ', $request->toArray());
100 2
        return response()->json(['success' => true]);
101
    }
102
103
    //  Submit the change password form
104 4
    public function changePassword(ChangeUserPasswordRequest $request)
105
    {
106
        //  Before changing user password, verify that the user does not have more permission
107 4
        $user = (new GetUserDetails($request->user_id))->getUserData();
108 4
        if($user->role_id < Auth::user()->role_id)
109
        {
110 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...
111
        }
112
113 2
        (new SetUserDetails)->updatePassword($request->password, $request->user_id, $request->force_change);
114 2
        Log::info('Password for User ID '.$request->user_id.' has been updated by '.Auth::user()->full_name);
115
116 2
        return response()->json(['success' => true]);
117
    }
118
119
    //  See the login history for the user
120 2
    public function loginHistory($userID, $username)
121
    {
122 2
        return $userID;
123
    }
124
125
    //  Deactivate a user
126 4
    public function destroy($userID)
127
    {
128
        //  Before disabling user, verify that the user does not have more permission
129 4
        $user = (new GetUserDetails($userID))->getUserData();
130 4
        if($user->role_id < Auth::user()->role_id)
131
        {
132 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...
133
        }
134
135 2
        (new SetUserDetails)->disableUser($userID);
136 2
        Log::notice('User '.$user->full_name.' has been disabled by '.Auth::user()->full_name.'.  Details - ', $user->toArray());
137 2
        return response()->json(['success' => true]);
138
    }
139
140
    //  Reactivate a disabled user
141 2
    public function activate($userID)
142
    {
143 2
        (new SetUserDetails)->reactivateUser($userID);
144 2
        Log::notice('User ID '.$userID.' has been reactivated by '.Auth::user()->full_name);
145 2
        return response()->json(['success' => true]);
146
    }
147
}
148