/** * Assigns the currently logged in user to a Comment. * * @param \FOS\CommentBundle\Event\CommentEvent $event * @return void */ public function blame(CommentEvent $event) { $comment = $event->getComment(); if (null === $this->securityContext) { if ($this->logger) { $this->logger->debug("Comment Blamer did not receive the security.context service."); } return; } if (!$comment instanceof SignedCommentInterface) { if ($this->logger) { $this->logger->debug("Comment does not implement SignedCommentInterface, skipping"); } return; } if (null === $this->securityContext->getToken()) { if ($this->logger) { $this->logger->debug("There is no firewall configured. We cant get a user."); } return; } if (null === $comment->getAuthor() && $this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $comment->setAuthor($this->securityContext->getToken()->getUser()); } }
/** * @return User */ public function getUser() { if (null === ($token = $this->securityContext->getToken())) { return null; } if (!is_object($user = $token->getUser())) { return null; } return $user; }
/** * Assigns the currently logged in user to a Comment. * * @throws InvalidArgumentException when the Comment is not a SignedCommentInterface * @param CommentInterface $comment * @return void */ public function blame(CommentInterface $comment) { if (!$comment instanceof SignedCommentInterface) { throw new InvalidArgumentException('The comment must implement SignedCommentInterface'); } if (null === $this->securityContext->getToken()) { throw new RuntimeException('You must configure a firewall for this route'); } if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) { $comment->setAuthor($this->securityContext->getToken()->getUser()); } }
/** * Method actually invoked upon dispatching an event "kernel.response" * * @param FilterResponseEvent $event * * @return void */ public function onKernelRequest(GetResponseEvent $event) { if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { return; } $token = $this->securityContext->getToken(); if ($token instanceof UsernamePasswordToken and $token->isAuthenticated()) { $user = $token->getUser(); $user->setLastActive(new \DateTime(date('Y-m-d H:i:s', time()))); $this->entityManager->persist($user); $this->entityManager->flush(); } }
public function hasRouteAccess($routeName) { $token = $this->securityContext->getToken(); if ($token->isAuthenticated()) { $route = $this->router->getRouteCollection()->get($routeName); $controller = $route->getDefault('_controller'); list($class, $method) = explode('::', $controller, 2); $metadata = $this->getMetadata($class); if (!isset($metadata->methodMetadata[$method])) { return false; } foreach ($metadata->methodMetadata[$method]->roles as $role) { if ($this->securityContext->isGranted($role)) { return true; } } } return false; }