diff -u b/core/modules/contact/contact.module b/core/modules/contact/contact.module --- b/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -87,14 +87,12 @@ 'title' => 'Contact', 'route_name' => 'contact_site_page', 'menu_name' => 'footer', - 'type' => MENU_SUGGESTED_ITEM, ); $items['contact/%contact_category'] = array( 'title' => 'Contact category form', 'title callback' => 'entity_page_label', 'title arguments' => array(1), - 'route_name' => 'contact_site_page', - 'access arguments' => array('access site-wide contact form'), + 'route_name' => 'contact_site_page_category', 'type' => MENU_VISIBLE_IN_BREADCRUMB, ); $items['user/%user/contact'] = array( diff -u b/core/modules/contact/contact.routing.yml b/core/modules/contact/contact.routing.yml --- b/core/modules/contact/contact.routing.yml +++ b/core/modules/contact/contact.routing.yml @@ -27,15 +27,24 @@ _entity_access: contact_category.update contact_site_page: + pattern: 'contact' + defaults: + _content: '\Drupal\contact\Controller\ContactPageController::contactSitePage' + requirements: + _permission: 'access site-wide contact form' + +contact_site_page_category: pattern: 'contact/{contact_category}' defaults: _content: '\Drupal\contact\Controller\ContactPageController::contactSitePage' - contact_category: 0 requirements: _permission: 'access site-wide contact form' contact_personal_page: - pattern: 'user/{user}/contact' + pattern: 'user/{account}/contact' + options: + converters: + account: 'user' defaults: _content: '\Drupal\contact\Controller\ContactPageController::contactPersonalPage' requirements: diff -u b/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php b/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php --- b/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php +++ b/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php @@ -14,7 +14,7 @@ use Drupal\User\UserData; /** - * Access check for test routes. + * Access check for contact routes. */ class ContactPageAccess implements AccessCheckInterface { @@ -39,6 +39,7 @@ $this->configFactory = $configFactory; $this->userData = $userData; } + /** * Implements AccessCheckInterface::applies(). */ @@ -53,38 +54,40 @@ global $user; - $account = $request->attributes->get('user'); + // Account that we want to contact. + $contact_account = $request->attributes->get('account'); // Anonymous users cannot have contact forms. - if (!$account->uid) { - return FALSE; + if (!$contact_account->id()) { + return static::DENY; } // Users may not contact themselves. - if ($user->uid == $account->uid) { - return FALSE; + if ($user->id() == $contact_account->id()) { + return static::DENY; } // User administrators should always have access to personal contact forms. - if (user_access('administer users')) { - return TRUE; + if (user_access('administer users', $user)) { + return static::ALLOW; } // If requested user has been blocked, do not allow users to contact them. - if (empty($account->status)) { - return FALSE; + if (empty($contact_account->status)) { + return static::DENY; } // If the requested user has disabled their contact form, do not allow users // to contact them. - $account_data = $this->userData->get('contact', $account->id(), 'enabled'); + $account_data = $this->userData->get('contact', $contact_account->id(), 'enabled'); if (isset($account_data) && empty($account_data)) { - return FALSE; + return static::DENY; } // If the requested user did not save a preference yet, deny access if the // configured default is disabled. - elseif (!$this->configFactory('contact.settings')->get('user_default_enabled')) { - return FALSE; + elseif (!$this->configFactory->get('contact.settings')->get('user_default_enabled')) { + return static::DENY; } - return user_access('access user contact forms'); + return user_access('access user contact forms', $user); } + } diff -u b/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php b/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php --- b/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php +++ b/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php @@ -1,14 +1,23 @@ get('flood'), $container->get('config.factory')); + return new static( + $container->get('flood'), + $container->get('config.factory'), + $container->get('plugin.manager.entity') + ); } /** @@ -48,34 +65,41 @@ * @param \Drupal\Core\Config\ConfigFactory $configFactory * Configuration Factory. */ - public function __construct(FloodInterface $flood, ConfigFactory $configFactory) { + public function __construct(FloodInterface $flood, ConfigFactory $configFactory, EntityManager $entity_manager) { $this->flood = $flood; $this->configFactory = $configFactory; + $this->entityManager = $entity_manager; } + /** * Presents the site-wide contact form. * - * @param Drupal\contact\Plugin\Core\Entity\Category $category + * @param Drupal\contact\Plugin\Core\Entity\Category $contact_category * (optional) The contact category to use. * * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * Exception is thrown when user doesn't pass flood control check. + * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException + * Exception is thrown when user tries to access non existing contact category + * form and doesn't have permissions to set it up. * * @see contact_menu() * @see contact_site_form_submit() * @ingroup forms */ public function contactSitePage(Category $contact_category = NULL) { - $category = $contact_category; // Check if flood control has been activated for sending e-mails. if (!user_access('administer contact forms')) { $this->contactFloodControl(); } - if (!isset($category)) { - $categories = entity_load_multiple('contact_category'); - $default_category = config('contact.settings')->get('default_category'); + if (empty($contact_category)) { + $controller = $this->entityManager->getStorageController('contact_category'); + $categories = $controller->loadMultiple(); + + $default_category = $this->configFactory->get('contact.settings')->get('default_category'); if (isset($categories[$default_category])) { - $category = $categories[$default_category]; + $contact_category = $categories[$default_category]; } // If there are no categories, do not display the form. else { @@ -88,10 +112,13 @@ } } } - $message = entity_create('contact_message', array( - 'category' => $category->id(), - )); - return entity_get_form($message); + $message = $this->entityManager + ->getStorageController('contact_message') + ->create(array( + 'category' => $contact_category->id(), + )); + + return $this->entityManager->getForm($message); } /** @@ -108,41 +135,42 @@ * @ingroup forms */ - public function contactPersonalPage($user) { - $recipient = $user; - global $user; - + public function contactPersonalPage(AccountInterface $account) { // Check if flood control has been activated for sending e-mails. - if (!user_access('administer contact forms') && !user_access('administer users')) { + if (!user_access('administer contact forms', $account) && !user_access('administer users', $account)) { $this->contactFloodControl(); } - drupal_set_title(t('Contact @username', array('@username' => user_format_name($recipient))), PASS_THROUGH); + drupal_set_title(t('Contact @username', array('@username' => $account->getUsername())), PASS_THROUGH); + + $message = $this->entityManager + ->getStorageController('contact_message') + ->create(array( + 'category' => 'personal', + 'recipient' => $account->id(), + )); - $message = entity_create('contact_message', array( - 'recipient' => $recipient, - )); - return entity_get_form($message); + return $this->entityManager->getForm($message); } -/** - * Throws an exception if the current user is not allowed to submit a contact form. - * - * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException - * - * @see \Drupal\contact\Controller\ContactSitePage() - * @see \Drupal\contact\Controller\ContactPersonalPage() - */ -protected function contactFloodControl() { - $config = $this->configFactory('contact.settings'); - $limit = $config->get('flood.limit'); - $interval = $config->get('flood.interval'); - if (!$this->flood->isAllowed('contact', $limit, $interval)) { - drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array( - '%limit' => $limit, - '@interval' => format_interval($interval), - )), 'error'); - throw new AccessDeniedHttpException(); + /** + * Throws an exception if the current user is not allowed to submit a contact form. + * + * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * + * @see \Drupal\contact\Controller\ContactSitePage() + * @see \Drupal\contact\Controller\ContactPersonalPage() + */ + protected function contactFloodControl() { + $contact_settings = $this->configFactory->get('contact.settings'); + $limit = $contact_settings->get('flood.limit'); + $interval = $contact_settings->get('flood.interval'); + if (!$this->flood->isAllowed('contact', $limit, $interval)) { + drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array( + '%limit' => $limit, + '@interval' => format_interval($interval), + )), 'error'); + throw new AccessDeniedHttpException(); + } } -} }