5
5
How to Secure any Service or Method in your Application
6
6
=======================================================
7
7
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::
11
12
12
13
// ...
13
14
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@@ -19,21 +20,19 @@ and checking the current user's role::
19
20
// ...
20
21
}
21
22
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 ``
23
24
service into it. For a general introduction to injecting dependencies into
24
25
services see the :doc: `/book/service_container ` chapter of the book. For
25
26
example, 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::
30
30
31
31
// src/AppBundle/Newsletter/NewsletterManager.php
32
32
namespace AppBundle\Newsletter;
33
33
34
34
class NewsletterManager
35
35
{
36
-
37
36
public function sendNewsletter()
38
37
{
39
38
// ... where you actually do the work
@@ -49,8 +48,9 @@ check, this is an ideal candidate for constructor injection, which guarantees
49
48
that the authorization checker object will be available inside the ``NewsletterManager ``
50
49
class::
51
50
52
- namespace AppBundle\ Newsletter;
51
+ // src/ AppBundle/ Newsletter/NewsletterManager.php
53
52
53
+ // ...
54
54
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
55
55
56
56
class NewsletterManager
0 commit comments