55How to Secure any Service or Method in your Application
66=======================================================
77
8- In the security chapter, you can see how to :ref: `secure a controller <book-security-securing-controller >`
9- by requesting the ``security.authorization_checker `` service from the Service Container
10- and checking the current user's role::
8+ In the security chapter, you can see how to
9+ :ref: `secure a controller <book-security-securing-controller >` by requesting
10+ the ``security.authorization_checker `` service from the Service Container and
11+ checking the current user's role::
1112
1213 // ...
1314 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@@ -19,21 +20,19 @@ and checking the current user's role::
1920 // ...
2021 }
2122
22- You can also secure *any * service in a similar way by injecting the ``security.authorization_checker ``
23+ You can also secure *any * service by injecting the ``security.authorization_checker ``
2324service into it. For a general introduction to injecting dependencies into
2425services see the :doc: `/book/service_container ` chapter of the book. For
2526example, suppose you have a ``NewsletterManager `` class that sends out emails
26- and you want to restrict its use to only users who have some ``ROLE_NEWSLETTER_ADMIN ``
27- role. Before you add security, the class looks something like this:
28-
29- .. code-block :: php
27+ and you want to restrict its use to only users who have some
28+ ``ROLE_NEWSLETTER_ADMIN `` role. Before you add security, the class looks
29+ something like this::
3030
3131 // src/AppBundle/Newsletter/NewsletterManager.php
3232 namespace AppBundle\Newsletter;
3333
3434 class NewsletterManager
3535 {
36-
3736 public function sendNewsletter()
3837 {
3938 // ... where you actually do the work
@@ -49,8 +48,9 @@ check, this is an ideal candidate for constructor injection, which guarantees
4948that the authorization checker object will be available inside the ``NewsletterManager ``
5049class::
5150
52- namespace AppBundle\ Newsletter;
51+ // src/ AppBundle/ Newsletter/NewsletterManager.php
5352
53+ // ...
5454 use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
5555
5656 class NewsletterManager
0 commit comments