You are here

class ContentHubAccess in Acquia Content Hub 8.2

Same name and namespace in other branches
  1. 8 src/Access/ContentHubAccess.php \Drupal\acquia_contenthub\Access\ContentHubAccess

Implements permission to prevent unauthorized access to webhooks.

Hierarchy

Expanded class hierarchy of ContentHubAccess

1 string reference to 'ContentHubAccess'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses ContentHubAccess
access_check.acquia_contenthub.contenthub_access in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\Access\ContentHubAccess

File

src/Access/ContentHubAccess.php, line 15

Namespace

Drupal\acquia_contenthub\Access
View source
class ContentHubAccess implements AccessInterface {

  /**
   * Logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $loggerFactory;

  /**
   * Content Hub Client Manager.
   *
   * @var \Drupal\acquia_contenthub\Client\ClientFactory
   */
  protected $clientFactory;

  /**
   * Constructs a ContentHubAccess object.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory.
   * @param \Drupal\acquia_contenthub\Client\ClientFactory $client_factory
   *   The client factory.
   */
  public function __construct(LoggerChannelFactoryInterface $logger_factory, ClientFactory $client_factory) {
    $this->loggerFactory = $logger_factory;
    $this->clientFactory = $client_factory;
  }

  /**
   * Checks access to Entity CDF.
   *
   * Only grants access to logged in users with 'Administer Acquia Content Hub'
   * permission or if the request verifies its HMAC signature.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The HTTP request object.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   TRUE if granted access, FALSE otherwise.
   */
  public function access(Request $request, AccountInterface $account) {

    // Check permissions and combine that with any custom access checking
    // needed. Pass forward parameters from the route and/or request as needed.
    if ($account
      ->hasPermission('administer acquia content hub')) {

      // If this is a logged in user with 'Administer Acquia Content Hub'
      // permission then grant access.
      return AccessResult::allowed();
    }
    else {
      if (!$this->clientFactory
        ->getClient()) {
        $this->loggerFactory
          ->get('acquia_contenthub')
          ->debug('Access denied: Acquia Content Hub Client not connected.');
        return AccessResult::forbidden('Acquia Content Hub Client not connected.');
      }

      // Only allow access if the Signature validates.
      return AccessResult::allowedIf((bool) $this->clientFactory
        ->authenticate($request));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentHubAccess::$clientFactory protected property Content Hub Client Manager.
ContentHubAccess::$loggerFactory protected property Logger.
ContentHubAccess::access public function Checks access to Entity CDF.
ContentHubAccess::__construct public function Constructs a ContentHubAccess object.