Completed
Push — master ( f40498...e3ae3a )
by ARCANEDEV
03:17
created

UsersController::listByRole()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4286
cc 2
eloc 6
nc 2
nop 2
crap 6
1
<?php namespace Arcanesoft\Auth\Http\Controllers\Foundation;
2
3
use Arcanesoft\Auth\Bases\FoundationController;
4
use Arcanesoft\Auth\Http\Requests\Backend\Users\CreateUserRequest;
5
use Arcanesoft\Auth\Http\Requests\Backend\Users\UpdateUserRequest;
6
use Arcanesoft\Contracts\Auth\Models\Role;
7
use Arcanesoft\Contracts\Auth\Models\User;
8
use Log;
9
10
/**
11
 * Class     UsersController
12
 *
13
 * @package  Arcanesoft\Auth\Http\Controllers\Foundation
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @todo: Adding the authorization checks
17
 */
18
class UsersController extends FoundationController
19
{
20
    /* ------------------------------------------------------------------------------------------------
21
     |  Properties
22
     | ------------------------------------------------------------------------------------------------
23
     */
24
    /**
25
     * The user model.
26
     *
27
     * @var  \Arcanesoft\Contracts\Auth\Models\User
28
     */
29
    protected $user;
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Constructor
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * Instantiate the controller.
37
     *
38
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
39
     */
40
    public function __construct(User $user)
41
    {
42
        parent::__construct();
43
44
        $this->user = $user;
45
46
        $this->setCurrentPage('auth-users');
47
        $this->addBreadcrumbRoute('Users', 'auth::foundation.users.index');
48
    }
49
50
    /* ------------------------------------------------------------------------------------------------
51
     |  Main Functions
52
     | ------------------------------------------------------------------------------------------------
53
     */
54
    /**
55
     * List the users.
56
     *
57
     * @param  bool  $trashed
58
     *
59
     * @return \Illuminate\View\View
60
     */
61
    public function index($trashed = false)
62
    {
63
        $users = $this->user->with('roles');
64
65
        $users = $trashed
66
            ? $users->onlyTrashed()->paginate(30)
67
            : $users->paginate(30);
68
69
        $title = 'List of users' . ($trashed ? ' - Trashed' : '');
70
        $this->setTitle($title);
71
        $this->addBreadcrumb($title);
72
73
        return $this->view('foundation.users.list', compact('trashed', 'users'));
74
    }
75
76
    /**
77
     * List the trashed users.
78
     *
79
     * @return \Illuminate\View\View
80
     */
81
    public function trashList()
82
    {
83
        return $this->index(true);
84
    }
85
86
    /**
87
     * List the users by a role.
88
     *
89
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
90
     * @param  bool                                    $trashed
91
     *
92
     * @return \Illuminate\View\View
93
     */
94
    public function listByRole(Role $role, $trashed = false)
95
    {
96
        $users = $role->users()->with('roles')->paginate(30);
97
98
        $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...
99
        $this->setTitle($title);
100
        $this->addBreadcrumb($title);
101
102
        return $this->view('foundation.users.list', compact('trashed', 'users'));
103
    }
104
105
    /**
106
     * Show the create a new user form.
107
     *
108
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
109
     *
110
     * @return \Illuminate\View\View
111
     */
112
    public function create(Role $role)
113
    {
114
        $roles = $role->all();
115
116
        $title = 'Create a new user';
117
        $this->setTitle($title);
118
        $this->addBreadcrumb($title);
119
120
        return $this->view('foundation.users.create', compact('roles'));
121
    }
122
123
    /**
124
     * Store the new user.
125
     *
126
     * @param  \Arcanesoft\Auth\Http\Requests\Backend\Users\CreateUserRequest  $request
127
     * @param  \Arcanesoft\Contracts\Auth\Models\User                          $user
128
     *
129
     * @return \Illuminate\Http\RedirectResponse
130
     */
131
    public function store(CreateUserRequest $request, User $user)
132
    {
133
        $data = $request->only([
134
            'username', 'email', 'first_name', 'last_name', 'password'
135
        ]);
136
        $user->fill($data);
137
        $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...
138
        $user->save();
139
        $user->roles()->sync($request->get('roles'));
140
141
        $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...
142
        Log::info($message, $user->toArray());
143
        $this->notifySuccess($message, 'User created !');
144
145
        return redirect()->route('auth::foundation.users.index');
146
    }
147
148
    /**
149
     * Show the user's details.
150
     *
151
     * @param \Arcanesoft\Contracts\Auth\Models\User $user
152
     *
153
     * @return \Illuminate\View\View
154
     */
155
    public function show(User $user)
156
    {
157
        $user->load(['roles', 'roles.permissions']);
158
159
        $title = 'User details';
160
        $this->setTitle($title);
161
        $this->addBreadcrumb($title);
162
163
        return $this->view('foundation.users.show', compact('user'));
164
    }
165
166
    /**
167
     * Show the edit the user form.
168
     *
169
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
170
     * @param  \Arcanesoft\Contracts\Auth\Models\Role  $role
171
     *
172
     * @return \Illuminate\View\View
173
     */
174
    public function edit(User $user, Role $role)
175
    {
176
        $user->load(['roles', 'roles.permissions']);
177
        $roles = $role->all();
178
179
        $title = 'Edit a user';
180
        $this->setTitle($title);
181
        $this->addBreadcrumb($title);
182
183
        return $this->view('foundation.users.edit', compact('user', 'roles'));
184
    }
185
186
    /**
187
     * Update the user.
188
     *
189
     * @param  \Arcanesoft\Auth\Http\Requests\Backend\Users\UpdateUserRequest  $request
190
     * @param  \Arcanesoft\Contracts\Auth\Models\User                          $user
191
     *
192
     * @return \Illuminate\Http\RedirectResponse
193
     */
194
    public function update(UpdateUserRequest $request, User $user)
195
    {
196
        $inputs = ['username', 'email', 'first_name', 'last_name'];
197
198
        if ($request->has('password')) {
199
            $inputs[] = 'password';
200
        }
201
202
        $user->update($request->only($inputs));
203
        $user->roles()->sync($request->get('roles'));
204
205
        $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...
206
        Log::info($message, $user->toArray());
207
        $this->notifySuccess($message, 'User Updated !');
208
209
        return redirect()->route('auth::foundation.users.show', [
210
            $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...
211
        ]);
212
    }
213
214
    /**
215
     * Activate/Disable a user.
216
     *
217
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
218
     *
219
     * @return \Illuminate\Http\JsonResponse
220
     */
221
    public function activate(User $user)
222
    {
223
        self::onlyAjax();
224
225
        try {
226
            if ($user->isActive()) {
227
                $title   = 'User disabled !';
228
                $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...
229
                $user->deactivate();
230
            }
231
            else {
232
                $title   = 'User activated !';
233
                $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...
234
                $user->activate();
235
            }
236
237
            Log::info($message, $user->toArray());
238
            $this->notifySuccess($message, $title);
239
240
            $ajax = [
241
                'status'  => 'success',
242
                'message' => $message,
243
            ];
244
        }
245
        catch (\Exception $e) {
246
            $ajax = [
247
                'status'  => 'error',
248
                'message' => $e->getMessage(),
249
            ];
250
        }
251
252
        return response()->json($ajax);
253
    }
254
255
    /**
256
     * Restore the trashed user.
257
     *
258
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
259
     *
260
     * @return \Illuminate\Http\JsonResponse
261
     */
262
    public function restore(User $user)
263
    {
264
        self::onlyAjax();
265
266
        try {
267
            $user->restore();
268
269
            $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...
270
            Log::info($message, $user->toArray());
271
            $this->notifySuccess($message, 'User restored !');
272
273
            $ajax = [
274
                'status'  => 'success',
275
                'message' => $message,
276
            ];
277
        }
278
        catch (\Exception $e) {
279
            $ajax = [
280
                'status'  => 'error',
281
                'message' => $e->getMessage(),
282
            ];
283
        }
284
285
        return response()->json($ajax);
286
    }
287
288
    /**
289
     * Delete a user.
290
     *
291
     * @param  \Arcanesoft\Contracts\Auth\Models\User  $user
292
     *
293
     * @return \Illuminate\Http\JsonResponse
294
     */
295
    public function delete(User $user)
296
    {
297
        self::onlyAjax();
298
299
        try {
300
            if ($user->trashed()) {
301
                $user->forceDelete();
302
                $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...
303
                Log::info($message, $user->toArray());
304
            }
305
            else {
306
                $user->delete();
307
                $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...
308
            }
309
310
            $this->notifySuccess($message, 'User deleted !');
311
312
            $ajax = [
313
                'status'  => 'success',
314
                'message' => $message,
315
            ];
316
        }
317
        catch(\Exception $e) {
318
            $ajax = [
319
                'status'  => 'error',
320
                'message' => $e->getMessage(),
321
            ];
322
        }
323
324
        return response()->json($ajax);
325
    }
326
}
327