/** * {@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); }
/** * Updates the user last action * * @param PostResponseEvent $event */ public function onRequestTermination(PostResponseEvent $event) { $this->userFetcher->setThrowOnTokenError(false); if (null === ($user = $this->userFetcher->resolve())) { return; } $requestTime = $event->getRequest()->server->get('REQUEST_TIME'); $dateTime = new \DateTime(); $dateTime->setTimestamp($requestTime); $user->updateLastAction($dateTime); $this->userRepository->modify($user); }
/** * Checks if after a reload if the locale has changed. * If the user is logged in, the route is the default application route and the locale has changed, * the user locale will be modified * * @param GetResponseEvent $event */ public function switchLocaleOnRequest(GetResponseEvent $event) { // no user is set in the access token // which means that no system user is authenticated and // that trigger is irrelevant if (null === ($user = $this->userFetcher->resolve())) { return; } $userLocale = $user->getSimpleProfile()->getLocale(); if ($userLocale === ($cookie = $event->getRequest()->cookies->get('locale'))) { return; } $validLocale = true; try { $user->changeUserLocale($cookie); } catch (ChangeUserLocaleException $ex) { $validLocale = false; $request = $event->getRequest(); $request->cookies->remove('locale'); $request->setLocale($userLocale); $request->attributes->set('_locale', $userLocale); } // if the locale is invalid, // the cookie will be fixed in the response event if (!$validLocale) { $this->fixCookie = true; return; } $this->userRepository->modify($user); }
/** * {@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; }
/** * Activates a user * * @param User $user * @param ActivateUserDTO $keyCode */ public function activateUser(User $user, ActivateUserDTO $keyCode) { $user->activate($keyCode->getCode()); $this->userRepository->modify($user); }
/** * Resets the password of a user * * @param User $user */ public function resetPassword(User $user) { $user->resetPassword($this->keyGenerator); $this->userRepository->modify($user); }
/** * Removes the user token * * @param User $user */ public function purgeToken(User $user) { $user->deleteToken(); $this->userRepository->modify($user); }
/** * {@inheritdoc} */ public function loadUserByUsername($username) { return $this->toSecurityUser($this->userRepository->findOneByName($username)); }