Skip to content
Snippets Groups Projects
UwNodeAccessCheck.php 3.54 KiB
<?php

namespace Drupal\uw_cfg_common\Access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\menu_admin_per_menu\Access\MenuAdminPerMenuAccess;
use Drupal\uw_cfg_common\Service\UWService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Checks access for displaying configuration translation page.
 */
class UwNodeAccessCheck implements AccessInterface {

  /**
   * A custom access check.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   Route matching.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result.
   */
  public function access(RouteMatchInterface $route_match, AccountInterface $account): AccessResult {
    $route_name = $route_match->getRouteName();

    // Menu link edit pages.
    if ($route_name === 'menu_ui.link_edit') {
      $menu_link_plugin = $route_match->getParameter('menu_link_plugin');
      // Only those with permission may edit home page menu entry.
      if ($menu_link_plugin->getPluginId() === 'uw_base_profile.front_page') {
        return $account->hasPermission('bypass home page protection') ? AccessResult::allowed() : AccessResult::forbidden();
      }
      else {
        // Otherwise, default to access set in menu_admin_per_menu.
        $menu_admin_per_menu = new MenuAdminPerMenuAccess();
        return $menu_admin_per_menu->menuLinkAccess($account, $menu_link_plugin);
      }
    }

    // Node delete pages.
    if ($route_name === 'entity.node.delete_form') {
      $node = $route_match->getParameter('node');
      // Only those with permission may delete the home page.
      if ($node && UWService::nodeIsHomePage((int) $node->id())) {
        return $account->hasPermission('bypass home page protection') ? AccessResult::allowed() : AccessResult::forbidden();
      }
      else {
        return AccessResult::allowed();
      }
    }

    // Dashboard config: admin/config/dashboards/dashboardssettings.
    if ($route_name === 'dashboards.dashboards_settings_form') {
      return $account->hasPermission('access dashboard config') ? AccessResult::allowed() : AccessResult::forbidden();
    }

    // Get the node object, which is in the route match variable.
    $node = $route_match->getParameter('node');

    // Check if this is a sidebar content type and if the user has permission
    // to edit the content type. We want to throw a 404 (NotFoundHttpException)
    // if they do not have access.  This is the case when a user is not logged
    // in, and when they do not have permission to edit it.
    if ($node && $node->bundle() == 'uw_ct_sidebar' && !$account->hasPermission('edit any uw_ct_sidebar content')) {
      throw new NotFoundHttpException();
    }

    // Check if this is a sidebar content type and if the user has permission
    // to edit the content type. We want to throw a 404 (NotFoundHttpException)
    // if they do not have access.  This is the case when a user is not logged
    // in, and when they do not have permission to edit it.
    if ($node && $node->bundle() == 'uw_ct_site_footer' && !$account->hasPermission('edit any uw_ct_site_footer content')) {
      throw new NotFoundHttpException();
    }

    // We have to return some type of access, so we are going to return
    // allowed, if they do not have access, the new exception is going to be
    // thrown above.
    return AccessResult::allowed();
  }

}