Completed
Push — master ( 08597b...c6ec6e )
by ARCANEDEV
03:52
created

UsersController::impersonate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Foundation;
2
3
use Arcanesoft\Auth\Helpers\UserImpersonator;
4
use Arcanesoft\Auth\Http\Requests\Backend\Users\CreateUserRequest;
5
use Arcanesoft\Auth\Http\Requests\Backend\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\Foundation
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', 'auth::foundation.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('foundation.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('foundation.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('foundation.users.create', compact('roles'));
126
    }
127
128
    /**
129
     * Store the new user.
130
     *
131
     * @param  \Arcanesoft\Auth\Http\Requests\Backend\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('auth::foundation.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('foundation.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('foundation.users.edit', compact('user', 'roles'));
195
    }
196
197
    /**
198
     * Update the user.
199
     *
200
     * @param  \Arcanesoft\Auth\Http\Requests\Backend\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('auth::foundation.users.show', [
223
            $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...
224
        ]);
225
    }
226
227
    /**
228
     * Activate/Disable a user.
229
     *
230
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
231
     *
232
     * @return \Illuminate\Http\JsonResponse
233
     */
234
    public function activate(User $user)
235
    {
236
        self::onlyAjax();
237
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
238
239
        try {
240
            if ($user->isActive()) {
241
                $title   = 'User disabled !';
242
                $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...
243
                $user->deactivate();
244
            }
245
            else {
246
                $title   = 'User activated !';
247
                $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...
248
                $user->activate();
249
            }
250
251
            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...
252
            $this->notifySuccess($message, $title);
253
254
            $ajax = [
255
                'status'  => 'success',
256
                'message' => $message,
257
            ];
258
        }
259
        catch (\Exception $e) {
260
            $ajax = [
261
                'status'  => 'error',
262
                'message' => $e->getMessage(),
263
            ];
264
        }
265
266
        return response()->json($ajax);
267
    }
268
269
    /**
270
     * Restore the trashed user.
271
     *
272
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
273
     *
274
     * @return \Illuminate\Http\JsonResponse
275
     */
276
    public function restore(User $user)
277
    {
278
        self::onlyAjax();
279
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
280
281
        try {
282
            $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...
283
284
            $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...
285
            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...
286
            $this->notifySuccess($message, 'User restored !');
287
288
            $ajax = [
289
                'status'  => 'success',
290
                'message' => $message,
291
            ];
292
        }
293
        catch (\Exception $e) {
294
            $ajax = [
295
                'status'  => 'error',
296
                'message' => $e->getMessage(),
297
            ];
298
        }
299
300
        return response()->json($ajax);
301
    }
302
303
    /**
304
     * Delete a user.
305
     *
306
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
307
     *
308
     * @return \Illuminate\Http\JsonResponse
309
     */
310
    public function delete(User $user)
311
    {
312
        self::onlyAjax();
313
        $this->authorize(UsersPolicy::PERMISSION_DELETE);
314
315
        try {
316
            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...
317
                $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...
318
                $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...
319
                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...
320
            }
321
            else {
322
                $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...
323
                $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...
324
            }
325
326
            $this->notifySuccess($message, 'User deleted !');
327
328
            $ajax = [
329
                'status'  => 'success',
330
                'message' => $message,
331
            ];
332
        }
333
        catch(\Exception $e) {
334
            $ajax = [
335
                'status'  => 'error',
336
                'message' => $e->getMessage(),
337
            ];
338
        }
339
340
        return response()->json($ajax);
341
    }
342
343
    /**
344
     * Impersonate a user.
345
     *
346
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
347
     *
348
     * @return \Illuminate\Http\RedirectResponse
349
     */
350
    public function impersonate(User $user)
351
    {
352
        if (UserImpersonator::start($user)) {
353
            return redirect()->to('/');
354
        }
355
356
        $this->notifyDanger('Impersonate disabled for this user.', 'Impersonation failed');
357
358
        return redirect()->back();
359
    }
360
}
361