Completed
Push — master ( 720025...0e57e6 )
by ARCANEDEV
04:07
created

UsersController::delete()   B

Complexity

Conditions 3
Paths 8

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
ccs 0
cts 28
cp 0
rs 8.8571
cc 3
eloc 20
nc 8
nop 1
crap 12
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelAuth\Services\UserImpersonator;
4
use Arcanesoft\Auth\Http\Requests\Admin\Users\CreateUserRequest;
5
use Arcanesoft\Auth\Http\Requests\Admin\Users\UpdateUserRequest;
6
use Arcanesoft\Auth\Policies\UsersPolicy;
7
use Arcanesoft\Contracts\Auth\Models\Role;
8
use Arcanesoft\Contracts\Auth\Models\User;
9
use Log;
10
11
/**
12
 * Class     UsersController
13
 *
14
 * @package  Arcanesoft\Auth\Http\Controllers\Admin
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class UsersController extends Controller
18
{
19
    /* ------------------------------------------------------------------------------------------------
20
     |  Properties
21
     | ------------------------------------------------------------------------------------------------
22
     */
23
    /**
24
     * The user model.
25
     *
26
     * @var  \Arcanesoft\Contracts\Auth\Models\User
27
     */
28
    protected $user;
29
30
    /* ------------------------------------------------------------------------------------------------
31
     |  Constructor
32
     | ------------------------------------------------------------------------------------------------
33
     */
34
    /**
35
     * Instantiate the controller.
36
     *
37
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
38
     */
39
    public function __construct(User $user)
40
    {
41
        parent::__construct();
42
43
        $this->user = $user;
44
45
        $this->setCurrentPage('auth-users');
46
        $this->addBreadcrumbRoute('Users', 'admin::auth.users.index');
47
    }
48
49
    /* ------------------------------------------------------------------------------------------------
50
     |  Main Functions
51
     | ------------------------------------------------------------------------------------------------
52
     */
53
    /**
54
     * List the users.
55
     *
56
     * @param  bool  $trashed
57
     *
58
     * @return \Illuminate\View\View
59
     */
60
    public function index($trashed = false)
61
    {
62
        $this->authorize(UsersPolicy::PERMISSION_LIST);
63
64
        $users = $this->user->with('roles');
0 ignored issues
show
Bug introduced by
The method with() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66
        $users = $trashed
67
            ? $users->onlyTrashed()->paginate(30)
68
            : $users->paginate(30);
69
70
        $title = 'List of users' . ($trashed ? ' - Trashed' : '');
71
        $this->setTitle($title);
72
        $this->addBreadcrumb($title);
73
74
        return $this->view('admin.users.list', compact('trashed', 'users'));
75
    }
76
77
    /**
78
     * List the trashed users.
79
     *
80
     * @return \Illuminate\View\View
81
     */
82
    public function trashList()
83
    {
84
        return $this->index(true);
85
    }
86
87
    /**
88
     * List the users by a role.
89
     *
90
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
91
     * @param  bool                                    $trashed
92
     *
93
     * @return \Illuminate\View\View
94
     */
95
    public function listByRole(Role $role, $trashed = false)
96
    {
97
        $this->authorize(UsersPolicy::PERMISSION_LIST);
98
99
        $users = $role->users()->with('roles')->paginate(30);
100
101
        $title = "List of users - {$role->name} Role" . ($trashed ? ' - Trashed' : '');
0 ignored issues
show
Bug introduced by
Accessing name on the interface Arcanesoft\Contracts\Auth\Models\Role suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
102
        $this->setTitle($title);
103
        $this->addBreadcrumb($title);
104
105
        return $this->view('admin.users.list', compact('trashed', 'users'));
106
    }
107
108
    /**
109
     * Show the create a new user form.
110
     *
111
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
112
     *
113
     * @return \Illuminate\View\View
114
     */
115
    public function create(Role $role)
116
    {
117
        $this->authorize(UsersPolicy::PERMISSION_CREATE);
118
119
        $roles = $role->all();
0 ignored issues
show
Bug introduced by
The method all() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\Role>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
121
        $title = 'Create a new user';
122
        $this->setTitle($title);
123
        $this->addBreadcrumb($title);
124
125
        return $this->view('admin.users.create', compact('roles'));
126
    }
127
128
    /**
129
     * Store the new user.
130
     *
131
     * @param  \Arcanesoft\Auth\Http\Requests\Admin\Users\CreateUserRequest $request
132
     * @param  \Arcanesoft\Contracts\Auth\Models\User                       $user
133
     *
134
     * @return \Illuminate\Http\RedirectResponse
135
     */
136
    public function store(CreateUserRequest $request, User $user)
137
    {
138
        $this->authorize(UsersPolicy::PERMISSION_CREATE);
139
140
        $data = $request->only([
141
            'username', 'email', 'first_name', 'last_name', 'password'
142
        ]);
143
        $user->fill($data);
0 ignored issues
show
Bug introduced by
The method fill() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        $user->is_active = true;
0 ignored issues
show
Bug introduced by
Accessing is_active on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
145
        $user->save();
146
        $user->roles()->sync($request->get('roles'));
147
148
        $message = "The user {$user->username} was created successfully !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
149
        Log::info($message, $user->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
        $this->notifySuccess($message, 'User created !');
151
152
        return redirect()->route('admin::auth.users.index');
153
    }
154
155
    /**
156
     * Show the user's details.
157
     *
158
     * @param \Arcanesoft\Contracts\Auth\Models\User $user
159
     *
160
     * @return \Illuminate\View\View
161
     */
162
    public function show(User $user)
163
    {
164
        $this->authorize(UsersPolicy::PERMISSION_SHOW);
165
166
        $user->load(['roles', 'roles.permissions']);
167
168
        $title = 'User details';
169
        $this->setTitle($title);
170
        $this->addBreadcrumb($title);
171
172
        return $this->view('admin.users.show', compact('user'));
173
    }
174
175
    /**
176
     * Show the edit the user form.
177
     *
178
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
179
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
180
     *
181
     * @return \Illuminate\View\View
182
     */
183
    public function edit(User $user, Role $role)
184
    {
185
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
186
187
        $user->load(['roles', 'roles.permissions']);
188
        $roles = $role->all();
0 ignored issues
show
Bug introduced by
The method all() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\Role>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
189
190
        $title = 'Edit a user';
191
        $this->setTitle($title);
192
        $this->addBreadcrumb($title);
193
194
        return $this->view('admin.users.edit', compact('user', 'roles'));
195
    }
196
197
    /**
198
     * Update the user.
199
     *
200
     * @param  \Arcanesoft\Auth\Http\Requests\Admin\Users\UpdateUserRequest $request
201
     * @param  \Arcanesoft\Contracts\Auth\Models\User                       $user
202
     *
203
     * @return \Illuminate\Http\RedirectResponse
204
     */
205
    public function update(UpdateUserRequest $request, User $user)
206
    {
207
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
208
209
        $inputs = ['username', 'email', 'first_name', 'last_name'];
210
211
        if ($request->has('password')) {
212
            $inputs[] = 'password';
213
        }
214
215
        $user->update($request->only($inputs));
0 ignored issues
show
Bug introduced by
The method update() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
216
        $user->roles()->sync($request->get('roles'));
217
218
        $message = "The user {$user->username} was updated successfully !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
219
        Log::info($message, $user->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
220
        $this->notifySuccess($message, 'User Updated !');
221
222
        return redirect()->route('admin::auth.users.show', [$user->hashed_id]);
0 ignored issues
show
Bug introduced by
Accessing hashed_id on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
223
    }
224
225
    /**
226
     * Activate/Disable a user.
227
     *
228
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
229
     *
230
     * @return \Illuminate\Http\JsonResponse
231
     */
232
    public function activate(User $user)
233
    {
234
        self::onlyAjax();
235
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
236
237
        try {
238
            if ($user->isActive()) {
239
                $title   = 'User disabled !';
240
                $message = "The user {$user->username} has been successfully disabled !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
241
                $user->deactivate();
242
            }
243
            else {
244
                $title   = 'User activated !';
245
                $message = "The user {$user->username} has been successfully activated !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
246
                $user->activate();
247
            }
248
249
            Log::info($message, $user->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
250
            $this->notifySuccess($message, $title);
251
252
            $ajax = [
253
                'status'  => 'success',
254
                'message' => $message,
255
            ];
256
        }
257
        catch (\Exception $e) {
258
            $ajax = [
259
                'status'  => 'error',
260
                'message' => $e->getMessage(),
261
            ];
262
        }
263
264
        return response()->json($ajax);
265
    }
266
267
    /**
268
     * Restore the trashed user.
269
     *
270
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
271
     *
272
     * @return \Illuminate\Http\JsonResponse
273
     */
274
    public function restore(User $user)
275
    {
276
        self::onlyAjax();
277
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
278
279
        try {
280
            $user->restore();
0 ignored issues
show
Bug introduced by
The method restore() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
281
282
            $message = "The user {$user->username} has been successfully restored !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
283
            Log::info($message, $user->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
284
            $this->notifySuccess($message, 'User restored !');
285
286
            $ajax = [
287
                'status'  => 'success',
288
                'message' => $message,
289
            ];
290
        }
291
        catch (\Exception $e) {
292
            $ajax = [
293
                'status'  => 'error',
294
                'message' => $e->getMessage(),
295
            ];
296
        }
297
298
        return response()->json($ajax);
299
    }
300
301
    /**
302
     * Delete a user.
303
     *
304
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
305
     *
306
     * @return \Illuminate\Http\JsonResponse
307
     */
308
    public function delete(User $user)
309
    {
310
        self::onlyAjax();
311
        $this->authorize(UsersPolicy::PERMISSION_DELETE);
312
313
        try {
314
            if ($user->trashed()) {
0 ignored issues
show
Bug introduced by
The method trashed() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
315
                $user->forceDelete();
0 ignored issues
show
Bug introduced by
The method forceDelete() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
316
                $message = "The user {$user->username} has been successfully deleted !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
317
                Log::info($message, $user->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
318
            }
319
            else {
320
                $user->delete();
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Arcanesoft\Contracts\Auth\Models\User>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
321
                $message = "The user {$user->username} was placed in trashed users !";
0 ignored issues
show
Bug introduced by
Accessing username on the interface Arcanesoft\Contracts\Auth\Models\User suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
322
            }
323
324
            $this->notifySuccess($message, 'User deleted !');
325
326
            $ajax = [
327
                'status'  => 'success',
328
                'message' => $message,
329
            ];
330
        }
331
        catch(\Exception $e) {
332
            $ajax = [
333
                'status'  => 'error',
334
                'message' => $e->getMessage(),
335
            ];
336
        }
337
338
        return response()->json($ajax);
339
    }
340
341
    /**
342
     * Impersonate a user.
343
     *
344
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
345
     *
346
     * @return \Illuminate\Http\RedirectResponse
347
     */
348
    public function impersonate(User $user)
349
    {
350
        if (UserImpersonator::start($user)) {
351
            return redirect()->to('/');
352
        }
353
354
        $this->notifyDanger('Impersonate disabled for this user.', 'Impersonation failed');
355
356
        return redirect()->back();
357
    }
358
}
359