SearchPageAccessControlHandler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Drupal\search;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Access\AccessibleInterface;
  5. use Drupal\Core\Entity\EntityAccessControlHandler;
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\Core\Session\AccountInterface;
  8. /**
  9. * Defines the access control handler for the search page entity type.
  10. *
  11. * @see \Drupal\search\Entity\SearchPage
  12. */
  13. class SearchPageAccessControlHandler extends EntityAccessControlHandler {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  18. /** @var $entity \Drupal\search\SearchPageInterface */
  19. if (in_array($operation, ['delete', 'disable'])) {
  20. if ($entity->isDefaultSearch()) {
  21. return AccessResult::forbidden()->addCacheableDependency($entity);
  22. }
  23. else {
  24. return parent::checkAccess($entity, $operation, $account)->addCacheableDependency($entity);
  25. }
  26. }
  27. if ($operation == 'view') {
  28. if (!$entity->status()) {
  29. return AccessResult::forbidden()->addCacheableDependency($entity);
  30. }
  31. $plugin = $entity->getPlugin();
  32. if ($plugin instanceof AccessibleInterface) {
  33. return $plugin->access($operation, $account, TRUE)->addCacheableDependency($entity);
  34. }
  35. return AccessResult::allowed()->addCacheableDependency($entity);
  36. }
  37. return parent::checkAccess($entity, $operation, $account);
  38. }
  39. }