/** * @param Comment $comment * @return bool */ private function userHasAccess(Comment $comment) { if ($comment->getUser() == $this->getUser() || $this->get('security.context')->isGranted('ROLE_ADMIN')) { return true; } return false; }
/** * @param Comment $comment * @return bool */ public function isCommentAuthor(Comment $comment) { $commentAuthor = $comment->getUser(); $user = $this->container->get('security.context')->getToken()->getUser(); $isGranted = $this->container->get('security.context')->isGranted('ROLE_ADMIN'); if ($commentAuthor == $user || $isGranted) { return true; } return false; }
/** * {@inheritDoc} */ public function load(ObjectManager $manager) { // Create comments foreach ($this->commentsData as $commentData) { $comment = new Comment(); $post = $manager->getRepository('AcmeBlogBundle:Post')->findOneBySlug($commentData['post']); $comment->setPost($post)->setUsername($commentData['username'])->setMail($commentData['mail'])->setContent($commentData['content'])->setCreated(new \DateTime($commentData['created'])); $manager->persist($comment); } $manager->flush(); }
/** * {@inheritDoc} */ public function load(ObjectManager $manager) { $faker = \Faker\Factory::create('pl_PL'); for ($a = 0; $a < self::SIZE; $a++) { $comment = new Comment(); $comment->setFirstName($faker->firstNameMale); $comment->setContent($faker->text); $comment->setPost($this->getReference(sprintf('comment-post-%d', rand(0, LoadCommentPostData::MAX_SIZE - 1)))); $manager->persist($comment); } $manager->flush(); }
/** * Finds and displays a post */ public function showAction($slug) { $em = $this->getDoctrine()->getManager(); $post = $em->getRepository('AcmeBlogBundle:Post')->findOneBySlug($slug); if (!$post) { throw $this->createNotFoundException('Unable to find this post.'); } $request = $this->getRequest(); // Create form $comment = new Comment(); $comment->setPost($post); $form = $this->createForm(new CommentType(), $comment, array('method' => 'POST')); // If it's a submit, valid and save the comment if ('POST' == $request->getMethod()) { $form->handleRequest($request); if ($form->isValid()) { $em->persist($comment); $em->flush(); return $this->redirect($request->headers->get('referer') . '#comments'); } } return $this->render('AcmeBlogBundle:Public:show.html.twig', array('post' => $post, 'form' => $form->createView())); }