@@ -101,13 +101,16 @@ public function newAction(Request $request)
101101 *
102102 * @Route("/{id}", requirements={"id" = "\d+"}, name="admin_post_show")
103103 * @Method("GET")
104- * @Security("post.isAuthor(user)")
105- *
106- * NOTE: You can also centralize security logic by using a "voter"
107- * See http://symfony.com/doc/current/cookbook/security/voters_data_permission.html
108104 */
109105 public function showAction (Post $ post )
110106 {
107+ // This security check can also be performed:
108+ // 1. Using an annotation: @Security("post.isAuthor(user)")
109+ // 2. Using a "voter" (see http://symfony.com/doc/current/cookbook/security/voters_data_permission.html)
110+ if (null === $ this ->getUser () || !$ post ->isAuthor ($ this ->getUser ())) {
111+ throw $ this ->createAccessDeniedException ('Posts can only be shown to their authors. ' );
112+ }
113+
111114 $ deleteForm = $ this ->createDeleteForm ($ post );
112115
113116 return $ this ->render ('admin/blog/show.html.twig ' , array (
@@ -121,10 +124,13 @@ public function showAction(Post $post)
121124 *
122125 * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="admin_post_edit")
123126 * @Method({"GET", "POST"})
124- * @Security("post.isAuthor(user)")
125127 */
126128 public function editAction (Post $ post , Request $ request )
127129 {
130+ if (null === $ this ->getUser () || !$ post ->isAuthor ($ this ->getUser ())) {
131+ throw $ this ->createAccessDeniedException ('Posts can only be edited by their authors. ' );
132+ }
133+
128134 $ em = $ this ->getDoctrine ()->getManager ();
129135
130136 $ editForm = $ this ->createForm (new PostType (), $ post );
0 commit comments