|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CraftCms\Cms\Http\Controllers\Auth; |
| 6 | + |
| 7 | +use Craft; |
| 8 | +use craft\helpers\UrlHelper; |
| 9 | +use CraftCms\Cms\Auth\Enums\CpAuthPath; |
| 10 | +use CraftCms\Cms\User\Elements\User; |
| 11 | +use Illuminate\Http\Request; |
| 12 | +use Illuminate\Support\Facades\Auth; |
| 13 | +use Illuminate\Support\Timebox; |
| 14 | +use RuntimeException; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | + |
| 17 | +use function CraftCms\Cms\cp_url; |
| 18 | + |
| 19 | +final readonly class LoginController extends AuthenticationController |
| 20 | +{ |
| 21 | + public function showLogin(Request $request) |
| 22 | + { |
| 23 | + // see if they're already logged in |
| 24 | + if ($user = $request->user()) { |
| 25 | + return $this->handleSuccessfulLogin($request, $user); |
| 26 | + } |
| 27 | + |
| 28 | + // should we be showing the 2FA form? |
| 29 | + if ($request->input('verify')) { |
| 30 | + return redirect()->action([TwoFactorAuthenticationController::class, 'showForm']); |
| 31 | + } |
| 32 | + |
| 33 | + // TODO: _rerouteWithFallbackTemplate?? |
| 34 | + return view('craftcms::login'); |
| 35 | + } |
| 36 | + |
| 37 | + public function attemptLogin(Request $request): Response |
| 38 | + { |
| 39 | + $request->validate([ |
| 40 | + 'loginName' => ['required', 'string'], |
| 41 | + 'password' => ['required', 'string'], |
| 42 | + 'rememberMe' => ['nullable'], |
| 43 | + ]); |
| 44 | + |
| 45 | + /** @var \CraftCms\Cms\Auth\UserProvider $provider */ |
| 46 | + $provider = Auth::guard('craft')->getProvider(); |
| 47 | + $user = $provider->retrieveByCredentials($request->only('loginName', 'password')); |
| 48 | + |
| 49 | + return new Timebox()->call(function () use ($request, $provider, $user) { |
| 50 | + if (! $user || $user->password === null) { |
| 51 | + return $this->handleLoginFailure($request, User::AUTH_INVALID_CREDENTIALS); |
| 52 | + } |
| 53 | + |
| 54 | + if (! $provider->validateCredentials($user, ['password' => $request->input('password')])) { |
| 55 | + return $this->handleLoginFailure($request, $user->authError, $user); |
| 56 | + } |
| 57 | + |
| 58 | + // Valid credentials |
| 59 | + if (config('hashing.rehash_on_login', true)) { |
| 60 | + $provider->rehashPasswordIfRequired($user, ['password' => $request->input('password')]); |
| 61 | + } |
| 62 | + |
| 63 | + $authService = Craft::$app->getAuth(); |
| 64 | + if (! $this->generalConfig->disable2fa && $authService->hasActiveMethod($user)) { |
| 65 | + $request->session()->put('user.id', $user->id); |
| 66 | + |
| 67 | + if (! $request->isCpRequest() && ! $request->wantsJson()) { |
| 68 | + $loginPath = $this->generalConfig->getLoginPath(); |
| 69 | + |
| 70 | + if (! $loginPath) { |
| 71 | + $request->session()->forget('user.id'); |
| 72 | + throw new RuntimeException('User requires two-step verification, but the loginPath config setting is disabled.'); |
| 73 | + } |
| 74 | + |
| 75 | + return redirect(UrlHelper::siteUrl($loginPath, array_filter([ |
| 76 | + 'verify' => 1, |
| 77 | + 'returnUrl' => $this->getPostedRedirectUrl($user), |
| 78 | + ]))); |
| 79 | + } |
| 80 | + |
| 81 | + return redirect()->action([TwoFactorAuthenticationController::class, 'showForm']); |
| 82 | + } |
| 83 | + |
| 84 | + // if we're impersonating, pass the user we're impersonating to the complete method |
| 85 | + $impersonator = Craft::$app->getUser()->getImpersonator(); |
| 86 | + if ($impersonator !== null) { |
| 87 | + $user = Auth::user() ?? $user; |
| 88 | + } |
| 89 | + |
| 90 | + return $this->completeLogin($request, $user, $request->boolean('rememberMe')); |
| 91 | + }, 30_000); |
| 92 | + } |
| 93 | + |
| 94 | + public function logout(Request $request): Response |
| 95 | + { |
| 96 | + Auth::guard('craft')->logout(); |
| 97 | + |
| 98 | + if ($request->wantsJson()) { |
| 99 | + return $this->asSuccess(); |
| 100 | + } |
| 101 | + |
| 102 | + // Redirect to the login page if this is a control panel request |
| 103 | + if ($request->isCpRequest()) { |
| 104 | + return redirect(cp_url(CpAuthPath::Login->value)); |
| 105 | + } |
| 106 | + |
| 107 | + return $this->asSuccess( |
| 108 | + redirect: $this->generalConfig->getPostLogoutRedirect() |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments