| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 5 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 17
PasswordController | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 5 | 42.00 | 0.00% covered (danger) | 0.00% | 0 / 17
__construct | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 2
change | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1
update | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 8
rules | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 5
validationErrorMessages | 0.00% covered (danger) | 0.00% | 0 / 1 | 2.00 | 0.00% covered (danger) | 0.00% | 0 / 1

1 | <?php
2 |
3 | namespace App\Http\Controllers\Auth;
4 |
5 | use App\Http\Controllers\Controller;
6 | use Illuminate\Auth\Events\PasswordReset;
7 | use Illuminate\Foundation\Auth\ResetsPasswords;
8 | use Illuminate\Http\Request;
9 | use Illuminate\Support\Facades\Auth;
10 | use Illuminate\Support\Facades\Hash;
11 | use Illuminate\Support\Str;
12 |
13 | class PasswordController extends Controller
14 | {
15 |
16 |
17 | /**
18 | * Where to redirect users after resetting their password.
19 | *
20 | * @var string
21 | */
22 | protected $redirectTo = '/home';
23 |
24 | /**
25 | * Create a new controller instance.
26 | *
27 | * @return void
28 | */
29 | public function __construct()
30 | {
31 | $this->middleware('auth');
32 | }
33 | public function change()
34 | {
35 | return view('auth.passwords.change');
36 | }
37 | public function update(Request $request)
38 | {
39 | $user=Auth::user();
40 | $request->validate($this->rules(), $this->validationErrorMessages());
41 | $user->password = Hash::make($request->input('password'));
42 | $user->setRememberToken(Str::random(60));
43 | $user->save();
44 | event(new PasswordReset($user));
45 | Auth::guard()->login($user);
46 | return view('auth.passwords.changed');
47 | }
48 | protected function rules()
49 | {
50 | return [
51 | 'old_password' => ['required',function($attr,$val,$fail){
52 | $oldPwd=Auth::user()['password'];
53 | if(!Hash::check($val,$oldPwd)){
54 | $fail('旧密码输入不匹配');
55 | }
56 | }],
57 | 'password' => 'required|confirmed|min:8',
58 | ];
59 | }
60 | /**
61 | * Get the password reset validation error messages.
62 | *
63 | * @return array
64 | */
65 | protected function validationErrorMessages()
66 | {
67 | return [
68 | 'old_password.required'=>'请输入正确的旧密码',
69 | 'password.required'=>'不得为空',
70 | 'password.min'=>'密码至少要输入 :min 位或以上',
71 | 'password.confirmed'=>'两次密码必须一致'
72 | ];
73 | }
74 | }