UsersController::updatePassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\CMS\Http\Controllers;
4
5
use App\User;
6
use Illuminate\Database\QueryException;
7
use Illuminate\Http\Request;
8
use Yajra\Acl\Models\Role;
9
use Yajra\CMS\Contracts\Validators\StoreUserValidator;
10
use Yajra\CMS\Contracts\Validators\UpdateUserValidator;
11
use Yajra\CMS\DataTables\UsersDataTable;
12
use Yajra\CMS\Events\Users\PasswordWasUpdated;
13
use Yajra\CMS\Events\Users\UserWasCreated;
14
use Yajra\CMS\Events\Users\UserWasUpdated;
15
16
class UsersController extends Controller
17
{
18
    /**
19
     * @var \Illuminate\Http\Request
20
     */
21
    protected $request;
22
23
    /**
24
     * @var \Yajra\Acl\Models\Role
25
     */
26
    protected $role;
27
28
    /**
29
     * Controller specific permission ability map.
30
     *
31
     * @var array
32
     */
33
    protected $customPermissionMap = [
34
        'activate'       => 'update',
35
        'ban'            => 'update',
36
        'forceDelete'    => 'delete',
37
        'restore'        => 'update',
38
        'password'       => 'update',
39
        'updatePassword' => 'update',
40
    ];
41
42
    /**
43
     * @param \Illuminate\Http\Request $request
44
     * @param \Yajra\Acl\Models\Role $role
45
     */
46
    public function __construct(Request $request, Role $role)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
47
    {
48
        $this->request = $request;
49
        $this->role    = $role;
50
51
        $this->authorizePermissionResource('user');
52
    }
53
54
    /**
55
     * Display list of users.
56
     *
57
     * @param \Yajra\CMS\DataTables\UsersDataTable $dataTable
58
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View
59
     */
60
    public function index(UsersDataTable $dataTable)
61
    {
62
        $roles = $this->role->pluck('name', 'id');
63
64
        return $dataTable->render('administrator.users.index', compact('roles'));
65
    }
66
67
    /**
68
     * Show user form.
69
     *
70
     * @return \Illuminate\View\View
71
     */
72
    public function create()
73
    {
74
        $roles = $this->getAllowedRoles();
75
76
        $selectedRoles = $this->request->old('roles');
77
        $user          = new User([
78
            'blocked'   => 0,
79
            'confirmed' => 1,
80
        ]);
81
82
        return view('administrator.users.create', compact('user', 'roles', 'selectedRoles'));
83
    }
84
85
    /**
86
     * Get allowed roles for the current user.
87
     *
88
     * @return mixed
89
     */
90
    protected function getAllowedRoles()
91
    {
92
        if ($this->request->user('administrator')->isRole('super-administrator')) {
93
            $roles = $this->role->get();
94
        } else {
95
            $roles = $this->role->where('slug', '!=', 'super-administrator')->get();
96
        }
97
98
        return $roles;
99
    }
100
101
    /**
102
     * Store a newly created user.
103
     *
104
     * @param \Yajra\CMS\Contracts\Validators\StoreUserValidator $request
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107
    public function store(StoreUserValidator $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        $user                = new User($this->request->all());
110
        $user->password      = bcrypt($this->request->get('password'));
111
        $user->confirmed     = $this->request->get('confirmed', false);
112
        $user->blocked       = $this->request->get('blocked', false);
113
        $user->administrator = $this->request->get('administrator', false);
114
        $user->save();
115
        $user->syncRoles($this->request->get('roles'));
116
117
        event(new UserWasCreated($user));
118
119
        flash()->success('User ' . $user->first_name . 'successfully created!');
120
121
        return redirect()->route('administrator.users.index');
122
    }
123
124
    /**
125
     * Show and edit selected user.
126
     *
127
     * @param \App\User $user
128
     * @return \Illuminate\View\View
129
     */
130
    public function edit(User $user)
131
    {
132
        $roles         = $this->getAllowedRoles();
133
        $selectedRoles = $user->roles()->pluck('roles.id')->toArray();
134
135
        return view('administrator.users.edit', compact('user', 'roles', 'selectedRoles'));
136
    }
137
138
    /**
139
     * Show and edit password.
140
     *
141
     * @param \App\User $user
142
     * @return \Illuminate\View\View
143
     */
144
    public function password(User $user)
145
    {
146
        return view('administrator.users.password', compact('user'));
147
    }
148
149
    /**
150
     * @param \App\User $user
151
     * @return \Illuminate\Http\RedirectResponse
152
     */
153
    public function updatePassword(User $user)
154
    {
155
        $this->validate($this->request, [
156
            'password'              => 'required|min:4|confirmed',
157
            'password_confirmation' => 'required|min:4',
158
        ]);
159
160
        $password       = $this->request->get('password');
161
        $user->password = bcrypt($password);
162
        $user->save();
163
164
        event(new PasswordWasUpdated($user, $password));
165
166
        flash()->success($user->name . "'s password successfully updated!");
167
168
        return redirect()->route('administrator.users.index');
169
    }
170
171
    /**
172
     * @param \App\User $user
173
     * @return \Illuminate\View\View
174
     */
175
    public function show(User $user)
176
    {
177
        return view('administrator.users.profile', compact('user'));
178
    }
179
180
    /**
181
     * @param \App\User $user
182
     * @param \Yajra\CMS\Contracts\Validators\UpdateUserValidator $request
183
     * @return \Illuminate\Http\RedirectResponse
184
     */
185
    public function update(User $user, UpdateUserValidator $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        $user->fill($this->request->except('password'));
188
        $password = $this->request->get('password');
189
        if (! empty($password)) {
190
            $user->password = bcrypt($password);
191
        }
192
        $user->confirmed     = $this->request->get('confirmed', false);
193
        $user->blocked       = $this->request->get('blocked', false);
194
        $user->administrator = $this->request->get('administrator', false);
195
        $user->save();
196
        $user->syncRoles($this->request->get('roles'));
197
198
        event(new UserWasUpdated($user));
199
200
        flash()->success('User ' . $user->first_name . ' successfully updated!');
201
202
        return redirect()->route('administrator.users.index');
203
    }
204
205
    /**
206
     * @param int $id
207
     * @return \Illuminate\Http\RedirectResponse
208
     */
209
    public function forceDelete($id)
210
    {
211
        $user = User::withTrashed()->findOrFail($id);
212
213
        return $this->delete($user, true);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Yajra\CMS\Http\Controllers\UsersController. 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...
214
    }
215
216
    /**
217
     * Remove selected user.
218
     *
219
     * @param \App\User $user
220
     * @param bool|false $force
221
     * @return \Illuminate\Http\JsonResponse
222
     * @throws \Exception
223
     */
224
    public function destroy(User $user, $force = false)
225
    {
226
        if ($user->id <> auth('administrator')->id()) {
0 ignored issues
show
Bug introduced by
The method id 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...
227
            try {
228
                if ($force) {
229
                    $user->forceDelete();
230
                } else {
231
                    $user->delete();
232
                }
233
234
                return $this->notifySuccess('User successfully deleted!');
235
            } catch (QueryException $e) {
236
                return $this->notifyError($e->getMessage());
237
            }
238
        }
239
240
        return $this->notifyError('You cannot delete your own record!');
241
    }
242
243
    /**
244
     * @param int $id
245
     * @return \Illuminate\Http\JsonResponse
246
     */
247
    public function restore($id)
248
    {
249
        $user = User::withTrashed()->findOrFail($id);
250
        $user->restore();
251
252
        return $this->notifySuccess($user->name . ' successfully restored!');
253
    }
254
255
    /**
256
     * Toggle ban status of a user.
257
     *
258
     * @param \App\User $user
259
     * @return \Illuminate\Http\JsonResponse
260
     */
261 View Code Duplication
    public function ban(User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263
        $user->blocked = ! $user->blocked;
264
        $user->save();
265
266
        if ($user->blocked) {
267
            return $this->notifySuccess('User ' . $user->name . ' blocked!');
268
        } else {
269
            return $this->notifySuccess('User ' . $user->name . ' un-blocked!');
270
        }
271
    }
272
273
    /**
274
     * Toggle user activation status.
275
     *
276
     * @param \App\User $user
277
     * @return \Illuminate\Http\JsonResponse
278
     */
279 View Code Duplication
    public function activate(User $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
    {
281
        $user->confirmed = ! $user->confirmed;
282
        $user->save();
283
284
        if ($user->confirmed) {
285
            return $this->notifySuccess('User ' . $user->name . ' activated!');
286
        } else {
287
            return $this->notifySuccess('User ' . $user->name . ' deactivated!');
288
        }
289
    }
290
}
291