@@ -438,7 +438,7 @@ Next, create the controller that will display the login form::
438438
439439 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
440440 use Symfony\Component\HttpFoundation\Request;
441- use Symfony\Component\Security\Core\SecurityContextInterface ;
441+ use Symfony\Component\Security\Core\Security ;
442442
443443 class SecurityController extends Controller
444444 {
@@ -447,19 +447,19 @@ Next, create the controller that will display the login form::
447447 $session = $request->getSession();
448448
449449 // get the login error if there is one
450- if ($request->attributes->has(SecurityContextInterface ::AUTHENTICATION_ERROR)) {
450+ if ($request->attributes->has(Security ::AUTHENTICATION_ERROR)) {
451451 $error = $request->attributes->get(
452- SecurityContextInterface ::AUTHENTICATION_ERROR
452+ Security ::AUTHENTICATION_ERROR
453453 );
454- } elseif (null !== $session && $session->has(SecurityContextInterface ::AUTHENTICATION_ERROR)) {
455- $error = $session->get(SecurityContextInterface ::AUTHENTICATION_ERROR);
456- $session->remove(SecurityContextInterface ::AUTHENTICATION_ERROR);
454+ } elseif (null !== $session && $session->has(Security ::AUTHENTICATION_ERROR)) {
455+ $error = $session->get(Security ::AUTHENTICATION_ERROR);
456+ $session->remove(Security ::AUTHENTICATION_ERROR);
457457 } else {
458458 $error = '';
459459 }
460460
461461 // last username entered by the user
462- $lastUsername = (null === $session) ? '' : $session->get(SecurityContextInterface ::LAST_USERNAME);
462+ $lastUsername = (null === $session) ? '' : $session->get(Security ::LAST_USERNAME);
463463
464464 return $this->render(
465465 'AcmeSecurityBundle:Security:login.html.twig',
@@ -713,7 +713,7 @@ see :doc:`/cookbook/security/form_login`.
713713 ``/login_check `` doesn't match any firewall, you'll receive a ``Unable
714714 to find the controller for path "/login_check" `` exception.
715715
716- **4. Multiple firewalls don't share security context **
716+ **4. Multiple firewalls don't share the same context **
717717
718718 If you're using multiple firewalls and you authenticate against one firewall,
719719 you will *not * be authenticated against any other firewalls automatically.
@@ -1174,7 +1174,7 @@ authorization from inside a controller::
11741174
11751175 public function helloAction($name)
11761176 {
1177- if (false === $this->get('security.context ')->isGranted('ROLE_ADMIN')) {
1177+ if (false === $this->get('security.authorization_checker ')->isGranted('ROLE_ADMIN')) {
11781178 throw $this->createAccessDeniedException('Unable to access this page!');
11791179 }
11801180
@@ -1186,6 +1186,10 @@ authorization from inside a controller::
11861186.. versionadded :: 2.5
11871187 The ``createAccessDeniedException `` method was introduced in Symfony 2.5.
11881188
1189+ .. versionadded :: 2.6
1190+ The ``security.authorization_checker `` service was introduced in Symfony 2.6. Prior
1191+ to Symfony 2.6, you had to use the ``isGranted() `` method of the ``security.context `` service.
1192+
11891193The :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::createAccessDeniedException `
11901194method creates a special :class: `Symfony\\ Component\\ Security\\ Core\\ Exception\\ AccessDeniedException `
11911195object, which ultimately triggers a 403 HTTP response inside Symfony.
@@ -1621,14 +1625,18 @@ Retrieving the User Object
16211625~~~~~~~~~~~~~~~~~~~~~~~~~~
16221626
16231627After authentication, the ``User `` object of the current user can be accessed
1624- via the ``security.context `` service. From inside a controller, this will
1628+ via the ``security.token_storage `` service. From inside a controller, this will
16251629look like::
16261630
16271631 public function indexAction()
16281632 {
1629- $user = $this->get('security.context ')->getToken()->getUser();
1633+ $user = $this->get('security.token_storage ')->getToken()->getUser();
16301634 }
16311635
1636+ .. versionadded :: 2.6
1637+ The ``security.token_storage `` service was introduced in Symfony 2.6. Prior
1638+ to Symfony 2.6, you had to use the ``getToken() `` method of the ``security.context `` service.
1639+
16321640In a controller this can be shortcut to:
16331641
16341642.. code-block :: php
@@ -1898,13 +1906,17 @@ authorization from inside a controller::
18981906
18991907 public function helloAction($name)
19001908 {
1901- if (false === $this->get('security.context ')->isGranted('ROLE_ADMIN')) {
1909+ if (false === $this->get('security.authorization_checker ')->isGranted('ROLE_ADMIN')) {
19021910 throw new AccessDeniedException();
19031911 }
19041912
19051913 // ...
19061914 }
19071915
1916+ .. versionadded :: 2.6
1917+ The ``security.authorization_checker `` service was introduced in Symfony 2.6. Prior
1918+ to Symfony 2.6, you had to use the ``isGranted() `` method of the ``security.context `` service.
1919+
19081920.. caution ::
19091921
19101922 A firewall must be active or an exception will be thrown when the ``isGranted() ``
@@ -1928,7 +1940,7 @@ accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` object::
19281940
19291941 public function indexAction()
19301942 {
1931- if (!$this->get('security.context ')->isGranted(new Expression(
1943+ if (!$this->get('security.authorization_checker ')->isGranted(new Expression(
19321944 '"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
19331945 ))) {
19341946 throw new AccessDeniedException();
@@ -1937,6 +1949,10 @@ accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` object::
19371949 // ...
19381950 }
19391951
1952+ .. versionadded :: 2.6
1953+ The ``security.authorization_checker `` service was introduced in Symfony 2.6. Prior
1954+ to Symfony 2.6, you had to use the ``isGranted() `` method of the ``security.context `` service.
1955+
19401956In this example, if the current user has ``ROLE_ADMIN `` or if the current
19411957user object's ``isSuperAdmin() `` method returns ``true ``, then access will
19421958be granted (note: your User object may not have an ``isSuperAdmin `` method,
@@ -1982,10 +1998,10 @@ Additionally, you have access to a number of functions inside the expression:
19821998 use Symfony\Component\ExpressionLanguage\Expression;
19831999 // ...
19842000
1985- $sc = $this->get('security.context ');
1986- $access1 = $sc ->isGranted('IS_AUTHENTICATED_REMEMBERED');
2001+ $authorizationChecker = $this->get('security.authorization_checker ');
2002+ $access1 = $authorizationChecker ->isGranted('IS_AUTHENTICATED_REMEMBERED');
19872003
1988- $access2 = $sc ->isGranted(new Expression(
2004+ $access2 = $authorizationChecker ->isGranted(new Expression(
19892005 'is_remember_me() or is_fully_authenticated()'
19902006 ));
19912007
0 commit comments