| Conditions | 21 |
| Paths | 141 |
| Total Lines | 168 |
| Code Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 62 | #[Route('/login_json', name: 'login_json', methods: ['POST'])] |
||
| 63 | public function loginJson( |
||
| 64 | Request $request, |
||
| 65 | TokenStorageInterface $tokenStorage, |
||
| 66 | TranslatorInterface $translator, |
||
| 67 | ): Response { |
||
| 68 | if (!$this->isGranted('IS_AUTHENTICATED_FULLY')) { |
||
| 69 | throw $this->createAccessDeniedException($translator->trans('Invalid login request: check that the Content-Type header is <em>application/json</em>.')); |
||
| 70 | } |
||
| 71 | |||
| 72 | $dataRequest = json_decode($request->getContent(), true); |
||
| 73 | if (!\is_array($dataRequest)) { |
||
| 74 | $dataRequest = []; |
||
| 75 | } |
||
| 76 | |||
| 77 | $rememberRequested = (bool) ($dataRequest['_remember_me'] ?? false); |
||
| 78 | |||
| 79 | $user = $this->userHelper->getCurrent(); |
||
| 80 | |||
| 81 | if (User::ACTIVE !== $user->getActive()) { |
||
| 82 | if (User::INACTIVE === $user->getActive()) { |
||
| 83 | $message = $translator->trans('Your account has not been activated.'); |
||
| 84 | } else { |
||
| 85 | $message = $translator->trans('Invalid credentials. Please try again or contact support if you continue to experience issues.'); |
||
| 86 | } |
||
| 87 | |||
| 88 | $tokenStorage->setToken(null); |
||
| 89 | $request->getSession()->invalidate(); |
||
| 90 | |||
| 91 | return $this->createAccessDeniedException($message); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($user->getMfaEnabled()) { |
||
| 95 | $totpCode = $dataRequest['totp'] ?? null; |
||
| 96 | |||
| 97 | if (null === $totpCode) { |
||
| 98 | $tokenStorage->setToken(null); |
||
| 99 | $request->getSession()->invalidate(); |
||
| 100 | |||
| 101 | return $this->json(['requires2FA' => true], 200); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!$this->isTOTPValid($user, $totpCode)) { |
||
| 105 | $tokenStorage->setToken(null); |
||
| 106 | $request->getSession()->invalidate(); |
||
| 107 | |||
| 108 | return $this->json(['error' => 'Invalid 2FA code.'], 401); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if (null !== $user->getExpirationDate() && $user->getExpirationDate() <= new DateTime()) { |
||
| 113 | $message = $translator->trans('Your account has expired.'); |
||
| 114 | |||
| 115 | $tokenStorage->setToken(null); |
||
| 116 | $request->getSession()->invalidate(); |
||
| 117 | |||
| 118 | return $this->createAccessDeniedException($message); |
||
| 119 | } |
||
| 120 | |||
| 121 | $extraFieldValuesRepository = $this->entityManager->getRepository(ExtraFieldValues::class); |
||
| 122 | $legalTermsRepo = $this->entityManager->getRepository(Legal::class); |
||
| 123 | if ( |
||
| 124 | $user->isStudent() |
||
| 125 | && 'true' === $this->settingsManager->getSetting('allow_terms_conditions', true) |
||
| 126 | && 'login' === $this->settingsManager->getSetting('workflows.load_term_conditions_section', true) |
||
| 127 | ) { |
||
| 128 | $termAndConditionStatus = false; |
||
| 129 | $extraValue = $extraFieldValuesRepository->findLegalAcceptByItemId($user->getId()); |
||
| 130 | if (!empty($extraValue['value'])) { |
||
| 131 | $result = $extraValue['value']; |
||
| 132 | $userConditions = explode(':', $result); |
||
| 133 | $version = $userConditions[0]; |
||
| 134 | $langId = (int) $userConditions[1]; |
||
| 135 | $realVersion = $legalTermsRepo->getLastVersion($langId); |
||
| 136 | $termAndConditionStatus = ($version >= $realVersion); |
||
| 137 | } |
||
| 138 | |||
| 139 | if (false === $termAndConditionStatus) { |
||
| 140 | $tempTermAndCondition = ['user_id' => $user->getId()]; |
||
| 141 | $this->tokenStorage->setToken(null); |
||
| 142 | $request->getSession()->invalidate(); |
||
| 143 | $request->getSession()->start(); |
||
| 144 | $request->getSession()->set('term_and_condition', $tempTermAndCondition); |
||
| 145 | |||
| 146 | $afterLogin = $this->getRedirectAfterLoginPath($user); |
||
| 147 | |||
| 148 | return $this->json([ |
||
| 149 | 'load_terms' => true, |
||
| 150 | 'redirect' => '/main/auth/tc.php?return='.urlencode($afterLogin), |
||
| 151 | ]); |
||
| 152 | } |
||
| 153 | $request->getSession()->remove('term_and_condition'); |
||
| 154 | } |
||
| 155 | |||
| 156 | $redirectUrl = $this->calculateRedirectUrl( |
||
| 157 | $user, |
||
| 158 | $this->entityManager->getRepository(Course::class), |
||
| 159 | ); |
||
| 160 | |||
| 161 | if (null !== $redirectUrl) { |
||
| 162 | return $this->json([ |
||
| 163 | 'redirect' => $redirectUrl, |
||
| 164 | ]); |
||
| 165 | } |
||
| 166 | |||
| 167 | // Password rotation check |
||
| 168 | $days = (int) $this->settingsManager->getSetting('security.password_rotation_days', true); |
||
| 169 | if ($days > 0) { |
||
| 170 | $lastUpdate = $user->getPasswordUpdatedAt() ?? $user->getCreatedAt(); |
||
| 171 | $diffDays = (new DateTimeImmutable())->diff($lastUpdate)->days; |
||
|
|
|||
| 172 | |||
| 173 | if ($diffDays > $days) { |
||
| 174 | // Clean token & session |
||
| 175 | $tokenStorage->setToken(null); |
||
| 176 | $request->getSession()->invalidate(); |
||
| 177 | |||
| 178 | return $this->json([ |
||
| 179 | 'rotate_password' => true, |
||
| 180 | 'redirect' => '/account/change-password?rotate=1&userId='.$user->getId(), |
||
| 181 | ]); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | $data = null; |
||
| 186 | if ($user) { |
||
| 187 | $data = $this->serializer->serialize($user, 'jsonld', ['groups' => ['user_json:read']]); |
||
| 188 | } |
||
| 189 | |||
| 190 | $response = new JsonResponse($data, Response::HTTP_OK, [], true); |
||
| 191 | |||
| 192 | // Remember Me: only on HTTPS. |
||
| 193 | if ($rememberRequested && $request->isSecure()) { |
||
| 194 | $ttlSeconds = 1209600; // 14 days |
||
| 195 | |||
| 196 | // Opportunistic cleanup of expired remember-me tokens. |
||
| 197 | /** @var ValidationTokenRepository $validationTokenRepo */ |
||
| 198 | $validationTokenRepo = $this->entityManager->getRepository(ValidationToken::class); |
||
| 199 | $validationTokenRepo->deleteExpiredRememberMeTokens((new DateTimeImmutable())->modify('-'.$ttlSeconds.' seconds')); |
||
| 200 | |||
| 201 | $rawToken = rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '='); |
||
| 202 | $hash = hash('sha256', $rawToken); |
||
| 203 | |||
| 204 | $tokenEntity = new ValidationToken(ValidationTokenHelper::TYPE_REMEMBER_ME, (int) $user->getId(), $hash); |
||
| 205 | $validationTokenRepo->save($tokenEntity, true); |
||
| 206 | |||
| 207 | $secret = (string) $this->getParameter('kernel.secret'); |
||
| 208 | $sigRaw = hash_hmac('sha256', (string) $user->getId().'|'.$rawToken, $secret, true); |
||
| 209 | $sig = rtrim(strtr(base64_encode($sigRaw), '+/', '-_'), '='); |
||
| 210 | |||
| 211 | $cookieValue = $user->getId().':'.$rawToken.':'.$sig; |
||
| 212 | $expiresAt = (new DateTimeImmutable())->modify('+'.$ttlSeconds.' seconds'); |
||
| 213 | |||
| 214 | $cookie = new Cookie( |
||
| 215 | 'ch_remember_me', |
||
| 216 | $cookieValue, |
||
| 217 | $expiresAt->getTimestamp(), |
||
| 218 | '/', |
||
| 219 | null, |
||
| 220 | true, // Secure |
||
| 221 | true, // HttpOnly |
||
| 222 | false, |
||
| 223 | Cookie::SAMESITE_STRICT |
||
| 224 | ); |
||
| 225 | |||
| 226 | $response->headers->setCookie($cookie); |
||
| 227 | } |
||
| 228 | |||
| 229 | return $response; |
||
| 230 | } |
||
| 439 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.