@@ -123,95 +123,6 @@ Using ``@Security``, this looks like:
123123 // ...
124124 }
125125
126- <<<<<<< HEAD
127- Using Expressions for Complex Security Restrictions
128- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129-
130- If your security logic is a little bit more complex, you can use an `expression `_
131- inside ``@Security ``. In the following example, a user can only access the
132- controller if their email matches the value returned by the ``getAuthorEmail ``
133- method on the ``Post `` object:
134-
135- .. code-block :: php
136-
137- use AppBundle\Entity\Post;
138- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
139- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
140-
141- /**
142- * @Route("/{id}/edit", name="admin_post_edit")
143- * @Security("user.getEmail() == post.getAuthorEmail()")
144- */
145- public function editAction(Post $post)
146- {
147- // ...
148- }
149-
150- Notice that this requires the use of the `ParamConverter `_, which automatically
151- queries for the ``Post `` object and puts it on the ``$post `` argument. This
152- is what makes it possible to use the ``post `` variable in the expression.
153-
154- This has one major drawback: an expression in an annotation cannot easily
155- be reused in other parts of the application. Imagine that you want to add
156- a link in a template that will only be seen by authors. Right now you'll
157- need to repeat the expression code using Twig syntax:
158-
159- .. code-block :: html+jinja
160-
161- {% if app.user and app.user.email == post.authorEmail %}
162- <a href=""> ... </a>
163- {% endif %}
164-
165- The easiest solution - if your logic is simple enough - is to add a new method
166- to the ``Post `` entity that checks if a given user is its author:
167-
168- .. code-block :: php
169-
170- // src/AppBundle/Entity/Post.php
171- // ...
172-
173- class Post
174- {
175- // ...
176-
177- /**
178- * Is the given User the author of this Post?
179- *
180- * @return bool
181- */
182- public function isAuthor(User $user = null)
183- {
184- return $user && $user->getEmail() == $this->getAuthorEmail();
185- }
186- }
187-
188- Now you can reuse this method both in the template and in the security expression:
189-
190- .. code-block :: php
191-
192- use AppBundle\Entity\Post;
193- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
194-
195- /**
196- * @Route("/{id}/edit", name="admin_post_edit")
197- * @Security("post.isAuthor(user)")
198- */
199- public function editAction(Post $post)
200- {
201- // ...
202- }
203-
204- .. code-block :: html+jinja
205-
206- {% if post.isAuthor(app.user) %}
207- <a href=""> ... </a>
208- {% endif %}
209-
210- .. _best-practices-directly-isGranted :
211- =======
212- .. _best-practices-directy-isGranted :
213- >>>>>>> pull/4548
214-
215126 Checking Permissions without @Security
216127--------------------------------------
217128
0 commit comments