Test Failed
Push — dev5 ( ba6069...125e24 )
by Ron
09:51
created

UserController::edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 10
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Domains\Users\UserList;
6
use App\Domains\Users\GetUserDetails;
7
use App\Domains\Users\GetUserRoles;
8
use App\Domains\Users\SetUserDetails;
9
use App\User;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Auth;
12
use App\Http\Controllers\Controller;
13
use App\Http\Requests\AdminUserChangePasswordRequest;
14
use App\Http\Requests\UserBasicAccountRequest;
15
use App\Http\Requests\UserCreateRequest;
16
use Illuminate\Support\Facades\Route;
17
use App\Http\Resources\UserCollection;
18
19
20
class UserController extends Controller
21
{
22
    //  Constructor sets up middleware
23
    public function __construct()
24
    {
25 142
        $this->middleware('auth')->except('initializeUser', 'submitInitializeUser');
26
        $this->middleware(function($request, $next) {
27 142
            $this->authorize('hasAccess', 'Manage Users');
28
            return $next($request);
29 122
        });
30 102
    }
31 142
32 142
    //  Show the list of current users to edit
33
    public function index()
34
    {
35 2
        return view('admin.userIndex', [
36
            'userList' => (new UserList)->getActiveUsers()->toJson(),
37 2
        ]);
38
    }
39 2
40 2
    //  Check if a username is in use
41
    public function checkUser($username, $type)
42 2
    {
43
        $user = (new GetUserDetails)->checkForDuplicate($type, $username);
44 2
45 2
        if(!$user)
46 2
        {
47
            return response()->json(['duplicate' => false]);
48
        }
49
50
        return response()->json([
51 12
            'duplicate' => true,
52
            'user'      => $user->full_name,
53 12
            'username'  => $user->username,
54
            'active'    => $user->deleted_at == null ? 1 : 0,
55 12
        ]);
56
    }
57 12
58
    //  Show the Add User form
59 4
    public function create()
60 4
    {
61
        return view('admin.newUser', [
62
            'roles' => (new GetUserRoles)->getRoleList()->toJson(),
0 ignored issues
show
Bug introduced by
The method toJson() does not exist on App\Http\Resources\UserRoleTypeCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            'roles' => (new GetUserRoles)->getRoleList()->/** @scrutinizer ignore-call */ toJson(),
Loading history...
63 8
        ]);
64 8
    }
65 8
66 8
    //  Submit the Add User form
67 8
    public function store(UserCreateRequest $request)
68 8
    {
69
        (new SetUserDetails)->createNewUser($request);
70
        return response()->json(['success' => true]);
71
    }
72
73 8
74
75 8
76
77 8
78 8
79
80 8
81
82 8
    //  List all inactive users
83
    public function show($type)
84 4
    {
85 4
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
86
        $route = '';
87 8
88
        if($type !== 'inactive')
89 2
        {
90 2
            Log::error('Someone tried to access the Inactive Users link with an improper argument - Argument: '.$type);
91
            return abort(404);
92
        }
93
        $userList = new UserCollection(User::onlyTrashed()->get()
94 8
                /** @scrutinizer ignore-call */
95 8
                ->makeVisible('user_id')
96 8
                ->makeVisible('deleted_at'));
97
98
        Log::debug('List of inactive users - ', array($userList));
99
        return view('admin.userDeleted', [
100
            'userList' => $userList,
101 8
            'route'    => $route,
102 8
        ]);
103 8
    }
104
105
    //  Reactivate a disabled user
106
    public function reactivateUser($id)
107
    {
108 16
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
109
        User::withTrashed()->where('user_id', $id)->restore();
110 16
111
        Log::notice('User ID '.$id.' reactivated by '.Auth::user()->full_name);
112
        return response()->json([
113 16
            'success' => true,
114 16
        ]);
115
    }
116
117
118
119
120
121
122 2
123 2
124 2
125 2
    //  Open the edit user form
126 2
    public function edit($id)
127 2
    {
128 2
        $userObj = new GetUserDetails($id);
129
        $details = $userObj->getuserData()->makeVisible(['user_id', 'username', 'role_id']);
130 2
        if($details->role_id < Auth::user()->role_id)
131 2
        {
132
            abort(403, 'You cannot edit a user with more permissions than you');
133 2
        }
134 2
135
        return view('admin.userEdit', [
136 2
            'roles' => (new GetUserRoles)->getRoleList()->toJson(),
137
            'user'  => $details->toJson(),
138
        ]);
139 2
    }
140 2
141 2
    //  Submit the update user form
142 2
    public function update(UserBasicAccountRequest $request, $id)
143
    {
144 2
        $userObj = new SetUserDetails;
145
        $userObj->updateUserDetails($request, $id);
146
147 2
        return response()->json(['success' => true]);
148
    }
149 2
150
    //  Submit the change password form
151
    public function submitPassword(AdminUserChangePasswordRequest $request)
152 2
    {
153
        $userObj = new SetUserDetails;
154
        $userObj->updateUserPassword($request->password, $request->user_id, $request->force_change);
155
156 6
        return response()->json([
157
            'success' => true,
158 6
            'reason'  => 'Password successfully reset',
159 6
        ]);
160
    }
161 6
162
    //  Disable the user
163 2
    public function destroy($id)
164 2
    {
165
        $userObj = new SetUserDetails;
166 4
        $userObj->disableUser($id);
167
168 4
        return response()->json([
169 4
            'success' => true,
170
            'reason'  => 'User successfully deactivated',
171 4
        ]);
172 4
    }
173
}
174