Path: blob/1.0-develop/app/Http/Controllers/Auth/ResetPasswordController.php
14044 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\Events\User\PasswordChanged;11use Pterodactyl\Exceptions\DisplayException;12use Pterodactyl\Http\Controllers\Controller;13use Illuminate\Foundation\Auth\ResetsPasswords;14use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest;15use Pterodactyl\Contracts\Repository\UserRepositoryInterface;1617class ResetPasswordController extends Controller18{19use ResetsPasswords;2021/**22* The URL to redirect users to after password reset.23*/24public string $redirectTo = '/';2526protected bool $hasTwoFactor = false;2728/**29* ResetPasswordController constructor.30*/31public function __construct(32private Dispatcher $dispatcher,33private Hasher $hasher,34private UserRepositoryInterface $userRepository,35) {36}3738/**39* Reset the given user's password.40*41* @throws DisplayException42*/43public function __invoke(ResetPasswordRequest $request): JsonResponse44{45// Here we will attempt to reset the user's password. If it is successful we46// will update the password on an actual user model and persist it to the47// database. Otherwise, we will parse the error and return the response.48$response = $this->broker()->reset(49$this->credentials($request),50function ($user, $password) {51$this->resetPassword($user, $password);52}53);5455// If the password was successfully reset, we will redirect the user back to56// the application's home authenticated view. If there is an error we can57// redirect them back to where they came from with their error message.58if ($response === Password::PASSWORD_RESET) {59return $this->sendResetResponse();60}6162throw new DisplayException(trans($response));63}6465/**66* Reset the given user's password. If the user has two-factor authentication enabled on their67* account do not automatically log them in. In those cases, send the user back to the login68* form with a note telling them their password was changed and to log back in.69*70* @param \Illuminate\Contracts\Auth\CanResetPassword&\Pterodactyl\Models\User $user71* @param string $password72*73* @throws \Pterodactyl\Exceptions\Model\DataValidationException74* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException75*/76protected function resetPassword($user, $password)77{78$user = $this->userRepository->update($user->id, [79'password' => $this->hasher->make($password),80$user->getRememberTokenName() => Str::random(60),81]);8283$this->dispatcher->dispatch(new PasswordReset($user));84PasswordChanged::dispatch($user);8586// If the user is not using 2FA log them in, otherwise skip this step and force a87// fresh login where they'll be prompted to enter a token.88if (!$user->use_totp) {89$this->guard()->login($user);90}9192$this->hasTwoFactor = $user->use_totp;93}9495/**96* Send a successful password reset response back to the callee.97*/98protected function sendResetResponse(): JsonResponse99{100return response()->json([101'success' => true,102'redirect_to' => $this->redirectTo,103'send_to_login' => $this->hasTwoFactor,104]);105}106}107108109