Skip to content

Commit 1f52ad4

Browse files
committed
Fixed code examples in cookbook/security
1 parent 35e43cd commit 1f52ad4

File tree

8 files changed

+77
-73
lines changed

8 files changed

+77
-73
lines changed

cookbook/security/acl.rst

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,38 +85,44 @@ Creating an ACL, and adding an ACE
8585

8686
.. code-block:: php
8787
88+
// src/Acme/DemoBundle/Controller/BlogController.php
89+
namespace Acme\DemoBundle\Controller;
90+
91+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8892
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
8993
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
9094
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
9195
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
92-
// ...
93-
94-
// BlogController.php
95-
public function addCommentAction(Post $post)
96-
{
97-
$comment = new Comment();
9896
99-
// setup $form, and bind data
97+
class BlogController
98+
{
10099
// ...
101100
102-
if ($form->isValid()) {
103-
$entityManager = $this->get('doctrine.orm.default_entity_manager');
104-
$entityManager->persist($comment);
105-
$entityManager->flush();
101+
public function addCommentAction(Post $post)
102+
{
103+
$comment = new Comment();
106104
107-
// creating the ACL
108-
$aclProvider = $this->get('security.acl.provider');
109-
$objectIdentity = ObjectIdentity::fromDomainObject($comment);
110-
$acl = $aclProvider->createAcl($objectIdentity);
105+
// ... setup $form, and bind data
111106
112-
// retrieving the security identity of the currently logged-in user
113-
$securityContext = $this->get('security.context');
114-
$user = $securityContext->getToken()->getUser();
115-
$securityIdentity = UserSecurityIdentity::fromAccount($user);
107+
if ($form->isValid()) {
108+
$entityManager = $this->get('doctrine.orm.default_entity_manager');
109+
$entityManager->persist($comment);
110+
$entityManager->flush();
111+
112+
// creating the ACL
113+
$aclProvider = $this->get('security.acl.provider');
114+
$objectIdentity = ObjectIdentity::fromDomainObject($comment);
115+
$acl = $aclProvider->createAcl($objectIdentity);
116116
117-
// grant owner access
118-
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
119-
$aclProvider->updateAcl($acl);
117+
// retrieving the security identity of the currently logged-in user
118+
$securityContext = $this->get('security.context');
119+
$user = $securityContext->getToken()->getUser();
120+
$securityIdentity = UserSecurityIdentity::fromAccount($user);
121+
122+
// grant owner access
123+
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
124+
$aclProvider->updateAcl($acl);
125+
}
120126
}
121127
}
122128
@@ -147,19 +153,26 @@ Checking Access
147153

148154
.. code-block:: php
149155
150-
// BlogController.php
151-
public function editCommentAction(Comment $comment)
156+
// src/Acme/DemoBundle/Controller/BlogController.php
157+
158+
// ...
159+
160+
class BlogController
152161
{
153-
$securityContext = $this->get('security.context');
162+
// ...
154163
155-
// check for edit access
156-
if (false === $securityContext->isGranted('EDIT', $comment))
164+
public function editCommentAction(Comment $comment)
157165
{
158-
throw new AccessDeniedException();
159-
}
166+
$securityContext = $this->get('security.context');
160167
161-
// retrieve actual comment object, and do your editing here
162-
// ...
168+
// check for edit access
169+
if (false === $securityContext->isGranted('EDIT', $comment))
170+
{
171+
throw new AccessDeniedException();
172+
}
173+
174+
// ... retrieve actual comment object, and do your editing here
175+
}
163176
}
164177
165178
In this example, we check whether the user has the ``EDIT`` permission.

cookbook/security/custom_authentication_provider.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ create a class which implements
300300
}
301301
302302
public function addConfiguration(NodeDefinition $node)
303-
{}
303+
{
304+
}
304305
}
305306
306307
The :class:`Symfony\\Bundle\\SecurityBundle\\DependencyInjection\\Security\\Factory\\SecurityFactoryInterface`
@@ -500,15 +501,14 @@ the ``addConfiguration`` method.
500501
501502
class WsseFactory implements SecurityFactoryInterface
502503
{
503-
# ...
504+
// ...
504505
505506
public function addConfiguration(NodeDefinition $node)
506507
{
507508
$node
508509
->children()
509-
->scalarNode('lifetime')->defaultValue(300)
510-
->end()
511-
;
510+
->scalarNode('lifetime')->defaultValue(300)
511+
->end();
512512
}
513513
}
514514
@@ -528,10 +528,10 @@ in order to put it to use.
528528
->setDefinition($providerId,
529529
new DefinitionDecorator('wsse.security.authentication.provider'))
530530
->replaceArgument(0, new Reference($userProvider))
531-
->replaceArgument(2, $config['lifetime'])
532-
;
531+
->replaceArgument(2, $config['lifetime']);
533532
// ...
534533
}
534+
535535
// ...
536536
}
537537
@@ -559,4 +559,4 @@ The rest is up to you! Any relevant configuration items can be defined
559559
in the factory and consumed or passed to the other classes in the container.
560560

561561
.. _`WSSE`: http://www.xml.com/pub/a/2003/12/17/dive.html
562-
.. _`nonce`: http://en.wikipedia.org/wiki/Cryptographic_nonce
562+
.. _`nonce`: http://en.wikipedia.org/wiki/Cryptographic_nonce

cookbook/security/custom_provider.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ Here's an example of how this might look::
126126
public function loadUserByUsername($username)
127127
{
128128
// make a call to your webservice here
129-
// $userData = ...
129+
$userData = ...
130130
// pretend it returns an array on success, false if there is no user
131131

132132
if ($userData) {
133-
// $password = '...';
133+
$password = '...';
134+
134135
// ...
135136

136137
return new WebserviceUser($username, $password, $salt, $roles)
@@ -262,4 +263,4 @@ options, the password may be encoded multiple times and encoded to base64.
262263
encode_as_base64: false
263264
iterations: 1
264265
265-
.. _MessageDigestPasswordEncoder: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php
266+
.. _MessageDigestPasswordEncoder: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php

cookbook/security/entity_provider.rst

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ focus on the most important methods that come from the
4343
.. code-block:: php
4444
4545
// src/Acme/UserBundle/Entity/User.php
46-
4746
namespace Acme\UserBundle\Entity;
4847
4948
use Doctrine\ORM\Mapping as ORM;
@@ -200,7 +199,6 @@ then be checked against our User entity records in the database:
200199
.. code-block:: yaml
201200
202201
# app/config/security.yml
203-
204202
security:
205203
encoders:
206204
Acme\UserBundle\Entity\User:
@@ -267,16 +265,15 @@ For this example, the first three methods will return ``true`` whereas the
267265
.. code-block:: php
268266
269267
// src/Acme/UserBundle/Entity/User.php
270-
271268
namespace Acme\Bundle\UserBundle\Entity;
272269
273270
// ...
274271
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
275272
276-
// ...
277273
class User implements AdvancedUserInterface
278274
{
279275
// ...
276+
280277
public function isAccountNonExpired()
281278
{
282279
return true;
@@ -325,7 +322,6 @@ The code below shows the implementation of the
325322
``UserRepository`` class::
326323

327324
// src/Acme/UserBundle/Entity/UserRepository.php
328-
329325
namespace Acme\UserBundle\Entity;
330326

331327
use Symfony\Component\Security\Core\User\UserInterface;
@@ -421,12 +417,11 @@ more users. As a group is also a role, the previous ``getRoles()`` method now
421417
returns the list of related groups::
422418

423419
// src/Acme/UserBundle/Entity/User.php
424-
425420
namespace Acme\Bundle\UserBundle\Entity;
426421

427422
use Doctrine\Common\Collections\ArrayCollection;
428-
429423
// ...
424+
430425
class User implements AdvancedUserInterface
431426
{
432427
/**
@@ -455,6 +450,7 @@ important thing to notice is that the ``AcmeUserBundle:Group`` entity class
455450
implements the :class:`Symfony\\Component\\Security\\Core\\Role\\RoleInterface`
456451
that forces it to have a ``getRole()`` method::
457452

453+
// src/Acme/Bundle/UserBundle/Entity/Group.php
458454
namespace Acme\Bundle\UserBundle\Entity;
459455

460456
use Symfony\Component\Security\Core\Role\RoleInterface;
@@ -511,7 +507,6 @@ relationship in the ``UserRepository::loadUserByUsername()`` method. This will
511507
fetch the user and his associated roles / groups with a single query::
512508

513509
// src/Acme/UserBundle/Entity/UserRepository.php
514-
515510
namespace Acme\Bundle\UserBundle\Entity;
516511

517512
// ...
@@ -527,8 +522,7 @@ fetch the user and his associated roles / groups with a single query::
527522
->where('u.username = :username OR u.email = :email')
528523
->setParameter('username', $username)
529524
->setParameter('email', $username)
530-
->getQuery()
531-
;
525+
->getQuery();
532526

533527
// ...
534528
}

cookbook/security/form_login.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ the following config:
149149
$container->loadFromExtension('security', array(
150150
'firewalls' => array(
151151
'main' => array('form_login' => array(
152-
// ...
152+
...,
153153
'default_target_path' => '/admin',
154154
)),
155155
),
@@ -193,7 +193,7 @@ of what URL they had requested previously by setting the
193193
$container->loadFromExtension('security', array(
194194
'firewalls' => array(
195195
'main' => array('form_login' => array(
196-
// ...
196+
...,
197197
'always_use_default_target_path' => true,
198198
)),
199199
),
@@ -235,7 +235,7 @@ this by setting ``use_referer`` to true (it defaults to false):
235235
$container->loadFromExtension('security', array(
236236
'firewalls' => array(
237237
'main' => array('form_login' => array(
238-
// ...
238+
...,
239239
'use_referer' => true,
240240
)),
241241
),
@@ -271,7 +271,7 @@ redirect to the URL defined by some ``acount`` route, use the following:
271271

272272
.. code-block:: html+php
273273

274-
<?php // src/Acme/SecurityBundle/Resources/views/Security/login.html.php ?>
274+
<!-- src/Acme/SecurityBundle/Resources/views/Security/login.html.php -->
275275
<?php if ($error): ?>
276276
<div><?php echo $error->getMessage() ?></div>
277277
<?php endif; ?>
@@ -364,7 +364,7 @@ following config:
364364
$container->loadFromExtension('security', array(
365365
'firewalls' => array(
366366
'main' => array('form_login' => array(
367-
// ...
367+
...,
368368
'failure_path' => login_failure,
369369
)),
370370
),

cookbook/security/remember_me.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ are shown here:
1818
.. code-block:: yaml
1919
2020
# app/config/security.yml
21-
2221
firewalls:
2322
main:
2423
remember_me:
@@ -30,7 +29,6 @@ are shown here:
3029
.. code-block:: xml
3130
3231
<!-- app/config/security.xml -->
33-
3432
<config>
3533
<firewall>
3634
<remember-me
@@ -45,7 +43,6 @@ are shown here:
4543
.. code-block:: php
4644
4745
// app/config/security.php
48-
4946
$container->loadFromExtension('security', array(
5047
'firewalls' => array(
5148
'main' => array('remember_me' => array(
@@ -88,7 +85,7 @@ might ultimately look like this:
8885

8986
.. code-block:: html+php
9087

91-
<?php // src/Acme/SecurityBundle/Resources/views/Security/login.html.php ?>
88+
<!-- src/Acme/SecurityBundle/Resources/views/Security/login.html.php -->
9289
<?php if ($error): ?>
9390
<div><?php echo $error->getMessage() ?></div>
9491
<?php endif; ?>
@@ -158,14 +155,14 @@ In the following example, the action is only allowed if the user has the
158155

159156
.. code-block:: php
160157
161-
use Symfony\Component\Security\Core\Exception\AccessDeniedException
162158
// ...
159+
use Symfony\Component\Security\Core\Exception\AccessDeniedException
163160
164161
public function editAction()
165162
{
166163
if (false === $this->get('security.context')->isGranted(
167164
'IS_AUTHENTICATED_FULLY'
168-
)) {
165+
)) {
169166
throw new AccessDeniedException();
170167
}
171168

0 commit comments

Comments
 (0)