Skip to content

Commit

Permalink
Merge pull request #1 from ASUWebPlatforms/WS2-1205
Browse files Browse the repository at this point in the history
WS2-1205: Add webspark_cas module.
  • Loading branch information
mlsamuelson authored Jan 31, 2022
2 parents 5c685f1 + 571d77b commit 624a54c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# WebSpark CAS module

The WebSpark CAS module contains functionality which allows the Elastic Crawler to bypass the CAS redirect and process the web pages successfully.

## Setup

The following setting should be added to the `settings.php` file. If no such setting exists, the default setting will be used.

```php
$settings['webspark_cas_elastic_crawler_regex'] = '/^Elastic-Crawler .*$/';
```

This setting describes the RegExp used to determine the `Elastic Crawler` request based on its `User-Agent` HTTP Header value.
52 changes: 52 additions & 0 deletions src/EventSubscriber/WebSparkCasSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Drupal\webspark_cas\EventSubscriber;

use Drupal\cas\Subscriber\CasSubscriber;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Drupal\Core\Site\Settings;

/**
* WebSpark CAS event subscriber.
*/
class WebSparkCasSubscriber extends CasSubscriber {

/**
* {@inheritdoc}
*/
public function handle(GetResponseEvent $event) {
if ($this->isElasticCrawlerRequest()) {
return;
}

return parent::handle($event);
}

/**
* Checks if it is Elastic Crawler request.
*
* @return bool
* The check result.
*/
protected function isElasticCrawlerRequest(): bool {
$current_request = $this->requestStack->getCurrentRequest();

$defaultPattern = '/^Elastic-Crawler .*$/';

// Get the regex from $settings if available.
$elasticPattern = Settings::get('webspark_cas_elastic_crawler_regex', $defaultPattern);

$agent = $current_request->server->get('HTTP_USER_AGENT');
if (empty($agent)) {
return FALSE;
}

if (\preg_match($elasticPattern, $agent)) {
// Allow the Elastic crawler.
return TRUE;
}

return FALSE;
}

}
8 changes: 8 additions & 0 deletions webspark_cas.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: WebSpark CAS
type: module
description: WebSpark CAS
package: WebSpark
core: 8.x
core_version_requirement: ^8 || ^9
dependencies:
- cas:cas
6 changes: 6 additions & 0 deletions webspark_cas.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
cas.subscriber:
class: Drupal\webspark_cas\EventSubscriber\WebSparkCasSubscriber
arguments: ['@request_stack', '@current_route_match', '@config.factory', '@current_user', '@plugin.manager.condition', '@cas.helper', '@cas.redirector']
tags:
- { name: event_subscriber }

0 comments on commit 624a54c

Please sign in to comment.