diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/SetSwitchAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/SetSwitchAccessCheck.php index 9177033..e3aa6aa 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Access/SetSwitchAccessCheck.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Access/SetSwitchAccessCheck.php @@ -27,26 +27,20 @@ public function applies(Route $route) { * {@inheritdoc} */ public function access(Route $route, Request $request) { - $global_account = $request->attributes->get('account'); - $account = $request->attributes->get('_account'); + $account = $request->attributes->get('account'); - if (user_access('administer shortcuts', $global_account)) { - // Administrators can switch anyone's shortcut set. - return static::ALLOW; - } - - if (!user_access('switch shortcut sets', $global_account)) { - // The user has no permission to switch anyone's shortcut set. + // @todo For some reasons account might not exist when checking menu link + // access. + if (!isset($account)) { return static::DENY; } + $user = $request->attributes->get('user'); - if (!isset($account) || $global_account->id() == $account->id()) { - // Users with the 'switch shortcut sets' permission can switch their own - // shortcuts sets. - return static::ALLOW; - } + // Users with the 'switch shortcut sets' permission can switch their own + // shortcuts sets. + $access = user_access('switch shortcut sets', $account) && $user->id() == $account->id(); - return static::DENY; + return $access ? static::ALLOW : static::DENY; } } diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php index 0409cd2..165fa11 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/SetSwitch.php @@ -11,7 +11,10 @@ use Drupal\Core\Entity\EntityManager; use Drupal\Core\Form\FormInterface; use Drupal\Component\Utility\String; +use Drupal\Core\Routing\PathBasedGeneratorInterface; +use Drupal\Core\Routing\UrlGenerator; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\Translator\TranslatorInterface; use Drupal\user\UserInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -26,7 +29,7 @@ class SetSwitch implements ControllerInterface, FormInterface { * * @var \Drupal\user\UserInterface */ - protected $account; + protected $user; /** * The request object. @@ -50,14 +53,24 @@ class SetSwitch implements ControllerInterface, FormInterface { protected $userStorageController; /** + * The url generator. + * + * @var \Drupal\Core\Routing\PathBasedGeneratorInterface + */ + protected $urlGenerator; + + /** * Constructs a SetSwitch object. * * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager. + * @param \Drupal\Core\Routing\PathBasedGeneratorInterface $url_generator + * The url generator */ - public function __construct(EntityManager $entity_manager) { + public function __construct(EntityManager $entity_manager, PathBasedGeneratorInterface $url_generator) { $this->storageController = $entity_manager->getStorageController('shortcut'); $this->userStorageController = $entity_manager->getStorageController('user'); + $this->urlGenerator = $url_generator; } /** @@ -65,7 +78,9 @@ public function __construct(EntityManager $entity_manager) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('plugin.manager.entity') + $container->get('plugin.manager.entity'), + $container->get('url_generator'), + $container->get('string_translation') ); } @@ -79,25 +94,26 @@ public function getFormID() { /** * {@inheritdoc} */ - public function buildForm(array $form, array &$form_state, UserInterface $_account = NULL, Request $request = NULL) { - global $user; + public function buildForm(array $form, array &$form_state, UserInterface $user = NULL, Request $request = NULL) { $this->request = $request; - $this->account = $this->userStorageController->load($_account->id())->getBCEntity(); + $account = $request->attributes->get('account'); + + $this->user = $this->userStorageController->load($user->id())->getBCEntity(); // Prepare the list of shortcut sets. $options = array_map(function ($set) { return String::checkPlain($set->label()); }, $this->storageController->loadMultiple()); - $current_set = shortcut_current_displayed_set($this->account); + $current_set = shortcut_current_displayed_set($this->user); // Only administrators can add shortcut sets. - $add_access = user_access('administer shortcuts'); + $add_access = user_access('administer shortcuts', $account); if ($add_access) { $options['new'] = t('New set'); } - $account_is_user = $this->account->id() == $user->uid; + $account_is_user = $this->user->id() == $account->id(); if (count($options) > 1) { $form['set'] = array( '#type' => 'radios', @@ -131,7 +147,7 @@ public function buildForm(array $form, array &$form_state, UserInterface $_accou ); if (!$account_is_user) { - $default_set = $this->storageController->getDefaultSet($this->account->getBCEntity()); + $default_set = $this->storageController->getDefaultSet($this->user->getBCEntity()); $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label())); } @@ -175,12 +191,12 @@ public function validateForm(array &$form, array &$form_state) { * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { - global $user; + $account = $this->request->attributes->get('account'); - $account_is_user = $this->account->id() == $user->uid; + $account_is_user = $this->user->id() == $account->id(); if ($form_state['values']['set'] == 'new') { // Save a new shortcut set with links copied from the user's default set. - $default_set = $this->storageController->getDefaultSet($this->account); + $default_set = $this->storageController->getDefaultSet($this->user); $set = $this->storageController->create(array( 'id' => $form_state['values']['id'], 'label' => $form_state['values']['label'], @@ -188,9 +204,9 @@ public function submitForm(array &$form, array &$form_state) { )); $set->save(); $replacements = array( - '%user' => $this->account->label(), + '%user' => $this->user->label(), '%set_name' => $set->label(), - '@switch-url' => url($this->request->attributes->get('system_path')), + '@switch-url' => $this->urlGenerator->generateFromPath($this->request->attributes->get('system_path')), ); if ($account_is_user) { // Only administrators can create new shortcut sets, so we know they have @@ -206,14 +222,14 @@ public function submitForm(array &$form, array &$form_state) { // Switch to a different shortcut set. $set = $this->storageController->load($form_state['values']['set']); $replacements = array( - '%user' => $this->account->label(), + '%user' => $this->user->label(), '%set_name' => $set->label(), ); drupal_set_message($account_is_user ? t('You are now using the %set_name shortcut set.', $replacements) : t('%user is now using the %set_name shortcut set.', $replacements)); } // Assign the shortcut set to the provided user account. - $this->storageController->assignUser($set, $this->account); + $this->storageController->assignUser($set, $this->user); } /** @@ -226,8 +242,8 @@ public function submitForm(array &$form, array &$form_state) { * TRUE if the shortcut set exists, FALSE otherwise. */ public function exists($id) { - $sets = $this->storageController->loadMultiple(array($id)); - return !empty($sets[$id]); + $sets = $this->storageController->load($id); + return !empty($set); } } diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index ca4cef0..f3c73e6 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -27,13 +27,11 @@ shortcut_set_edit: _entity_access: 'shortcut.update' shortcut_set_switch: - pattern: '/user/{_account}/shortcuts' + pattern: '/user/{user}/shortcuts' defaults: _form: 'Drupal\shortcut\Form\SetSwitch' - options: - converters: - _account: 'user' requirements: + _permission: 'administer shortcuts' _shortcut_set_switch: 'TRUE' shortcut_link_add_inline: