Path: blob/1.0-develop/app/Http/Controllers/Auth/ResetPasswordController.php
10284 views
<?php12namespace Pterodactyl\Http\Controllers\Auth;34use Illuminate\Support\Str;5use Illuminate\Http\JsonResponse;6use Illuminate\Contracts\Hashing\Hasher;7use Illuminate\Support\Facades\Password;8use Illuminate\Auth\Events\PasswordReset;9use Illuminate\Contracts\Events\Dispatcher;10use Pterodactyl\Exceptions\DisplayException;11use Pterodactyl\Http\Controllers\Controller;12use Illuminate\Foundation\Auth\ResetsPasswords;13use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest;14use Pterodactyl\Contracts\Repository\UserRepositoryInterface;1516class ResetPasswordController extends Controller17{18use ResetsPasswords;1920/**21* The URL to redirect users to after password reset.22*/23public string $redirectTo = '/';2425protected bool $hasTwoFactor = false;2627/**28* ResetPasswordController constructor.29*/30public function __construct(31private Dispatcher $dispatcher,32private Hasher $hasher,33private UserRepositoryInterface $userRepository,34) {35}3637/**38* Reset the given user's password.39*40* @throws DisplayException41*/42public function __invoke(ResetPasswordRequest $request): JsonResponse43{44// Here we will attempt to reset the user's password. If it is successful we45// will update the password on an actual user model and persist it to the46// database. Otherwise, we will parse the error and return the response.47$response = $this->broker()->reset(48$this->credentials($request),49function ($user, $password) {50$this->resetPassword($user, $password);51}52);5354// If the password was successfully reset, we will redirect the user back to55// the application's home authenticated view. If there is an error we can56// redirect them back to where they came from with their error message.57if ($response === Password::PASSWORD_RESET) {58return $this->sendResetResponse();59}6061throw new DisplayException(trans($response));62}6364/**65* Reset the given user's password. If the user has two-factor authentication enabled on their66* account do not automatically log them in. In those cases, send the user back to the login67* form with a note telling them their password was changed and to log back in.68*69* @param \Illuminate\Contracts\Auth\CanResetPassword&\Pterodactyl\Models\User $user70* @param string $password71*72* @throws \Pterodactyl\Exceptions\Model\DataValidationException73* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException74*/75protected function resetPassword($user, $password)76{77$user = $this->userRepository->update($user->id, [78'password' => $this->hasher->make($password),79$user->getRememberTokenName() => Str::random(60),80]);8182$this->dispatcher->dispatch(new PasswordReset($user));8384// If the user is not using 2FA log them in, otherwise skip this step and force a85// fresh login where they'll be prompted to enter a token.86if (!$user->use_totp) {87$this->guard()->login($user);88}8990$this->hasTwoFactor = $user->use_totp;91}9293/**94* Send a successful password reset response back to the callee.95*/96protected function sendResetResponse(): JsonResponse97{98return response()->json([99'success' => true,100'redirect_to' => $this->redirectTo,101'send_to_login' => $this->hasTwoFactor,102]);103}104}105106107