Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c52274b

Browse files
committed
Merge branch '4.0' into 4.1
* 4.0: Add all serialized class properties Use version name in installer command Update tags declaration in yaml format Use the new Security helper in some code examples
2 parents c93a1b5 + 113f19b commit c52274b

File tree

8 files changed

+60
-66
lines changed

8 files changed

+60
-66
lines changed

components/serializer.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ use the Serializer service created before::
132132

133133
$jsonContent = $serializer->serialize($person, 'json');
134134

135-
// $jsonContent contains {"name":"foo","age":99,"sportsperson":false}
135+
// $jsonContent contains {"name":"foo","age":99,"sportsperson":false,"createdAt":null}
136136

137137
echo $jsonContent; // or return it in a Response
138138

controller/argument_value_resolver.rst

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,15 @@ retrieved from the token storage::
9191
use Symfony\Component\HttpFoundation\Request;
9292
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
9393
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
94-
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
95-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
94+
use Symfony\Component\Security\Core\Security;
9695

9796
class UserValueResolver implements ArgumentValueResolverInterface
9897
{
99-
private $tokenStorage;
98+
private $security;
10099

101-
public function __construct(TokenStorageInterface $tokenStorage)
100+
public function __construct(Security $security)
102101
{
103-
$this->tokenStorage = $tokenStorage;
102+
$this->security = $security;
104103
}
105104

106105
public function supports(Request $request, ArgumentMetadata $argument)
@@ -109,27 +108,20 @@ retrieved from the token storage::
109108
return false;
110109
}
111110

112-
$token = $this->tokenStorage->getToken();
113-
114-
if (!$token instanceof TokenInterface) {
115-
return false;
116-
}
117-
118-
return $token->getUser() instanceof User;
111+
return $this->security->getUser() instanceof User;
119112
}
120113

121114
public function resolve(Request $request, ArgumentMetadata $argument)
122115
{
123-
yield $this->tokenStorage->getToken()->getUser();
116+
yield $this->security->getUser();
124117
}
125118
}
126119

127120
In order to get the actual ``User`` object in your argument, the given value
128121
must fulfill the following requirements:
129122

130123
* An argument must be type-hinted as ``User`` in your action method signature;
131-
* A security token must be present;
132-
* The value must be an instance of the ``User``.
124+
* The value must be an instance of the ``User`` class.
133125

134126
When all those requirements are met and ``true`` is returned, the
135127
``ArgumentResolver`` calls ``resolve()`` with the same values as it called

form/dynamic_form_modification.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ contains only this user's friends. This can be done injecting the ``Security``
234234
service into the form type so you can get the current user object::
235235

236236
use Symfony\Component\Security\Core\Security;
237+
// ...
237238

238239
private $security;
239240

@@ -254,7 +255,8 @@ service into the form type so you can get the current user object::
254255
Customizing the Form Type
255256
~~~~~~~~~~~~~~~~~~~~~~~~~
256257

257-
Now that you have all the basics in place you can complete the listener logic::
258+
Now that you have all the basics in place you can use the features of the
259+
security helper to fill in the listener logic::
258260

259261
// src/Form/Type/FriendMessageFormType.php
260262

security.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,16 +1069,18 @@ the User object, and use the ``isGranted()`` method (or
10691069

10701070
An alternative way to get the current user in a controller is to type-hint
10711071
the controller argument with
1072-
:class:`Symfony\\Component\\Security\\Core\\User\\UserInterface`
1073-
(and default it to ``null`` if being logged-in is optional)::
1072+
:class:`Symfony\\Component\\Security\\Core\\Security`::
10741073

1075-
use Symfony\Component\Security\Core\User\UserInterface;
1074+
use Symfony\Component\Security\Core\Security;
10761075

1077-
public function index(UserInterface $user = null)
1076+
public function indexAction(Security $security)
10781077
{
1079-
// $user is null when not logged-in or anon.
1078+
$user = $security->getUser();
10801079
}
10811080

1081+
.. versionadded:: 3.4
1082+
The ``Security`` utility class was introduced in Symfony 3.4.
1083+
10821084
This is only recommended for experienced developers who don't extend from the
10831085
:ref:`Symfony base controller <the-base-controller-class-services>` and
10841086
don't use the :class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait`

security/impersonating_user.rst

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,28 @@ over the user's roles until you find one that a ``SwitchUserRole`` object::
108108
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
109109
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
110110
use Symfony\Component\Security\Core\Role\SwitchUserRole;
111+
use Symfony\Component\Security\Core\Security;
112+
// ...
111113

112-
private $authorizationChecker;
113-
private $tokenStorage;
114-
115-
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage)
114+
public class SomeService
116115
{
117-
$this->authorizationChecker = $authorizationChecker;
118-
$this->tokenStorage = $tokenStorage;
119-
}
116+
private $security;
120117

121-
public function someMethod()
122-
{
123-
// ...
118+
public function __construct(Security $security)
119+
{
120+
$this->security = $security;
121+
}
122+
123+
public function someMethod()
124+
{
125+
// ...
124126

125-
if ($authorizationChecker->isGranted('ROLE_PREVIOUS_ADMIN')) {
126-
foreach ($tokenStorage->getToken()->getRoles() as $role) {
127-
if ($role instanceof SwitchUserRole) {
128-
$impersonatorUser = $role->getSource()->getUser();
129-
break;
127+
if ($this->security->isGranted('ROLE_PREVIOUS_ADMIN')) {
128+
foreach ($this->security->getToken()->getRoles() as $role) {
129+
if ($role instanceof SwitchUserRole) {
130+
$impersonatorUser = $role->getSource()->getUser();
131+
break;
132+
}
130133
}
131134
}
132135
}

security/securing_services.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,27 @@ Before you add security, the class looks something like this::
4444
}
4545

4646
Your goal is to check the user's role when the ``sendNewsletter()`` method is
47-
called. The first step towards this is to inject the
48-
``Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface``
49-
service into the object::
47+
called. The first step towards this is to inject the ``security.helper`` service
48+
using the :class:`Symfony\\Component\\Security\\Core\\Security` class::
5049

5150
// src/Newsletter/NewsletterManager.php
5251

5352
// ...
54-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
5553
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
54+
use Symfony\Component\Security\Core\Security;
5655

5756
class NewsletterManager
5857
{
59-
protected $authChecker;
58+
protected $security;
6059

61-
public function __construct(AuthorizationCheckerInterface $authChecker)
60+
public function __construct(Security $security)
6261
{
63-
$this->authChecker = $authChecker;
62+
$this->security = $security;
6463
}
6564

6665
public function sendNewsletter()
6766
{
68-
if (!$this->authChecker->isGranted('ROLE_NEWSLETTER_ADMIN')) {
67+
if (!$this->security->isGranted('ROLE_NEWSLETTER_ADMIN')) {
6968
throw new AccessDeniedException();
7069
}
7170

@@ -76,8 +75,8 @@ service into the object::
7675
}
7776

7877
If you're using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
79-
Symfony will automatically pass the authorization checker to your service
80-
thanks to autowiring and the ``AuthorizationCheckerInterface`` type-hint.
78+
Symfony will automatically pass the ``security.helper`` to your service
79+
thanks to autowiring and the ``Security`` type-hint.
8180

8281
If the current user does not have the ``ROLE_NEWSLETTER_ADMIN``, they will
8382
be prompted to log in.

service_container/tags.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ example:
1717
services:
1818
App\Twig\AppExtension:
1919
public: false
20-
tags: [twig.extension]
20+
tags: ['twig.extension']
2121
2222
.. code-block:: xml
2323
@@ -162,10 +162,10 @@ For example, you may add the following transports as services:
162162
services:
163163
Swift_SmtpTransport:
164164
arguments: ['%mailer_host%']
165-
tags: [app.mail_transport]
165+
tags: ['app.mail_transport']
166166
167167
Swift_SendmailTransport:
168-
tags: [app.mail_transport]
168+
tags: ['app.mail_transport']
169169
170170
.. code-block:: xml
171171
@@ -317,11 +317,11 @@ To answer this, change the service declaration:
317317
Swift_SmtpTransport:
318318
arguments: ['%mailer_host%']
319319
tags:
320-
- { name: app.mail_transport, alias: smtp }
320+
- { name: 'app.mail_transport', alias: 'smtp' }
321321
322322
Swift_SendmailTransport:
323323
tags:
324-
- { name: app.mail_transport, alias: sendmail }
324+
- { name: 'app.mail_transport', alias: 'sendmail' }
325325
326326
.. code-block:: xml
327327
@@ -368,13 +368,13 @@ To answer this, change the service declaration:
368368
# Compact syntax
369369
Swift_SendmailTransport:
370370
class: \Swift_SendmailTransport
371-
tags: [app.mail_transport]
371+
tags: ['app.mail_transport']
372372
373373
# Verbose syntax
374374
Swift_SendmailTransport:
375375
class: \Swift_SendmailTransport
376376
tags:
377-
- { name: app.mail_transport }
377+
- { name: 'app.mail_transport' }
378378
379379
Notice that you've added a generic ``alias`` key to the tag. To actually
380380
use this, update the compiler::
@@ -424,10 +424,10 @@ first constructor argument to the ``App\HandlerCollection`` service:
424424
# config/services.yaml
425425
services:
426426
App\Handler\One:
427-
tags: [app.handler]
427+
tags: ['app.handler']
428428
429429
App\Handler\Two:
430-
tags: [app.handler]
430+
tags: ['app.handler']
431431
432432
App\HandlerCollection:
433433
# inject all services tagged with app.handler as first argument
@@ -500,7 +500,7 @@ application handlers.
500500
services:
501501
App\Handler\One:
502502
tags:
503-
- { name: app.handler, priority: 20 }
503+
- { name: 'app.handler', priority: 20 }
504504
505505
.. code-block:: xml
506506

session/proxy_examples.rst

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ can intercept the session before it is written::
110110

111111
use App\Entity\User;
112112
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
113-
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
113+
use Symfony\Component\Security\Core\Security;
114114

115115
class ReadOnlySessionProxy extends SessionHandlerProxy
116116
{
117-
private $tokenStorage;
117+
private $security;
118118

119-
public function __construct(\SessionHandlerInterface $handler, TokenStorageInterface $tokenStorage)
119+
public function __construct(\SessionHandlerInterface $handler, Security $security)
120120
{
121-
$this->tokenStorage = $tokenStorage;
121+
$this->security = $security;
122122

123123
parent::__construct($handler);
124124
}
@@ -134,11 +134,7 @@ can intercept the session before it is written::
134134

135135
private function getUser()
136136
{
137-
if (!$token = $this->tokenStorage->getToken()) {
138-
return;
139-
}
140-
141-
$user = $token->getUser();
137+
$user = $this->security->getUser();
142138
if (is_object($user)) {
143139
return $user;
144140
}

0 commit comments

Comments
 (0)