2016-09-03 21:09:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Auth;
|
|
|
|
|
2018-07-04 18:41:56 +00:00
|
|
|
use Illuminate\Support\Str;
|
2018-04-08 20:18:13 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-07-04 18:41:56 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher;
|
2018-04-08 20:18:13 +00:00
|
|
|
use Illuminate\Support\Facades\Password;
|
2018-07-04 18:41:56 +00:00
|
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2018-04-08 20:18:13 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2016-09-03 21:09:00 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Foundation\Auth\ResetsPasswords;
|
2018-04-08 20:18:13 +00:00
|
|
|
use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest;
|
2018-07-04 18:41:56 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2016-09-03 21:09:00 +00:00
|
|
|
|
|
|
|
class ResetPasswordController extends Controller
|
|
|
|
{
|
|
|
|
use ResetsPasswords;
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
|
|
|
* The URL to redirect users to after password reset.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-04 23:08:46 +00:00
|
|
|
public $redirectTo = '/';
|
|
|
|
|
2018-07-04 18:41:56 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $hasTwoFactor = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher
|
|
|
|
*/
|
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
|
|
|
*/
|
|
|
|
private $hasher;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $userRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResetPasswordController constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
|
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
|
|
|
|
*/
|
2018-07-05 01:09:07 +00:00
|
|
|
public function __construct(Dispatcher $dispatcher, Hasher $hasher, UserRepositoryInterface $userRepository)
|
2018-07-04 18:41:56 +00:00
|
|
|
{
|
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
$this->hasher = $hasher;
|
|
|
|
$this->userRepository = $userRepository;
|
|
|
|
}
|
|
|
|
|
2017-03-19 23:36:50 +00:00
|
|
|
/**
|
2018-04-08 20:18:13 +00:00
|
|
|
* Reset the given user's password.
|
2017-03-19 23:36:50 +00:00
|
|
|
*
|
2018-04-08 20:18:13 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Auth\ResetPasswordRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
|
|
|
public function __invoke(ResetPasswordRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
// Here we will attempt to reset the user's password. If it is successful we
|
|
|
|
// will update the password on an actual user model and persist it to the
|
|
|
|
// database. Otherwise we will parse the error and return the response.
|
|
|
|
$response = $this->broker()->reset(
|
|
|
|
$this->credentials($request), function ($user, $password) {
|
|
|
|
$this->resetPassword($user, $password);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// If the password was successfully reset, we will redirect the user back to
|
|
|
|
// the application's home authenticated view. If there is an error we can
|
|
|
|
// redirect them back to where they came from with their error message.
|
|
|
|
if ($response === Password::PASSWORD_RESET) {
|
|
|
|
return $this->sendResetResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new DisplayException(trans($response));
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:41:56 +00:00
|
|
|
/**
|
|
|
|
* Reset the given user's password. If the user has two-factor authentication enabled on their
|
|
|
|
* account do not automatically log them in. In those cases, send the user back to the login
|
|
|
|
* form with a note telling them their password was changed and to log back in.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\CanResetPassword|\Pterodactyl\Models\User $user
|
|
|
|
* @param string $password
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
protected function resetPassword($user, $password)
|
|
|
|
{
|
|
|
|
$user = $this->userRepository->update($user->id, [
|
|
|
|
'password' => $this->hasher->make($password),
|
|
|
|
$user->getRememberTokenName() => Str::random(60),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->dispatcher->dispatch(new PasswordReset($user));
|
|
|
|
|
|
|
|
// If the user is not using 2FA log them in, otherwise skip this step and force a
|
|
|
|
// fresh login where they'll be prompted to enter a token.
|
|
|
|
if (! $user->use_totp) {
|
|
|
|
$this->guard()->login($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->hasTwoFactor = $user->use_totp;
|
|
|
|
}
|
|
|
|
|
2018-04-08 20:18:13 +00:00
|
|
|
/**
|
|
|
|
* Send a successful password reset response back to the callee.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2017-03-19 23:36:50 +00:00
|
|
|
*/
|
2018-04-08 20:18:13 +00:00
|
|
|
protected function sendResetResponse(): JsonResponse
|
2016-12-07 22:46:38 +00:00
|
|
|
{
|
2018-04-08 20:18:13 +00:00
|
|
|
return response()->json([
|
|
|
|
'success' => true,
|
|
|
|
'redirect_to' => $this->redirectTo,
|
2018-07-05 01:09:07 +00:00
|
|
|
'send_to_login' => $this->hasTwoFactor,
|
2018-04-08 20:18:13 +00:00
|
|
|
]);
|
2016-10-30 20:02:39 +00:00
|
|
|
}
|
2016-09-03 21:09:00 +00:00
|
|
|
}
|