Skip to content

Commit b969c43

Browse files
committed
fixed markup for previous commit
1 parent 837d6c7 commit b969c43

File tree

4 files changed

+143
-124
lines changed

4 files changed

+143
-124
lines changed

guides/map.rst.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
* :doc:`Overview </guides/security/overview>` |
3636
* :doc:`/guides/security/users` |
3737
* :doc:`/guides/security/authentication` |
38-
* :doc:`/guides/security/authorization`
38+
* :doc:`/guides/security/authorization` |
39+
* :doc:`ACLs </guides/security/acl>` |
40+
* :doc:`Advanced ACLs </guides/security/acl_advanced>`
3941

4042
* **Cache**:
4143

guides/security/acl.rst

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,41 @@
44
Access Control Lists (ACLs)
55
===========================
66

7-
In complex applications, you will often face the problem that access decisions
8-
cannot only be based on the person (``Token``) who is requesting access, but
7+
In complex applications, you will often face the problem that access decisions
8+
cannot only be based on the person (``Token``) who is requesting access, but
99
also involve a domain object that access is being requested for. This is where
1010
the ACL system comes in.
1111

12-
Imagine you are designing a blog system where your users can comment on your
13-
posts. Now, you want a user to be able to edit his own comments, but not those
14-
of other users; besides, you yourself want to be able to edit all comments.
15-
In this scenario, ``Comment`` would be our domain object that you want to
16-
restrict access to. You could take several approaches to accomplish this using
12+
Imagine you are designing a blog system where your users can comment on your
13+
posts. Now, you want a user to be able to edit his own comments, but not those
14+
of other users; besides, you yourself want to be able to edit all comments. In
15+
this scenario, ``Comment`` would be our domain object that you want to
16+
restrict access to. You could take several approaches to accomplish this using
1717
Symfony2, two basic approaches are (non-exhaustive):
1818

19-
- *Enforce security in your business methods*: Basically, that means keeping
20-
a reference inside each ``Comment`` to all users who have access, and then
19+
- *Enforce security in your business methods*: Basically, that means keeping a
20+
reference inside each ``Comment`` to all users who have access, and then
2121
compare these users to the provided ``Token``.
22-
- *Enforce security with roles*: In this approach, you would add a role for
22+
- *Enforce security with roles*: In this approach, you would add a role for
2323
each ``Comment`` object, i.e. ``ROLE_COMMENT_1``, ``ROLE_COMMENT_2``, etc.
2424

25-
Both approaches are perfectly valid. However, they couple your authorization
26-
logic to your business code which makes it less reusable elsewhere, and also
27-
increases the difficulty of unit testing. Besides, you could run into
25+
Both approaches are perfectly valid. However, they couple your authorization
26+
logic to your business code which makes it less reusable elsewhere, and also
27+
increases the difficulty of unit testing. Besides, you could run into
2828
performance issues if many users would have access to a single domain object.
2929

3030
Fortunately, there is a better way, which we will talk about now.
3131

3232
Bootstrapping
3333
-------------
34-
Now, before we finally can get into action, we need to do some bootstrapping.
34+
35+
Now, before we finally can get into action, we need to do some bootstrapping.
3536
First, we need to configure the connection the ACL system is supposed to use:
3637

37-
.. configuration_block ::
38-
39-
.. code_block:: yaml
40-
38+
.. configuration-block::
39+
40+
.. code-block:: yaml
41+
4142
# app/config/security.yml
4243
security.acl:
4344
connection: default
@@ -54,18 +55,21 @@ First, we need to configure the connection the ACL system is supposed to use:
5455
// app/config/security.php
5556
$container->loadFromExtension('security', 'acl', array(
5657
'connection' => 'default',
57-
));
58+
));
5859
5960
6061
After the connection is configured. We have to import the database structure.
6162
Fortunately, we have a task for this. Simply run the following command:
6263

63-
``php app/console init:acl``
64+
.. code-block:: text
6465
66+
php app/console init:acl
6567
6668
Getting Started
6769
---------------
68-
Coming back to our small example from the beginning, let's implement ACL for it.
70+
71+
Coming back to our small example from the beginning, let's implement ACL for
72+
it.
6973

7074
Creating an ACL, and adding an ACE
7175
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -76,89 +80,91 @@ Creating an ACL, and adding an ACE
7680
public function addCommentAction(Post $post)
7781
{
7882
$comment = new Comment();
79-
83+
8084
// setup $form, and bind data
8185
// ...
82-
86+
8387
if ($form->isValid()) {
8488
$entityManager = $this->container->get('doctrine.orm.default_entity_manager');
8589
$entityManager->persist($comment);
8690
$entityManager->flush();
87-
91+
8892
// creating the ACL
8993
$aclProvider = $this->container->get('security.acl.provider');
9094
$objectIdentity = ObjectIdentity::fromDomainObject($comment);
9195
$acl = $aclProvider->createAcl($objectIdentity);
92-
96+
9397
// retrieving the security identity of the currently logged-in user
9498
$securityContext = $this->container->get('security.context');
9599
$user = $securityContext->getToken()->getUser();
96100
$securityIdentity = new UserSecurityIdentity($user);
97-
101+
98102
// grant owner access
99103
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
100104
$aclProvider->updateAcl($acl);
101105
}
102106
}
103107
104-
There are a couple of important implementation decisions in this code snippet. For now,
105-
I only want to highlight two:
108+
There are a couple of important implementation decisions in this code snippet.
109+
For now, I only want to highlight two:
106110

107-
First, you may have noticed that ``->createAcl()`` does not accept domain objects
108-
directly, but only implementations of the ``ObjectIdentityInterface``. This
109-
additional step of indirection allows you to work with ACLs even when you have
110-
no actual domain object instance at hand. This will be extremely helpful if you
111-
want to check permissions for a large number of objects without actually hydrating
112-
these objects.
111+
First, you may have noticed that ``->createAcl()`` does not accept domain
112+
objects directly, but only implementations of the ``ObjectIdentityInterface``.
113+
This additional step of indirection allows you to work with ACLs even when you
114+
have no actual domain object instance at hand. This will be extremely helpful
115+
if you want to check permissions for a large number of objects without
116+
actually hydrating these objects.
113117

114-
The other interesting part is the ``->insertObjectAce()`` call. In our example,
115-
we are granting the user who is currently logged in owner access to the Comment.
116-
The ``MaskBuilder::MASK_OWNER`` is a pre-defined integer bitmask; don't worry
117-
the mask builder will abstract away most of the technical details, but using
118-
this technique we can store many different permissions in one database row
119-
which gives us a considerable boost in performance.
118+
The other interesting part is the ``->insertObjectAce()`` call. In our
119+
example, we are granting the user who is currently logged in owner access to
120+
the Comment. The ``MaskBuilder::MASK_OWNER`` is a pre-defined integer bitmask;
121+
don't worry the mask builder will abstract away most of the technical details,
122+
but using this technique we can store many different permissions in one
123+
database row which gives us a considerable boost in performance.
120124

121125
.. tip::
122126

123-
The order in which ACEs are checked is significant. As a general rule, you
127+
The order in which ACEs are checked is significant. As a general rule, you
124128
should place more specific entries at the beginning.
125129

126130
Checking Access
127131
~~~~~~~~~~~~~~~
128132

129133
.. code-block:: php
130-
134+
131135
// BlogController.php
132136
public function editCommentAction(Comment $comment)
133137
{
134138
$securityContext = $this->container->get('security.context');
135-
139+
136140
// check for edit access
137141
if (false === $securityContext->vote('EDIT', $comment))
138142
{
139143
throw new AccessDeniedException();
140144
}
141-
145+
142146
// do your editing here
143147
}
144148
145-
In this example, we check whether the user has the ``EDIT`` permission. Internally,
146-
Symfony2 maps the permission to several integer bitmasks, and checks whether the
147-
user has any of them.
149+
In this example, we check whether the user has the ``EDIT`` permission.
150+
Internally, Symfony2 maps the permission to several integer bitmasks, and
151+
checks whether the user has any of them.
148152

149153
.. note::
150154

151-
You can define up to 32 base permissions (depending on your OS PHP might vary
152-
between 30 to 32). In addition, you can also define cumulative permissions.
155+
You can define up to 32 base permissions (depending on your OS PHP might
156+
vary between 30 to 32). In addition, you can also define cumulative
157+
permissions.
153158

154159
Cumulative Permissions
155160
----------------------
161+
156162
In our first example above, we only granted the user the ``OWNER`` base
157-
permission. While this effectively also allows the user to perform any operation
158-
such as view, edit, etc. on the domain object, there are cases where we want to
159-
grant these permissions explicitly.
163+
permission. While this effectively also allows the user to perform any
164+
operation such as view, edit, etc. on the domain object, there are cases where
165+
we want to grant these permissions explicitly.
160166

161-
The ``MaskBuilder`` can be used for creating bit masks easily by combining
167+
The ``MaskBuilder`` can be used for creating bit masks easily by combining
162168
several base permissions:
163169

164170
.. code-block:: php
@@ -171,8 +177,8 @@ several base permissions:
171177
->add('undelete')
172178
;
173179
$mask = $builder->get(); // int(15)
174-
175-
This integer bitmask can then be used to grant a user the base permissions you
180+
181+
This integer bitmask can then be used to grant a user the base permissions you
176182
added above:
177183

178184
.. code-block:: php

0 commit comments

Comments
 (0)