src/ApiV3Bundle/Security/Firewall/BasicAuthenticationListener.php line 11

Open in your IDE?
  1. <?php
  2. namespace ApiV3Bundle\Security\Firewall;
  3. use ApiV3Bundle\Exception\ErrorCode;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  6. class BasicAuthenticationListener extends BaseAuthenticationListener
  7. {
  8.     public function handle(Request $request)
  9.     {
  10.         if (null !== $this->getTokenStorage()->getToken()) {
  11.             return;
  12.         }
  13.         if (null === $username $request->headers->get('PHP_AUTH_USER')) {
  14.             return;
  15.         }
  16.         $user $this->validUser($username$request->headers->get('PHP_AUTH_PW'));
  17.         $token $this->createTokenFromRequest($request$user['id']);
  18.         $this->getTokenStorage()->setToken($token);
  19.     }
  20.     private function validUser($username$password)
  21.     {
  22.         $user $this->getUserService()->getUserByLoginField($username);
  23.         if (empty($user)) {
  24.             throw new UnauthorizedHttpException('Basic''用户帐号不存在'nullErrorCode::INVALID_CREDENTIAL);
  25.         }
  26.         if (!$this->getUserService()->verifyPassword($user['id'], $password)) {
  27.             throw new UnauthorizedHttpException('Basic''帐号密码不正确'nullErrorCode::INVALID_CREDENTIAL);
  28.         }
  29.         if ($user['locked']) {
  30.             throw new UnauthorizedHttpException('Basic''用户已锁定,请联系网校管理员'nullErrorCode::BANNED_CREDENTIAL);
  31.         }
  32.         return $user;
  33.     }
  34. }