/** * {@inheritdoc} */ public function analyse($content) { $users = []; preg_match_all('/@[A-zäöüÄÖÜß0-9_\\-\\.]+/i', $content, $matches); foreach ($matches[0] as $match) { $username = substr($match, 1); $user = $this->userRepository->findOneByName($username); if (!$user) { continue; } $users[] = $user; } // use symfony2 validation regex since this regex is tested and fully workable // but the sings ^ and $ need to be removed in order to show that the url needn't be the start or end of // the test $regex = sprintf(UrlValidator::PATTERN, 'http|https|ftp'); $length = strlen($regex); $regex = substr_replace($regex, null, $length - ($length - 1), 1); $regex = substr_replace($regex, null, $length - 6, 1); preg_match_all($regex, $content, $matches); $links = array_map(function ($link) { return new Link($link); }, $matches[0]); preg_match_all('/(#\\w+)/', $content, $matches)[0]; $tags = array_map(function ($tag) { return new Hashtag(substr($tag, 1)); }, $matches[0]); return new PostMetadata($users, $links, $tags); }
/** * Authenticates a user by its credentials * * @param AuthDTO $authDTO * * @return \Sententiaregum\User\Domain\Value\ApiKey * * @throws AuthenticationException If the username is invalid */ public function createToken(AuthDTO $authDTO) { $user = $this->userRepository->findOneByName($authDTO->getUsername()); if (!$user) { throw AuthenticationException::fromInvalidUsername(); } try { $token = $user->authenticateToken($authDTO, $this->apiKeyGenerator); } catch (AuthenticationException $ex) { // modify authentication failure $this->userRepository->modify($user); throw $ex; } $this->userRepository->modify($user); return $token; }
/** * {@inheritdoc} */ public function resolve(TokenInterface $token = null) { $token = $token ?: $this->tokenStorage->getToken(); if (!$token && $this->throwOnTokenError) { throw new \LogicException('The token is empty and contains no username!'); } elseif (!$token && !$this->throwOnTokenError) { return null; } $user = $token->getUser(); if (!$user instanceof UserInterface) { if ($this->throwOnTokenError) { throw new \LogicException(sprintf('The user in the security token must be an instance of "%s"!', UserInterface::class)); } return null; } if (null === ($resolvedUser = $this->repository->findOneByName($user->getUsername()))) { throw new \LogicException(sprintf('No user with username "%s" found!', $user->getUsername())); } return $resolvedUser; }
/** * {@inheritdoc} */ public function loadUserByUsername($username) { return $this->toSecurityUser($this->userRepository->findOneByName($username)); }