diff --git a/core/modules/contact/config/contact.category.user.yml b/core/modules/contact/config/contact.category.user.yml new file mode 100644 index 0000000..fcd7b66 --- /dev/null +++ b/core/modules/contact/config/contact.category.user.yml @@ -0,0 +1,5 @@ +id: user +label: 'Personal contact form' +recipients: [] +reply: '' +weight: '0' diff --git a/core/modules/contact/contact.pages.inc b/core/modules/contact/contact.pages.inc index 5ee9fb7..92eccc2 100644 --- a/core/modules/contact/contact.pages.inc +++ b/core/modules/contact/contact.pages.inc @@ -76,6 +76,7 @@ function contact_personal_page($recipient) { $message = entity_create('contact_message', array( 'recipient' => $recipient, + 'category' => 'user', )); return entity_get_form($message); } diff --git a/core/modules/contact/contact.routing.yml b/core/modules/contact/contact.routing.yml index 1f34937..8538973 100644 --- a/core/modules/contact/contact.routing.yml +++ b/core/modules/contact/contact.routing.yml @@ -3,4 +3,4 @@ contact_category_delete: defaults: _form: '\Drupal\contact\Form\DeleteForm' requirements: - _permission: 'administer contact forms' + _access_category_delete: 'TRUE' diff --git a/core/modules/contact/lib/Drupal/contact/Access/ContactDeleteAccessCheck.php b/core/modules/contact/lib/Drupal/contact/Access/ContactDeleteAccessCheck.php new file mode 100644 index 0000000..1d547aa --- /dev/null +++ b/core/modules/contact/lib/Drupal/contact/Access/ContactDeleteAccessCheck.php @@ -0,0 +1,34 @@ +getRequirements()); + } + + /** + * Implements AccessCheckInterface::access(). + */ + public function access(Route $route, Request $request) { + $entity = $request->attributes->get('contact_category'); + return user_access('administer contact forms') && ($entity && $entity->id() != 'user'); + } + +} diff --git a/core/modules/contact/lib/Drupal/contact/CategoryFormController.php b/core/modules/contact/lib/Drupal/contact/CategoryFormController.php index d60308f..498345e 100644 --- a/core/modules/contact/lib/Drupal/contact/CategoryFormController.php +++ b/core/modules/contact/lib/Drupal/contact/CategoryFormController.php @@ -37,7 +37,7 @@ public function form(array $form, array &$form_state, EntityInterface $category) '#machine_name' => array( 'exists' => 'contact_category_load', ), - '#disabled' => !$category->isNew(), + '#disabled' => !$category->isNew() || $category->id() == 'user', ); $form['recipients'] = array( '#type' => 'textarea', diff --git a/core/modules/contact/lib/Drupal/contact/CategoryListController.php b/core/modules/contact/lib/Drupal/contact/CategoryListController.php index a3ec886..420299e 100644 --- a/core/modules/contact/lib/Drupal/contact/CategoryListController.php +++ b/core/modules/contact/lib/Drupal/contact/CategoryListController.php @@ -34,6 +34,10 @@ public function getOperations(EntityInterface $entity) { 'weight' => 12, ); } + // Do not allow delete 'user' category used for personal contact form. + if ($entity->id() == 'user') { + unset($operations['delete']); + } return $operations; } diff --git a/core/modules/contact/lib/Drupal/contact/ContactBundle.php b/core/modules/contact/lib/Drupal/contact/ContactBundle.php new file mode 100644 index 0000000..218abd7 --- /dev/null +++ b/core/modules/contact/lib/Drupal/contact/ContactBundle.php @@ -0,0 +1,27 @@ +register('access_check.contact.delete', 'Drupal\contact\Access\ContactDeleteAccessCheck') + ->addTag('access_check'); + } + +} diff --git a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php index bdc98ef..e05cc35 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php @@ -49,8 +49,16 @@ function testSiteWideContact() { $this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration')); $this->assertText(t('The configuration options have been saved.')); + $this->drupalGet('admin/structure/contact'); + // Default category exists. + $this->assertLinkByHref('admin/structure/contact/manage/feedback/delete'); + // User category could not be deleted. + $this->assertNoLinkByHref('admin/structure/contact/manage/user/delete'); // Delete old categories to ensure that new categories are used. $this->deleteCategories(); + $this->drupalGet('admin/structure/contact'); + $this->assertLinkByHref('admin/structure/contact/manage/user'); + $this->assertNoLinkByHref('admin/structure/contact/manage/feedback'); // Ensure that the contact form won't be shown without categories. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form')); @@ -174,10 +182,6 @@ function testSiteWideContact() { $this->drupalGet('contact'); $this->assertResponse(403); $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => config('contact.settings')->get('flood.limit'), '@interval' => format_interval(600)))); - - // Delete created categories. - $this->drupalLogin($admin_user); - $this->deleteCategories(); } /** @@ -308,9 +312,16 @@ function submitContact($name, $mail, $subject, $id, $message) { function deleteCategories() { $categories = entity_load_multiple('contact_category'); foreach ($categories as $id => $category) { - $this->drupalPost("admin/structure/contact/manage/$id/delete", array(), t('Delete')); - $this->assertRaw(t('Category %label has been deleted.', array('%label' => $category->label()))); - $this->assertFalse(entity_load('contact_category', $id), format_string('Category %category not found', array('%category' => $category->label()))); + if ($id == 'user') { + // Personal category could not be deleted. + $this->drupalGet("admin/structure/contact/manage/$id/delete"); + $this->assertResponse(403); + } + else { + $this->drupalPost("admin/structure/contact/manage/$id/delete", array(), t('Delete')); + $this->assertRaw(t('Category %label has been deleted.', array('%label' => $category->label()))); + $this->assertFalse(entity_load('contact_category', $id), format_string('Category %category not found', array('%category' => $category->label()))); + } } }