@@ -438,7 +438,7 @@ Next, create the controller that will display the login form::
438
438
439
439
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
440
440
use Symfony\Component\HttpFoundation\Request;
441
- use Symfony\Component\Security\Core\SecurityContextInterface ;
441
+ use Symfony\Component\Security\Core\Security ;
442
442
443
443
class SecurityController extends Controller
444
444
{
@@ -447,19 +447,19 @@ Next, create the controller that will display the login form::
447
447
$session = $request->getSession();
448
448
449
449
// get the login error if there is one
450
- if ($request->attributes->has(SecurityContextInterface ::AUTHENTICATION_ERROR)) {
450
+ if ($request->attributes->has(Security ::AUTHENTICATION_ERROR)) {
451
451
$error = $request->attributes->get(
452
- SecurityContextInterface ::AUTHENTICATION_ERROR
452
+ Security ::AUTHENTICATION_ERROR
453
453
);
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);
457
457
} else {
458
458
$error = '';
459
459
}
460
460
461
461
// last username entered by the user
462
- $lastUsername = (null === $session) ? '' : $session->get(SecurityContextInterface ::LAST_USERNAME);
462
+ $lastUsername = (null === $session) ? '' : $session->get(Security ::LAST_USERNAME);
463
463
464
464
return $this->render(
465
465
'AcmeSecurityBundle:Security:login.html.twig',
@@ -713,7 +713,7 @@ see :doc:`/cookbook/security/form_login`.
713
713
``/login_check `` doesn't match any firewall, you'll receive a ``Unable
714
714
to find the controller for path "/login_check" `` exception.
715
715
716
- **4. Multiple firewalls don't share security context **
716
+ **4. Multiple firewalls don't share the same security context **
717
717
718
718
If you're using multiple firewalls and you authenticate against one firewall,
719
719
you will *not * be authenticated against any other firewalls automatically.
@@ -1174,7 +1174,7 @@ authorization from inside a controller::
1174
1174
1175
1175
public function helloAction($name)
1176
1176
{
1177
- if (false === $this->get('security.context ')->isGranted('ROLE_ADMIN')) {
1177
+ if (false === $this->get('security.authorization_checker ')->isGranted('ROLE_ADMIN')) {
1178
1178
throw $this->createAccessDeniedException('Unable to access this page!');
1179
1179
}
1180
1180
@@ -1186,6 +1186,10 @@ authorization from inside a controller::
1186
1186
.. versionadded :: 2.5
1187
1187
The ``createAccessDeniedException `` method was introduced in Symfony 2.5.
1188
1188
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
+
1189
1193
The :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::createAccessDeniedException `
1190
1194
method creates a special :class: `Symfony\\ Component\\ Security\\ Core\\ Exception\\ AccessDeniedException `
1191
1195
object, which ultimately triggers a 403 HTTP response inside Symfony.
@@ -1618,14 +1622,18 @@ Retrieving the User Object
1618
1622
~~~~~~~~~~~~~~~~~~~~~~~~~~
1619
1623
1620
1624
After authentication, the ``User `` object of the current user can be accessed
1621
- via the ``security.context `` service. From inside a controller, this will
1625
+ via the ``security.token_storage `` service. From inside a controller, this will
1622
1626
look like::
1623
1627
1624
1628
public function indexAction()
1625
1629
{
1626
- $user = $this->get('security.context ')->getToken()->getUser();
1630
+ $user = $this->get('security.token_storage ')->getToken()->getUser();
1627
1631
}
1628
1632
1633
+ .. versionadded :: 2.6
1634
+ The ``security.token_storage `` service was introduced in Symfony 2.6. Prior
1635
+ to Symfony 2.6, you had to use the ``getToken() `` method of the ``security.context `` service.
1636
+
1629
1637
In a controller this can be shortcut to:
1630
1638
1631
1639
.. code-block :: php
@@ -1895,13 +1903,17 @@ authorization from inside a controller::
1895
1903
1896
1904
public function helloAction($name)
1897
1905
{
1898
- if (false === $this->get('security.context ')->isGranted('ROLE_ADMIN')) {
1906
+ if (false === $this->get('security.authorization_checker ')->isGranted('ROLE_ADMIN')) {
1899
1907
throw new AccessDeniedException();
1900
1908
}
1901
1909
1902
1910
// ...
1903
1911
}
1904
1912
1913
+ .. versionadded :: 2.6
1914
+ The ``security.authorization_checker `` service was introduced in Symfony 2.6. Prior
1915
+ to Symfony 2.6, you had to use the ``isGranted() `` method of the ``security.context `` service.
1916
+
1905
1917
.. caution ::
1906
1918
1907
1919
A firewall must be active or an exception will be thrown when the ``isGranted() ``
@@ -1925,7 +1937,7 @@ accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` object::
1925
1937
1926
1938
public function indexAction()
1927
1939
{
1928
- if (!$this->get('security.context ')->isGranted(new Expression(
1940
+ if (!$this->get('security.authorization_checker ')->isGranted(new Expression(
1929
1941
'"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
1930
1942
))) {
1931
1943
throw new AccessDeniedException();
@@ -1934,6 +1946,10 @@ accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` object::
1934
1946
// ...
1935
1947
}
1936
1948
1949
+ .. versionadded :: 2.6
1950
+ The ``security.authorization_checker `` service was introduced in Symfony 2.6. Prior
1951
+ to Symfony 2.6, you had to use the ``isGranted() `` method of the ``security.context `` service.
1952
+
1937
1953
In this example, if the current user has ``ROLE_ADMIN `` or if the current
1938
1954
user object's ``isSuperAdmin() `` method returns ``true ``, then access will
1939
1955
be granted (note: your User object may not have an ``isSuperAdmin `` method,
@@ -1979,10 +1995,10 @@ Additionally, you have access to a number of functions inside the expression:
1979
1995
use Symfony\Component\ExpressionLanguage\Expression;
1980
1996
// ...
1981
1997
1982
- $sc = $this->get('security.context ');
1983
- $access1 = $sc ->isGranted('IS_AUTHENTICATED_REMEMBERED');
1998
+ $authorizationChecker = $this->get('security.authorization_checker ');
1999
+ $access1 = $authorizationChecker ->isGranted('IS_AUTHENTICATED_REMEMBERED');
1984
2000
1985
- $access2 = $sc ->isGranted(new Expression(
2001
+ $access2 = $authorizationChecker ->isGranted(new Expression(
1986
2002
'is_remember_me() or is_fully_authenticated()'
1987
2003
));
1988
2004
0 commit comments