You are here

class GroupInstalledContentAccessCheck in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Access/GroupInstalledContentAccessCheck.php \Drupal\group\Access\GroupInstalledContentAccessCheck

Determines access to routes based on whether a content plugin is installed.

Hierarchy

Expanded class hierarchy of GroupInstalledContentAccessCheck

1 string reference to 'GroupInstalledContentAccessCheck'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses GroupInstalledContentAccessCheck
access_check.group.installed_content in ./group.services.yml
Drupal\group\Access\GroupInstalledContentAccessCheck

File

src/Access/GroupInstalledContentAccessCheck.php, line 15

Namespace

Drupal\group\Access
View source
class GroupInstalledContentAccessCheck implements AccessInterface {

  /**
   * Checks access.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The parametrized route.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account to check access for.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    $access_string = $route
      ->getRequirement('_group_installed_content');

    // Don't interfere if no plugin ID was specified.
    if ($access_string === NULL) {
      return AccessResult::neutral();
    }

    // Don't interfere if no group was specified.
    $parameters = $route_match
      ->getParameters();
    if (!$parameters
      ->has('group')) {
      return AccessResult::neutral();
    }

    // Don't interfere if the group isn't a real group.
    $group = $parameters
      ->get('group');
    if (!$group instanceof GroupInterface) {
      return AccessResult::neutral();
    }

    // We default to not granting access.
    $access = FALSE;

    // Allow to conjunct the plugin IDs with OR ('+') or AND (',').
    $plugin_ids = explode(',', $access_string);
    if (count($plugin_ids) > 1) {
      $access = TRUE;
      foreach ($plugin_ids as $plugin_id) {
        if (!$group
          ->getGroupType()
          ->hasContentPlugin($plugin_id)) {
          $access = FALSE;
          break;
        }
      }
    }
    else {
      $plugin_ids = explode('+', $access_string);
      foreach ($plugin_ids as $plugin_id) {
        if ($group
          ->getGroupType()
          ->hasContentPlugin($plugin_id)) {
          $access = TRUE;
          break;
        }
      }
    }
    return AccessResult::allowedIf($access);
  }

}

Members