22
33namespace Pterodactyl \Http \Controllers \Auth ;
44
5+ use Illuminate \Support \Str ;
56use Illuminate \Http \JsonResponse ;
7+ use Illuminate \Contracts \Hashing \Hasher ;
68use Illuminate \Support \Facades \Password ;
9+ use Illuminate \Auth \Events \PasswordReset ;
10+ use Illuminate \Contracts \Events \Dispatcher ;
711use Pterodactyl \Exceptions \DisplayException ;
812use Pterodactyl \Http \Controllers \Controller ;
913use Illuminate \Foundation \Auth \ResetsPasswords ;
1014use Pterodactyl \Http \Requests \Auth \ResetPasswordRequest ;
15+ use Pterodactyl \Contracts \Repository \UserRepositoryInterface ;
1116
1217class ResetPasswordController extends Controller
1318{
@@ -20,6 +25,40 @@ class ResetPasswordController extends Controller
2025 */
2126 public $ redirectTo = '/ ' ;
2227
28+ /**
29+ * @var bool
30+ */
31+ protected $ hasTwoFactor = false ;
32+
33+ /**
34+ * @var \Illuminate\Contracts\Events\Dispatcher
35+ */
36+ private $ dispatcher ;
37+
38+ /**
39+ * @var \Illuminate\Contracts\Hashing\Hasher
40+ */
41+ private $ hasher ;
42+
43+ /**
44+ * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
45+ */
46+ private $ userRepository ;
47+
48+ /**
49+ * ResetPasswordController constructor.
50+ *
51+ * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
52+ * @param \Illuminate\Contracts\Hashing\Hasher $hasher
53+ * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
54+ */
55+ public function __construct (Dispatcher $ dispatcher , Hasher $ hasher , UserRepositoryInterface $ userRepository )
56+ {
57+ $ this ->dispatcher = $ dispatcher ;
58+ $ this ->hasher = $ hasher ;
59+ $ this ->userRepository = $ userRepository ;
60+ }
61+
2362 /**
2463 * Reset the given user's password.
2564 *
@@ -49,6 +88,35 @@ public function __invoke(ResetPasswordRequest $request): JsonResponse
4988 throw new DisplayException (trans ($ response ));
5089 }
5190
91+ /**
92+ * Reset the given user's password. If the user has two-factor authentication enabled on their
93+ * account do not automatically log them in. In those cases, send the user back to the login
94+ * form with a note telling them their password was changed and to log back in.
95+ *
96+ * @param \Illuminate\Contracts\Auth\CanResetPassword|\Pterodactyl\Models\User $user
97+ * @param string $password
98+ *
99+ * @throws \Pterodactyl\Exceptions\Model\DataValidationException
100+ * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
101+ */
102+ protected function resetPassword ($ user , $ password )
103+ {
104+ $ user = $ this ->userRepository ->update ($ user ->id , [
105+ 'password ' => $ this ->hasher ->make ($ password ),
106+ $ user ->getRememberTokenName () => Str::random (60 ),
107+ ]);
108+
109+ $ this ->dispatcher ->dispatch (new PasswordReset ($ user ));
110+
111+ // If the user is not using 2FA log them in, otherwise skip this step and force a
112+ // fresh login where they'll be prompted to enter a token.
113+ if (! $ user ->use_totp ) {
114+ $ this ->guard ()->login ($ user );
115+ }
116+
117+ $ this ->hasTwoFactor = $ user ->use_totp ;
118+ }
119+
52120 /**
53121 * Send a successful password reset response back to the callee.
54122 *
@@ -59,6 +127,7 @@ protected function sendResetResponse(): JsonResponse
59127 return response ()->json ([
60128 'success ' => true ,
61129 'redirect_to ' => $ this ->redirectTo ,
130+ 'send_to_login ' => $ this ->hasTwoFactor ,
62131 ]);
63132 }
64133}
0 commit comments