Completed
Pull Request — master (#88)
by Owen
01:58
created

EditAdminProfileController::updatePassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 23
rs 9.0856
cc 2
eloc 16
nc 2
nop 1
1
<?php
2
3
namespace Backpack\Base\app\Http\Controllers\Auth;
4
5
use Auth;
6
use Backpack\Base\app\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Hash;
9
use Illuminate\Validation\Rule;
10
use Validator;
11
12
class EditAdminProfileController extends Controller
13
{
14
    protected $data = [];
15
16
    public function __construct()
17
    {
18
        if (config('backpack.base.separate_admin_session')) {
19
            $this->middleware('backpack.admin.guard');
20
        } else {
21
            $this->middleware('admin');
22
        }
23
    }
24
25
    public function showEditForm()
26
    {
27
        $this->data['title'] = trans('backpack::base.edit_account');
28
        $this->data['user'] = Auth::guard(config('backpack.base.admin_guard.name'))->user();
29
30
        return view('backpack::auth.edit', $this->data);
31
    }
32
33
    public function update(Request $request)
34
    {
35
        $user = Auth::guard(config('backpack.base.admin_guard.name'))->user();
36
37
        $validator = Validator::make($request->all(), [
38
            'email' => [
39
                'required',
40
                'email',
41
                Rule::unique($user->getTable())->ignore($user->getKey()),
42
            ],
43
            'name' => 'required',
44
        ]);
45
46
        if ($validator->fails()) {
47
            return redirect()
48
            ->route('backpack.profile.edit')
49
            ->withErrors($validator)
50
            ->withInput($request->all());
51
        } else {
52
            $data = $request->except(['_token']);
53
            $user->update($data);
54
55
            return redirect()
56
            ->route('backpack.profile.edit')
57
            ->with('success', trans('backpack::base.account_updated'));
58
        }
59
    }
60
61
    public function updatePassword(Request $request)
62
    {
63
        $validator = Validator::make($request->all(), [
64
            'password'         => 'required|min:6',
65
            'confirm_password' => 'required|same:password|min:6',
66
        ]);
67
68
        if ($validator->fails()) {
69
            return response([
0 ignored issues
show
Documentation introduced by
array('success' => false...tor->errors()->first()) is of type array<string,?,{"success":"false","message":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
                'success' => false,
71
                'message' => $validator->errors()->first(),
72
            ], 422);
73
        } else {
74
            $user = Auth::guard(config('backpack.base.admin_guard.name'))->user();
75
            $user->password = Hash::make($request->password);
76
            $user->save();
77
78
            return response([
0 ignored issues
show
Documentation introduced by
array('success' => true,...ase.password_updated')) is of type array<string,boolean|obj...torInterface>|string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
                'success' => true,
80
                'message' => trans('backpack::base.password_updated'),
81
            ]);
82
        }
83
    }
84
}
85