Completed
Push — master ( 0ae202...6f780f )
by ARCANEDEV
06:36
created

UsersController::restore()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 3
nop 1
crap 6
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Admin;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanedev\LaravelAuth\Services\UserImpersonator;
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
    use JsonResponses;
25
26
    /* -----------------------------------------------------------------
27
     |  Properties
28
     | -----------------------------------------------------------------
29
     */
30
    /**
31
     * The user model.
32
     *
33
     * @var  \Arcanesoft\Contracts\Auth\Models\User
34
     */
35
    protected $user;
36
37
    /* -----------------------------------------------------------------
38
     |  Constructor
39
     | -----------------------------------------------------------------
40
     */
41
    /**
42
     * Instantiate the controller.
43
     *
44
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
45
     */
46
    public function __construct(User $user)
47
    {
48
        parent::__construct();
49
50
        $this->user = $user;
51
52
        $this->setCurrentPage('auth-users');
53
        $this->addBreadcrumbRoute('Users', 'admin::auth.users.index');
54
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Main Methods
58
     | -----------------------------------------------------------------
59
     */
60
    /**
61
     * List the users.
62
     *
63
     * @param  bool  $trashed
64
     *
65
     * @return \Illuminate\View\View
66
     */
67
    public function index($trashed = false)
68
    {
69
        $this->authorize(UsersPolicy::PERMISSION_LIST);
70
71
        $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...
72
73
        $users = $trashed
74
            ? $users->onlyTrashed()->paginate(30)
75
            : $users->paginate(30);
76
77
        $this->setTitle($title = 'List of users' . ($trashed ? ' - Trashed' : ''));
78
        $this->addBreadcrumb($title);
79
80
        return $this->view('admin.users.list', compact('trashed', 'users'));
81
    }
82
83
    /**
84
     * List the trashed users.
85
     *
86
     * @return \Illuminate\View\View
87
     */
88
    public function trashList()
89
    {
90
        return $this->index(true);
91
    }
92
93
    /**
94
     * List the users by a role.
95
     *
96
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
97
     * @param  bool                                    $trashed
98
     *
99
     * @return \Illuminate\View\View
100
     */
101
    public function listByRole(Role $role, $trashed = false)
102
    {
103
        $this->authorize(UsersPolicy::PERMISSION_LIST);
104
105
        $users = $role->users()->with('roles')->paginate(30);
106
107
        $this->setTitle($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...
108
        $this->addBreadcrumb($title);
109
110
        return $this->view('admin.users.list', compact('trashed', 'users'));
111
    }
112
113
    /**
114
     * Show the create a new user form.
115
     *
116
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
117
     *
118
     * @return \Illuminate\View\View
119
     */
120
    public function create(Role $role)
121
    {
122
        $this->authorize(UsersPolicy::PERMISSION_CREATE);
123
124
        $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...
125
126
        $this->setTitle($title = 'Create a new user');
127
        $this->addBreadcrumb($title);
128
129
        return $this->view('admin.users.create', compact('roles'));
130
    }
131
132
    /**
133
     * Store the new user.
134
     *
135
     * @param  \Arcanesoft\Auth\Http\Requests\Admin\Users\CreateUserRequest  $request
136
     * @param  \Arcanesoft\Contracts\Auth\Models\User                        $user
137
     *
138
     * @return \Illuminate\Http\RedirectResponse
139
     */
140
    public function store(CreateUserRequest $request, User $user)
141
    {
142
        $this->authorize(UsersPolicy::PERMISSION_CREATE);
143
144
        $data = $request->only([
145
            'username', 'email', 'first_name', 'last_name', 'password'
146
        ]);
147
        $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...
148
        $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...
149
        $user->save();
150
        $user->roles()->sync($request->get('roles'));
151
152
        $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...
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...
153
154
        return redirect()->route('admin::auth.users.index');
155
    }
156
157
    /**
158
     * Show the user's details.
159
     *
160
     * @param \Arcanesoft\Contracts\Auth\Models\User $user
161
     *
162
     * @return \Illuminate\View\View
163
     */
164
    public function show(User $user)
165
    {
166
        $this->authorize(UsersPolicy::PERMISSION_SHOW);
167
168
        $user->load(['roles', 'roles.permissions']);
169
170
        $this->setTitle($title = 'User details');
171
        $this->addBreadcrumb($title);
172
173
        return $this->view('admin.users.show', compact('user'));
174
    }
175
176
    /**
177
     * Show the edit the user form.
178
     *
179
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
180
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
181
     *
182
     * @return \Illuminate\View\View
183
     */
184
    public function edit(User $user, Role $role)
185
    {
186
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
187
188
        $user->load(['roles', 'roles.permissions']);
189
        $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...
190
191
        $this->setTitle($title = 'Edit a user');
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
        $user->update($request->intersect(['username', 'email', 'password', 'first_name', 'last_name']));
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...
210
        $user->roles()->sync($request->get('roles'));
211
212
        $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...
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...
213
214
        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...
215
    }
216
217
    /**
218
     * Activate/Disable a user.
219
     *
220
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
221
     *
222
     * @return \Illuminate\Http\JsonResponse
223
     */
224
    public function activate(User $user)
225
    {
226
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
227
228
        try {
229
            ($active = $user->isActive()) ? $user->deactivate() : $user->activate();
230
231
            return $this->jsonResponseSuccess(
232
                $this->transNotification($active ? 'disabled' : 'enabled', ['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...
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...
233
            );
234
        }
235
        catch (\Exception $e) {
236
            return $this->jsonResponseError($e->getMessage(), 500);
237
        }
238
    }
239
240
    /**
241
     * Restore the trashed user.
242
     *
243
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
244
     *
245
     * @return \Illuminate\Http\JsonResponse
246
     */
247
    public function restore(User $user)
248
    {
249
        $this->authorize(UsersPolicy::PERMISSION_UPDATE);
250
251
        try {
252
            $user->restore();
253
254
            return $this->jsonResponseSuccess(
255
                $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...
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...
256
            );
257
        }
258
        catch (\Exception $e) {
259
            return $this->jsonResponseError($e->getMessage(), 500);
260
        }
261
    }
262
263
    /**
264
     * Delete a user.
265
     *
266
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
267
     *
268
     * @return \Illuminate\Http\JsonResponse
269
     */
270
    public function delete(User $user)
271
    {
272
        $this->authorize(UsersPolicy::PERMISSION_DELETE);
273
274
        try {
275
            ($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...
276
277
            return $this->jsonResponseSuccess(
278
                $this->transNotification($trashed ? 'deleted' : 'trashed', ['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...
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...
279
            );
280
        }
281
        catch(\Exception $e) {
282
            return $this->jsonResponseError($e->getMessage(), 500);
283
        }
284
    }
285
286
    /**
287
     * Impersonate a user.
288
     *
289
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
290
     *
291
     * @return \Illuminate\Http\RedirectResponse
292
     */
293
    public function impersonate(User $user)
294
    {
295
        if (UserImpersonator::start($user))
296
            return redirect()->to('/');
297
298
        $this->notifyDanger('Impersonate disabled for this user.', 'Impersonation failed');
299
300
        return redirect()->back();
301
    }
302
303
    /* -----------------------------------------------------------------
304
     |  Other Functions
305
     | -----------------------------------------------------------------
306
     */
307
    /**
308
     * Notify with translation.
309
     *
310
     * @param  string  $action
311
     * @param  array   $replace
312
     * @param  array   $context
313
     *
314
     * @return string
315
     */
316
    protected function transNotification($action, array $replace = [], array $context = [])
317
    {
318
        $title   = trans("auth::users.messages.{$action}.title");
319
        $message = trans("auth::users.messages.{$action}.message", $replace);
320
321
        Log::info($message, $context);
322
        $this->notifySuccess($message, $title);
323
324
        return $message;
325
    }
326
}
327