Skip to content

Commit 1c0c9ca

Browse files
vegantriathletepaul-m
vegantriathlete
authored andcommitted
Issue #2913655 by vegantriathlete, Mile23: ContactAccessControlHandler creates access bypass?
1 parent 64a99ff commit 1c0c9ca

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

Diff for: content_entity_example/content_entity_example.routing.yml

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
entity.content_entity_example_contact.canonical:
66
path: '/content_entity_example_contact/{content_entity_example_contact}'
77
defaults:
8-
# Calls the view controller, defined in the annotation of the contact entity
8+
# Calls the view controller, defined in the annotation of the contact
9+
# entity. This marks this route as belonging to this entity type.
910
_entity_view: 'content_entity_example_contact'
1011
_title: 'Contact content'
1112
requirements:
12-
# Calls the access controller of the entity, $operation 'view'
13+
# Calls the access controller of the entity, passing in the suffix ('view')
14+
# as the $operation parameter to checkAccess().
1315
_entity_access: 'content_entity_example_contact.view'
1416

1517
entity.content_entity_example_contact.collection:
@@ -25,20 +27,25 @@ entity.content_entity_example_contact.collection:
2527
content_entity_example.contact_add:
2628
path: '/content_entity_example_contact/add'
2729
defaults:
28-
# Calls the form.add controller, defined in the contact entity.
29-
_entity_form: content_entity_example_contact.add
30+
# Calls the form.add controller, defined in the contact entity.
31+
_entity_form: content_entity_example_contact.default
3032
_title: 'Add contact'
3133
requirements:
34+
# Use the entity's access controller. _entity_create_access tells the router
35+
# to use the access controller's checkCreateAccess() method instead of
36+
# checkAccess().
3237
_entity_create_access: 'content_entity_example_contact'
3338

3439
entity.content_entity_example_contact.edit_form:
3540
path: '/content_entity_example_contact/{content_entity_example_contact}/edit'
3641
defaults:
3742
# Calls the form.edit controller, defined in the contact entity.
38-
_entity_form: content_entity_example_contact.edit
43+
_entity_form: content_entity_example_contact.default
3944
_title: 'Edit contact'
4045
requirements:
41-
_entity_access: 'content_entity_example_contact.edit'
46+
# Calls the access controller of the entity, passing in the suffix
47+
# ('update') as the $operation parameter to checkAccess().
48+
_entity_access: 'content_entity_example_contact.update'
4249

4350
entity.content_entity_example_contact.delete_form:
4451
path: '/contact/{content_entity_example_contact}/delete'
@@ -47,6 +54,8 @@ entity.content_entity_example_contact.delete_form:
4754
_entity_form: content_entity_example_contact.delete
4855
_title: 'Delete contact'
4956
requirements:
57+
# Calls the access controller of the entity, passing in the suffix
58+
# ('delete') as the $operation parameter to checkAccess().
5059
_entity_access: 'content_entity_example_contact.delete'
5160

5261
content_entity_example.contact_settings:

Diff for: content_entity_example/src/ContactAccessControlHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
2929
case 'view':
3030
return AccessResult::allowedIfHasPermission($account, 'view contact entity');
3131

32-
case 'edit':
32+
case 'update':
3333
return AccessResult::allowedIfHasPermission($account, 'edit contact entity');
3434

3535
case 'delete':

Diff for: content_entity_example/src/Entity/Contact.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@
4242
*
4343
* - form: We derive our own forms to add functionality like additional fields,
4444
* redirects etc. These forms are used when the route specifies an
45-
* '_entity_form' default for the entity type. Depending on the suffix
46-
* (.add/.edit/.delete) of the '_entity_form' default, the form specified in
47-
* the annotation is used.
45+
* '_entity_form' or '_entity_create_access' for the entity type. Depending on
46+
* the suffix (.add/.default/.delete) of the '_entity_form' default in the
47+
* route, the form specified in the annotation is used. The suffix then also
48+
* becomes the $operation parameter to the access handler. We use the
49+
* '.default' suffix for all operations that are not 'delete'.
4850
*
4951
* - access: Our own access controller, where we determine access rights based
5052
* on permissions.
@@ -81,8 +83,7 @@
8183
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
8284
* "list_builder" = "Drupal\content_entity_example\Entity\Controller\ContactListBuilder",
8385
* "form" = {
84-
* "add" = "Drupal\content_entity_example\Form\ContactForm",
85-
* "edit" = "Drupal\content_entity_example\Form\ContactForm",
86+
* "default" = "Drupal\content_entity_example\Form\ContactForm",
8687
* "delete" = "Drupal\content_entity_example\Form\ContactDeleteForm",
8788
* },
8889
* "access" = "Drupal\content_entity_example\ContactAccessControlHandler",
@@ -107,10 +108,12 @@
107108
* The 'links' above are defined by their path. For core to find the
108109
* corresponding route, the route name must follow the correct pattern:
109110
*
110-
* entity.<entity-name>.<link-name> (replace dashes with underscores)
111-
* Example: 'entity.content_entity_example_contact.canonical'
111+
* entity.<entity_type>.<link_name>
112112
*
113-
* See routing file above for the corresponding implementation
113+
* Example: 'entity.content_entity_example_contact.canonical'.
114+
*
115+
* See the routing file at content_entity_example.routing.yml for the
116+
* corresponding implementation.
114117
*
115118
* The Contact class defines methods and fields for the contact entity.
116119
*

0 commit comments

Comments
 (0)