/** * Tries to authenticate the provided token * * @param TokenInterface $token token to authenticate * @param UserProviderInterface $userProvider provider to auth against * @param string $providerKey key to auth with * * @return \Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken */ public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) { $username = $token->getCredentials(); $securityUser = false; // If no username in Strategy, check if required. if ($this->securityRequired && !$username) { $this->logger->warning('Authentication key is required.'); throw new AuthenticationException(sprintf('Authentication key is required.')); } /** @var SecurityUser $securityUser */ if ($user = $this->userProvider->loadUserByUsername($username)) { $securityUser = new SecurityUser($user, [SecurityUser::ROLE_USER]); } elseif ($this->securityTestUsername) { $this->logger->info('Authentication, loading test user: '******'Authentication, loading anonymous user.'); $securityUser = new SecurityUser(new AnonymousUser(), [SecurityUser::ROLE_ANONYMOUS]); } else { $this->logger->warning(sprintf('Authentication key "%s" could not be resolved.', $username)); throw new AuthenticationException(sprintf('Authentication key "%s" could not be resolved.', $username)); } } return new PreAuthenticatedToken($securityUser, $username, $providerKey, $securityUser->getRoles()); }
/** * roles should always return an array * * @return void */ public function testGetRoles() { $entity = new SecurityUser($this->getUserMock()); $this->assertInternalType('array', $entity->getRoles()); }