diff --git a/app/Services/ResetsPasswords.php b/app/Services/ResetsPasswords.php index d84ca05..251e521 100644 --- a/app/Services/ResetsPasswords.php +++ b/app/Services/ResetsPasswords.php @@ -1,309 +1,308 @@ sendResetLinkEmail($request); } /** * Display the password reset view for the given token. * * @param string $token * @return Response */ public function getReset($token = null) { if (is_null($token)) { throw new NotFoundHttpException; } return view('auth.reset')->with('token', $token); } /** * Reset the given user's password. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postReset(Request $request) { return $this->reset($request); } /** * Get the response for after a successful password reset. * * @param string $response * @return \Symfony\Component\HttpFoundation\Response */ protected function getResetSuccessResponse($response) { return redirect($this->redirectPath()) ->with('status', trans($response)); } /** * Get the response for after a failing password reset. * * @param Request $request * @param string $response * @return \Symfony\Component\HttpFoundation\Response */ protected function getResetFailureResponse(Request $request, $response) { return redirect()->back() ->withInput($request->only('email')) ->withErrors(['email' => trans($response)]); } /// /// Controller helper utilities /// /** * Get the name of the guest middleware. * * @return string */ protected function guestMiddleware() { $guard = $this->getGuard(); return $guard ? 'guest:' . $guard : 'guest'; } /** * Get the broker to be used during password reset. * * @return string|null */ public function getBroker() { return property_exists($this, 'broker') ? $this->broker : null; } /** * Get the guard to be used during password reset. * * @return string|null */ protected function getGuard() { return property_exists($this, 'guard') ? $this->guard : null; } /// /// Reset password e-mail helper methods /// /** * Send a reset link to the given user. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function sendResetLinkEmail(Request $request) { $this->validateSendResetLinkEmail($request); $broker = $this->getBroker(); $response = Password::broker($broker)->sendResetLink( $this->getSendResetLinkEmailCredentials($request), $this->resetEmailBuilder() ); switch ($response) { case Password::RESET_LINK_SENT: return $this->getSendResetLinkEmailSuccessResponse($response); case Password::INVALID_USER: default: return $this->getSendResetLinkEmailFailureResponse($response); } } /** * Validate the request of sending reset link. * * @param \Illuminate\Http\Request $request * @return void */ protected function validateSendResetLinkEmail(Request $request) { $this->validate($request, ['email' => 'required|email']); } /** * Get the needed credentials for sending the reset link. * * @param \Illuminate\Http\Request $request * @return array */ protected function getSendResetLinkEmailCredentials(Request $request) { return $request->only('email'); } /** * Get the Closure which is used to build the password reset email message. * * @return \Closure */ protected function resetEmailBuilder() { return function (Message $message) { $message->subject($this->getEmailSubject()); }; } /** * Get the e-mail subject line to be used for the reset link email. * * @return string */ protected function getEmailSubject() { return property_exists($this, 'subject') ? $this->subject : trans('emails.reset-password-subject'); } /** * Get the response for after the reset link has been successfully sent. * * @param string $response * @return \Symfony\Component\HttpFoundation\Response */ protected function getSendResetLinkEmailSuccessResponse($response) { return redirect()->back()->with('status', trans($response)); } /** * Get the response for after the reset link could not be sent. * * @param string $response * @return \Symfony\Component\HttpFoundation\Response */ protected function getSendResetLinkEmailFailureResponse($response) { return redirect()->back()->withErrors(['email' => trans($response)]); } /// /// Helper methods to handle password reset /// /** * Reset the given user's password. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function reset(Request $request) { $this->validate( $request, $this->getResetValidationRules(), $this->getResetValidationMessages(), $this->getResetValidationCustomAttributes() ); $credentials = $this->getResetCredentials($request); $broker = $this->getBroker(); $response = Password::broker($broker)->reset($credentials, function ($user, $password) { $this->resetPassword($user, $password); }); switch ($response) { case Password::PASSWORD_RESET: return $this->getResetSuccessResponse($response); default: return $this->getResetFailureResponse($request, $response); } } /** * Get the password reset validation rules. * * @return array */ protected function getResetValidationRules() { return [ 'token' => 'required', 'email' => 'required|email', 'password' => 'required|confirmed|min:8', ]; } /** * Get the password reset validation messages. * * @return array */ protected function getResetValidationMessages() { return []; } /** * Get the password reset validation custom attributes. * * @return array */ protected function getResetValidationCustomAttributes() { return []; } /** * Get the password reset credentials from the request. * * @param \Illuminate\Http\Request $request * @return array */ protected function getResetCredentials(Request $request) { return $request->only( 'email', 'password', 'password_confirmation', 'token' ); } /** * Reset the given user's password. * * @param \Illuminate\Contracts\Auth\CanResetPassword $user * @param string $password * @return void */ protected function resetPassword(CanResetPassword $user, $password) { $user->forceFill([ 'password' => bcrypt($password), 'remember_token' => Str::random(60), ])->save(); Auth::guard($this->getGuard())->login($user); } }