UsersController::delete()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 9.6333
c 0
b 0
f 0
cc 4
nc 8
nop 1
crap 20
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanedev\LaravelImpersonator\Contracts\Impersonator;
5
use Arcanesoft\Auth\Http\Requests\Admin\Users\CreateUserRequest;
6
use Arcanesoft\Auth\Http\Requests\Admin\Users\UpdateUserRequest;
7
use Arcanesoft\Auth\Policies\UsersPolicy;
8
use Arcanesoft\Contracts\Auth\Models\Role;
9
use Arcanesoft\Contracts\Auth\Models\User;
10
use Log;
11
12
/**
13
 * Class     UsersController
14
 *
15
 * @package  Arcanesoft\Auth\Http\Controllers\Admin
16
 * @author   ARCANEDEV <[email protected]>
17
 */
18
class UsersController extends Controller
19
{
20
    /* -----------------------------------------------------------------
21
     |  Traits
22
     | -----------------------------------------------------------------
23
     */
24
25
    use JsonResponses;
26
27
    /* -----------------------------------------------------------------
28
     |  Properties
29
     | -----------------------------------------------------------------
30
     */
31
32
    /**
33
     * The user model.
34
     *
35
     * @var  \Arcanesoft\Contracts\Auth\Models\User
36
     */
37
    protected $user;
38
39
    /* -----------------------------------------------------------------
40
     |  Constructor
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * Instantiate the controller.
46
     *
47
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
48
     */
49
    public function __construct(User $user)
50
    {
51
        parent::__construct();
52
53
        $this->user = $user;
54
55
        $this->setCurrentPage('auth-users');
56
        $this->addBreadcrumbRoute(trans('auth::users.titles.users'), 'admin::auth.users.index');
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Main Methods
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * List the users.
66
     *
67
     * @param  bool  $trashed
68
     *
69
     * @return \Illuminate\View\View
70
     */
71
    public function index($trashed = false)
72
    {
73
        $this->authorize(UsersPolicy::PERMISSION_LIST);
74
75
        $users = $this->user->with('roles')->protectAdmins()->when($trashed, function ($query) {
76
            return $query->onlyTrashed();
77
        })->paginate(30);
78
79
        $title = trans('auth::users.titles.users-list').($trashed ? ' - '.trans('core::generals.trashed') : '');
80
        $this->setTitle($title);
81
        $this->addBreadcrumb($title);
82
83
        return $this->view('admin.users.index', compact('trashed', 'users'));
84
    }
85
86
    /**
87
     * List the trashed users.
88
     *
89
     * @return \Illuminate\View\View
90
     */
91
    public function trashList()
92
    {
93
        return $this->index(true);
94
    }
95
96
    /**
97
     * List the users by a role.
98
     *
99
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
100
     * @param  bool                                    $trashed
101
     *
102
     * @return \Illuminate\View\View
103
     */
104
    public function listByRole(Role $role, $trashed = false)
105
    {
106
        $this->authorize(UsersPolicy::PERMISSION_LIST);
107
108
        $users = $role->users()->with('roles')
109
            ->protectAdmins()
110
            ->paginate(30);
111
112
        $title = trans('auth::users.titles.users-list')." - {$role->name} Role". ($trashed ? ' - '.trans('core::generals.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...
113
        $this->setTitle($title);
114
        $this->addBreadcrumb($title);
115
116
        return $this->view('admin.users.list', compact('trashed', 'users'));
117
    }
118
119
    /**
120
     * Show the create a new user form.
121
     *
122
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
123
     *
124
     * @return \Illuminate\View\View
125
     */
126
    public function create(Role $role)
127
    {
128
        $this->authorize(UsersPolicy::PERMISSION_CREATE);
129
130
        $roles = $role->all();
131
132
        $this->setTitle($title = trans('auth::users.titles.create-user'));
133
        $this->addBreadcrumb($title);
134
135
        return $this->view('admin.users.create', compact('roles'));
136
    }
137
138
    /**
139
     * Store the new user.
140
     *
141
     * @param  \Arcanesoft\Auth\Http\Requests\Admin\Users\CreateUserRequest  $request
142
     * @param  \Arcanesoft\Contracts\Auth\Models\User                        $user
143
     *
144
     * @return \Illuminate\Http\RedirectResponse
145
     */
146
    public function store(CreateUserRequest $request, User $user)
147
    {
148
        $this->authorize(UsersPolicy::PERMISSION_CREATE);
149
150
        $user->fill($request->getValidatedData());
151
        $user->activate(false);
152
        $user->save();
153
        $user->roles()->sync($request->get('roles'));
154
155
        $this->transNotification('created', ['name' => $user->full_name], $user->toArray());
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
156
157
        return redirect()->route('admin::auth.users.index');
158
    }
159
160
    /**
161
     * Show the user's details.
162
     *
163
     * @param \Arcanesoft\Contracts\Auth\Models\User $user
164
     *
165
     * @return \Illuminate\View\View
166
     */
167
    public function show(User $user)
168
    {
169
        $this->authorize(UsersPolicy::PERMISSION_SHOW);
170
171
        $user->load(['roles', 'roles.permissions']);
172
173
        $this->setTitle($title = trans('auth::users.titles.user-details'));
174
        $this->addBreadcrumb($title);
175
176
        return $this->view('admin.users.show', compact('user'));
177
    }
178
179
    /**
180
     * Show the edit the user form.
181
     *
182
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
183
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
184
     *
185
     * @return \Illuminate\View\View
186
     */
187
    public function edit(User $user, Role $role)
188
    {
189
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
190
191
        $user->load(['roles', 'roles.permissions']);
192
        $roles = $role->all();
193
194
        $this->setTitle($title = trans('auth::users.titles.edit-user'));
195
        $this->addBreadcrumb($title);
196
197
        return $this->view('admin.users.edit', compact('user', 'roles'));
198
    }
199
200
    /**
201
     * Update the user.
202
     *
203
     * @param  \Arcanesoft\Auth\Http\Requests\Admin\Users\UpdateUserRequest  $request
204
     * @param  \Arcanesoft\Contracts\Auth\Models\User                        $user
205
     *
206
     * @return \Illuminate\Http\RedirectResponse
207
     */
208
    public function update(UpdateUserRequest $request, User $user)
209
    {
210
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
211
212
        $user->update($request->getValidatedData());
213
        $user->roles()->sync($request->get('roles'));
214
215
        $this->transNotification('updated', ['name' => $user->full_name], $user->toArray());
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
216
217
        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...
218
    }
219
220
    /**
221
     * Activate/Disable a user.
222
     *
223
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
224
     *
225
     * @return \Illuminate\Http\JsonResponse
226
     */
227
    public function activate(User $user)
228
    {
229
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
230
231
        try {
232
            ($active = $user->isActive()) ? $user->deactivate() : $user->activate();
233
234
            $message = $this->transNotification(
235
                $active ? 'disabled' : 'enabled',
236
                ['name' => $user->full_name],
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
237
                $user->toArray()
238
            );
239
240
            return $this->jsonResponseSuccess(compact('message'));
241
        }
242
        catch (\Exception $e) {
243
            return $this->jsonResponseError(['message' => $e->getMessage()], 500);
244
        }
245
    }
246
247
    /**
248
     * Restore the trashed user.
249
     *
250
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
251
     *
252
     * @return \Illuminate\Http\JsonResponse
253
     */
254
    public function restore(User $user)
255
    {
256
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
257
258
        try {
259
            $user->restore();
260
261
            $message = $this->transNotification('restored', ['name' => $user->full_name], $user->toArray());
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
262
263
            return $this->jsonResponseSuccess(compact('message'));
264
        }
265
        catch (\Exception $e) {
266
            return $this->jsonResponseError(['message' => $e->getMessage()], 500);
267
        }
268
    }
269
270
    /**
271
     * Delete a user.
272
     *
273
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
274
     *
275
     * @return \Illuminate\Http\JsonResponse
276
     */
277
    public function delete(User $user)
278
    {
279
        $this->authorize(UsersPolicy::PERMISSION_DELETE);
280
281
        try {
282
            ($trashed = $user->trashed()) ? $user->forceDelete() : $user->delete();
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Arcanesoft\Contracts\Auth\Models\User. Did you maybe mean forceDelete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
283
284
            $message = $this->transNotification(
285
                $trashed ? 'deleted' : 'trashed',
286
                ['name' => $user->full_name],
0 ignored issues
show
Bug introduced by
Accessing full_name 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...
287
                $user->toArray()
288
            );
289
290
            return $this->jsonResponseSuccess(compact('message'));
291
        }
292
        catch(\Exception $e) {
293
            return $this->jsonResponseError(['message' => $e->getMessage()], 500);
294
        }
295
    }
296
297
    /**
298
     * Impersonate a user.
299
     *
300
     * @param  \Arcanesoft\Contracts\Auth\Models\User                 $user
301
     * @param  \Arcanedev\LaravelImpersonator\Contracts\Impersonator  $impersonator
302
     *
303
     * @return \Illuminate\Http\RedirectResponse
304
     */
305
    public function impersonate(User $user, Impersonator $impersonator)
306
    {
307
        /** @var  \Arcanedev\LaravelImpersonator\Contracts\Impersonatable  $user */
308
        if ( ! $impersonator->start(auth()->user(), $user)) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
309
            $this->notifyDanger(
310
                trans('auth::users.messages.impersonation-failed.message'),
311
                trans('auth::users.messages.impersonation-failed.title')
312
            );
313
314
            return redirect()->back();
315
        }
316
317
        return redirect()->to('/');
318
    }
319
320
    /* -----------------------------------------------------------------
321
     |  Other Methods
322
     | -----------------------------------------------------------------
323
     */
324
325
    /**
326
     * Notify with translation.
327
     *
328
     * @param  string  $action
329
     * @param  array   $replace
330
     * @param  array   $context
331
     *
332
     * @return string
333
     */
334
    protected function transNotification($action, array $replace = [], array $context = [])
335
    {
336
        $title   = trans("auth::users.messages.{$action}.title");
337
        $message = trans("auth::users.messages.{$action}.message", $replace);
338
339
        Log::info($message, $context);
340
        $this->notifySuccess($message, $title);
341
342
        return $message;
343
    }
344
}
345