diff --git a/core/modules/contact/contact.routing.yml b/core/modules/contact/contact.routing.yml index f601f33..7932479 100644 --- a/core/modules/contact/contact.routing.yml +++ b/core/modules/contact/contact.routing.yml @@ -41,12 +41,8 @@ contact_site_page_category: _permission: 'access site-wide contact form' contact_personal_page: - pattern: 'user/{account}/contact' - options: - parameters: - account: - type: 'entity:user' + pattern: 'user/{user}/contact' defaults: _content: '\Drupal\contact\Controller\ContactPageController::contactPersonalPage' requirements: - _contact_personal_tab_access: 'TRUE' + _access_contact_personal_tab: 'TRUE' diff --git a/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php b/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php index 048ce2a..533c833 100644 --- a/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php +++ b/core/modules/contact/lib/Drupal/contact/Access/ContactPageAccess.php @@ -8,10 +8,10 @@ namespace Drupal\contact\Access; use Drupal\Core\Access\StaticAccessCheckInterface; +use Drupal\user\UserDataInterface; use Symfony\Component\Routing\Route; use Symfony\Component\HttpFoundation\Request; use Drupal\Core\Config\ConfigFactory; -use Drupal\User\UserData; /** * Access check for contact routes. @@ -19,25 +19,30 @@ class ContactPageAccess implements StaticAccessCheckInterface { /** - * Config Management Object. + * The contact settings config object. * * @var \Drupal\Core\Config\Config */ - protected $configFactory; + protected $config; /** - * User Data Service. + * The user data service. * - * @var \Drupal\user\UserData; + * @var \Drupal\user\UserDataInterface; */ protected $userData; /** - * Constructor for ContactPageAccess. + * Constructs a ContactPageAccess instance. + * + * @param \Drupal\Core\Config\ConfigFactory $config_factory + * The config factory. + * @param \Drupal\user\UserDataInterface $user_data + * The user data service. */ - public function __construct(ConfigFactory $configFactory, UserData $userData) { - $this->configFactory = $configFactory; - $this->userData = $userData; + public function __construct(ConfigFactory $config_factory, UserDataInterface $user_data) { + $this->config = $config_factory->get('contact.settings'); + $this->userData = $user_data; } /** @@ -45,16 +50,15 @@ public function __construct(ConfigFactory $configFactory, UserData $userData) { */ public function appliesTo() { // @see contact.routing.yml - return array('_contact_personal_tab_access'); + return array('_access_contact_personal_tab'); } /** * {@inheritdoc} */ public function access(Route $route, Request $request) { - global $user; - // Account that we want to contact. - $contact_account = $request->attributes->get('account'); + $contact_account = $request->attributes->get('user'); + $current_account = $request->attributes->get('_account'); // Anonymous users cannot have contact forms. if ($contact_account->isAnonymous()) { @@ -62,12 +66,12 @@ public function access(Route $route, Request $request) { } // Users may not contact themselves. - if ($user->id() == $contact_account->id()) { + if ($current_account->id() == $contact_account->id()) { return static::DENY; } // User administrators should always have access to personal contact forms. - if ($user->hasPermission('administer users')) { + if ($current_account->hasPermission('administer users')) { return static::ALLOW; } @@ -84,11 +88,11 @@ public function access(Route $route, Request $request) { } // If the requested user did not save a preference yet, deny access if the // configured default is disabled. - elseif (!$this->configFactory->get('contact.settings')->get('user_default_enabled')) { + elseif (!$this->config->get('user_default_enabled')) { return static::DENY; } - return $user->hasPermission('access user contact forms'); + return $current_account->hasPermission('access user contact forms'); } } diff --git a/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php b/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php index 6d31774..8c8bc03 100644 --- a/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php +++ b/core/modules/contact/lib/Drupal/contact/Controller/ContactPageController.php @@ -7,17 +7,17 @@ namespace Drupal\contact\Controller; -use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; - +use Drupal\Core\Session\AccountInterface; use Drupal\user\UserInterface; use Drupal\Core\Controller\ControllerInterface; use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Entity\EntityManager; - use Drupal\contact\Plugin\Core\Entity\Category; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + /** * Controller routines for contact routes. @@ -25,14 +25,14 @@ class ContactPageController implements ControllerInterface { /** - * Flood Control Object. + * The flood service. * * @var \Drupal\Core\Flood\FloodInterface */ protected $flood; /** - * Config Manager Object. + * The config factory. * * @var \Drupal\Core\Config\ConfigFactory */ @@ -46,49 +46,51 @@ class ContactPageController implements ControllerInterface { protected $entityManager; /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('flood'), - $container->get('config.factory'), - $container->get('plugin.manager.entity') - ); - } - - /** * Constructs a ContactController object. * * @param \Drupal\Core\Flood\FloodInterface $flood * Flood Control Interface. - * @param \Drupal\Core\Config\ConfigFactory $configFactory + * @param \Drupal\Core\Config\ConfigFactory $config_factory * Configuration Factory. - * @param \Drupal\Core\Entity\EntityManager $entityManager + * @param \Drupal\Core\Entity\EntityManager $entity_manager * Entity Manager. */ - public function __construct(FloodInterface $flood, ConfigFactory $configFactory, EntityManager $entity_manager) { + public function __construct(FloodInterface $flood, ConfigFactory $config_factory, EntityManager $entity_manager) { $this->flood = $flood; - $this->configFactory = $configFactory; + $this->configFactory = $config_factory; $this->entityManager = $entity_manager; } /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('flood'), + $container->get('config.factory'), + $container->get('plugin.manager.entity'), + $container->get('string_translation') + ); + } + + /** * Presents the site-wide contact form. * * @param \Drupal\contact\Plugin\Core\Entity\Category $contact_category * (optional) The contact category to use. + * @param \Drupal\Core\Session\AccountInterface $_account + * The current account. + * + * @return array + * The form as render array as expected by drupal_render(). * - * @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. + * Exception is thrown when user tries to access non existing contact + * category form and does not have permissions to set it up. */ - public function contactSitePage(Category $contact_category = NULL) { - global $user; - + public function contactSitePage(Category $contact_category = NULL, AccountInterface $_account) { // Check if flood control has been activated for sending e-mails. - if (!$user->hasPermission('administer contact forms')) { + if (!$_account->hasPermission('administer contact forms')) { $this->contactFloodControl(); } // Use the default category if no category has been passed. @@ -99,7 +101,7 @@ public function contactSitePage(Category $contact_category = NULL) { // If there are no categories, do not display the form. if (empty($contact_category)) { - if ($user->hasPermission('administer contact forms')) { + if ($_account->hasPermission('administer contact forms')) { drupal_set_message(t('The contact form has not been configured. Add one or more categories to the form.', array('@add' => url('admin/structure/contact/add'))), 'error'); return array(); } @@ -124,25 +126,27 @@ public function contactSitePage(Category $contact_category = NULL) { /** * Form constructor for the personal contact form. * - * @param \Drupal\user\UserInterface $account + * @param \Drupal\user\UserInterface $user * The account for which a personal contact form should be generated. + * @param \Drupal\Core\Session\AccountInterface $_account + * The current user. * - * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * @return array + * The personal contact form as render array as expected by drupal_render(). */ - public function contactPersonalPage(UserInterface $account) { + public function contactPersonalPage(UserInterface $user, AccountInterface $_account) { // Check if flood control has been activated for sending e-mails. - global $user; - if (!$user->hasPermission('administer contact forms') && !$user->hasPermission('administer users')) { + if (!$_account->hasPermission('administer contact forms') && !$_account->hasPermission('administer users')) { $this->contactFloodControl(); } - drupal_set_title(t('Contact @username', array('@username' => $account->getUsername())), PASS_THROUGH); + drupal_set_title(t('Contact @username', array('@username' => $user->getUsername())), PASS_THROUGH); $message = $this->entityManager ->getStorageController('contact_message') ->create(array( 'category' => 'personal', - 'recipient' => $account->id(), + 'recipient' => $user->id(), )); return $this->entityManager->getForm($message); @@ -158,7 +162,7 @@ protected function contactFloodControl() { $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( + drupal_set_message(t('You cannot send more than %limit messages in @interval. Try again later.', array( '%limit' => $limit, '@interval' => format_interval($interval), )), 'error');