diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutSetSwitchAccessCheck.php b/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutSetSwitchAccessCheck.php deleted file mode 100644 index 5046f77..0000000 --- a/core/modules/shortcut/lib/Drupal/shortcut/Access/ShortcutSetSwitchAccessCheck.php +++ /dev/null @@ -1,51 +0,0 @@ -attributes->get('account'); - - if ($user->hasPermission('administer shortcuts')) { - // Administrators can switch anyone's shortcut set. - return static::ALLOW; - } - - if (!$user->hasPermission('switch shortcut sets')) { - // The user has no permission to switch anyone's shortcut set. - return static::DENY; - } - - if (!isset($account) || $user->id() == $account->id()) { - // Users with the 'switch shortcut sets' permission can switch their own - // shortcuts sets. - return static::ALLOW; - } - return static::DENY; - } - -} diff --git a/core/modules/shortcut/lib/Drupal/shortcut/Form/SwitchShortcutSet.php b/core/modules/shortcut/lib/Drupal/shortcut/Form/SwitchShortcutSet.php index 05bf9af..1d30afc 100644 --- a/core/modules/shortcut/lib/Drupal/shortcut/Form/SwitchShortcutSet.php +++ b/core/modules/shortcut/lib/Drupal/shortcut/Form/SwitchShortcutSet.php @@ -7,24 +7,18 @@ namespace Drupal\shortcut\Form; -use Drupal\Core\Controller\ControllerInterface; -use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\Core\Entity\EntityManager; +use Drupal\Component\Utility\String; +use Drupal\Core\Access\AccessInterface; use Drupal\Core\Entity\Query\QueryFactory; -use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Form\FormBase; -use Drupal\Core\Form\FormInterface; -use Drupal\Component\Utility\String; -use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\shortcut\ShortcutSetStorageControllerInterface; use Drupal\user\UserInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; /** * Builds the shortcut set switch form. */ -class SwitchShortcutSet extends FormBase implements ContainerInjectionInterface, FormInterface { +class SwitchShortcutSet extends FormBase { /** * The account the shortcut set is for. @@ -192,7 +186,7 @@ public function submitForm(array &$form, array &$form_state) { '%user' => $this->user->label(), '%set_name' => $set->label(), // @todo Convert once https://drupal.org/node/2073813 is in. - '@switch-url' => \Drupal::urlGenerator()->generateFromPath($this->getRequest()->attributes->get('_system_path')), + '@switch-url' => $this->urlGenerator()->generateFromPath($this->getRequest()->attributes->get('_system_path')), ); if ($account_is_user) { // Only administrators can create new shortcut sets, so we know they have @@ -234,4 +228,33 @@ public function exists($id) { ->execute(); } + /** + * Checks access for the shortcut set switch form. + * + * @param \Drupal\user\UserInterface $user + * The user whose shortcut sets are being switched. + * + * @return bool|null + * AccessInterface::ALLOW, AccessInterface::DENY, or AccessInterface::KILL. + */ + public function checkAccess(UserInterface $user) { + $account = $this->currentUser(); + if ($account->hasPermission('administer shortcuts')) { + // Administrators can switch anyone's shortcut set. + return AccessInterface::ALLOW; + } + + if (!$account->hasPermission('switch shortcut sets')) { + // The user has no permission to switch anyone's shortcut set. + return AccessInterface::DENY; + } + + if ($account->id() == $user->id()) { + // Users with the 'switch shortcut sets' permission can switch their own + // shortcuts sets. + return AccessInterface::ALLOW; + } + return AccessInterface::DENY; + } + } diff --git a/core/modules/shortcut/shortcut.routing.yml b/core/modules/shortcut/shortcut.routing.yml index 4884d0e..3bd6610 100644 --- a/core/modules/shortcut/shortcut.routing.yml +++ b/core/modules/shortcut/shortcut.routing.yml @@ -34,17 +34,6 @@ shortcut.set_edit: requirements: _entity_access: 'shortcut_set.update' -shortcut.set_switch: - path: '/user/{user}/shortcuts' - defaults: - _form: 'Drupal\shortcut\Form\SwitchShortcutSet' - _title: 'Shortcuts' - options: - _access_mode: 'ALL' - requirements: - _permission: 'administer shortcuts' - _access_shortcut_set_switch: 'TRUE' - shortcut.link_add_inline: path: '/admin/config/user-interface/shortcut/manage/{shortcut_set}/add-link-inline' defaults: @@ -74,3 +63,11 @@ shortcut.link_edit: _title: 'Add Shortcut' requirements: _access_shortcut_link: 'TRUE' + +shortcut.set_switch: + path: '/user/{user}/shortcuts' + defaults: + _form: 'Drupal\shortcut\Form\SwitchShortcutSet' + _title: 'Shortcuts' + requirements: + _custom_access: 'Drupal\shortcut\Form\SwitchShortcutSet::checkAccess' diff --git a/core/modules/shortcut/shortcut.services.yml b/core/modules/shortcut/shortcut.services.yml index c5a4bc5..0da60ec 100644 --- a/core/modules/shortcut/shortcut.services.yml +++ b/core/modules/shortcut/shortcut.services.yml @@ -8,8 +8,3 @@ services: class: Drupal\shortcut\Access\ShortcutSetEditAccessCheck tags: - { name: access_check } - - access_check.shortcut.set_switch: - class: Drupal\shortcut\Access\SwitchShortcutSetAccessCheck - tags: - - { name: access_check }