Skip to content

Commit 4a15477

Browse files
committed
add debugging tool files
1 parent fa2cd9c commit 4a15477

File tree

9 files changed

+292
-30
lines changed

9 files changed

+292
-30
lines changed

src/Controller/AccessDeniedController.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Debug/SamlauthDebugReactor.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Drupal\ubc_cwl_auth\Debug;
4+
5+
use Drupal\Core\Config\ConfigFactoryInterface;
6+
7+
/**
8+
* Reactor service — keep this light; push heavy work to queue/worker.
9+
*
10+
* Note: this class purposely does NOT inject logger.factory to avoid a
11+
* circular dependency with the logger.factory decorator. It obtains a logger
12+
* at runtime inside handleDebugMessage().
13+
*/
14+
class SamlauthDebugReactor {
15+
16+
protected ConfigFactoryInterface $configFactory;
17+
18+
public function __construct(ConfigFactoryInterface $config_factory) {
19+
$this->configFactory = $config_factory;
20+
}
21+
22+
/**
23+
* Handle a debug message from the samlauth channel.
24+
*
25+
* Keep this method light; if heavy work is required push to the queue.
26+
*
27+
* @param string $message
28+
* @param array $context
29+
*/
30+
public function handleDebugMessage(string $message, array $context = []) : void {
31+
// Obtain the logger at runtime to avoid container circular references.
32+
$logger = \Drupal::logger('ubc_cwl_auth');
33+
34+
if (strpos($message, 'SAML') !== FALSE || TRUE) {
35+
36+
$config = $this->configFactory->get('ubc_cwl_auth.settings');
37+
38+
if($config->get('ubc_cwl_auth_debug') == 1) {
39+
40+
$cid = time();
41+
\Drupal::cache('ubc_cwl_auth')->set($cid, $message, (time() + 24*60*60));
42+
}
43+
44+
}
45+
}
46+
}

src/EventSubscriber/AccessDeniedRedirectSubscriber.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Drupal\Core\Routing\UrlGeneratorInterface;
1111
use Symfony\Component\DependencyInjection\ContainerInterface;
1212
use Drupal\Core\Session\AccountInterface;
13+
use Drupal\Core\Url;
1314

1415
class AccessDeniedRedirectSubscriber implements EventSubscriberInterface {
1516

@@ -42,10 +43,16 @@ public function onKernelException(ExceptionEvent $event): void {
4243
return;
4344
}
4445

46+
4547
// Redirect other users to CWL Login
46-
//\Drupal::logger('UBC CWL')->debug('No CWL role... redirect');
47-
$url = $this->urlGenerator->generate('ubc_cwl_auth.ubc_cwl_redirect');
48-
$response = new RedirectResponse($url);
48+
$request = $event->getRequest();
49+
$source_page = $request->getRequestUri();
50+
51+
$redirect_url = Url::fromUri('internal:/saml/login', [
52+
'query' => ['destination' => $source_page],
53+
])->toString();
54+
55+
$response = new RedirectResponse($redirect_url);
4956
$event->setResponse($response);
5057
}
5158

src/Form/SettingsForm.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Drupal\ubc_cwl_auth\Form;
4+
5+
use Drupal\Core\Form\ConfigFormBase;
6+
use Drupal\Core\Form\FormStateInterface;
7+
8+
/**
9+
* Defines the settings form
10+
*/
11+
class SettingsForm extends ConfigFormBase {
12+
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function getEditableConfigNames() {
17+
return ['ubc_cwl_auth.settings'];
18+
}
19+
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function getFormId() {
24+
return 'ubc_cwl_auth_settings_form';
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function buildForm(array $form, FormStateInterface $form_state) {
31+
$config = $this->config('ubc_cwl_auth.settings');
32+
33+
$form['debug'] = [
34+
'#type' => 'checkbox',
35+
'#title' => $this->t('UBC CWL Debug Mode'),
36+
'#default_value' => $config->get('ubc_cwl_auth_debug'),
37+
'#description' => $this->t('Turn on debug mode'),
38+
'#return_value' => 1,
39+
];
40+
41+
$form['attributes'] = [
42+
'#markup' => $this->getDebugAttributes(),
43+
];
44+
45+
return parent::buildForm($form, $form_state);
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function submitForm(array &$form, FormStateInterface $form_state) {
52+
$this->config('ubc_cwl_auth.settings')
53+
->set('ubc_cwl_auth_debug', $form_state->getValue('debug'))
54+
->save();
55+
56+
\Drupal::logger('ubc_cwl_auth')->notice('UBC_CWL_AUTH Debug Setting: '.$form_state->getValue('debug'));
57+
58+
parent::submitForm($form, $form_state);
59+
}
60+
61+
private function getDebugAttributes() {
62+
63+
//\Drupal::logger('samlauth')->debug('Test SAML debug message from test');
64+
65+
return '<p>TODO: fetch data from Cache</p>';
66+
}
67+
68+
}

src/Logger/FactoryDecorator.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Drupal\ubc_cwl_auth\Logger;
4+
5+
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
6+
use Drupal\Core\Logger\LoggerChannelInterface;
7+
use Psr\Log\LoggerInterface;
8+
use Drupal\ubc_cwl_auth\Debug\SamlauthDebugReactor;
9+
10+
/**
11+
* Decorates the logger.factory service to intercept samlauth debug logs.
12+
*/
13+
class FactoryDecorator implements LoggerChannelFactoryInterface {
14+
15+
/**
16+
* The original logger factory.
17+
*
18+
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
19+
*/
20+
protected $innerFactory;
21+
22+
/**
23+
* The reactor service.
24+
*
25+
* @var \Drupal\ubc_cwl_auth\Debug\SamlauthDebugReactor
26+
*/
27+
protected $reactor;
28+
29+
/**
30+
* Constructs a new FactoryDecorator.
31+
*/
32+
public function __construct(LoggerChannelFactoryInterface $inner_factory, SamlauthDebugReactor $reactor) {
33+
$this->innerFactory = $inner_factory;
34+
$this->reactor = $reactor;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*
40+
* Note: no strict return type here to retain compatibility across Drupal versions.
41+
*/
42+
public function get($channel) {
43+
$channelLogger = $this->innerFactory->get($channel);
44+
45+
// Only wrap the samlauth channel.
46+
if ($channel === 'samlauth') {
47+
return new SamlauthChannelWrapper($channelLogger, $this->reactor);
48+
}
49+
50+
return $channelLogger;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*
56+
* Must match signature exactly (include $priority and no return type).
57+
*/
58+
public function addLogger(LoggerInterface $logger, $priority = 0) {
59+
$this->innerFactory->addLogger($logger, $priority);
60+
}
61+
62+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Drupal\ubc_cwl_auth\Logger;
4+
5+
use Drupal\Core\Logger\LoggerChannelInterface;
6+
use Drupal\ubc_cwl_auth\Debug\SamlauthDebugReactor;
7+
use Psr\Log\LoggerInterface;
8+
use Psr\Log\LogLevel;
9+
use Symfony\Component\HttpFoundation\RequestStack;
10+
use Drupal\Core\Session\AccountInterface;
11+
12+
/**
13+
* Wraps the samlauth logger channel so we can react to debug() messages.
14+
*/
15+
class SamlauthChannelWrapper implements LoggerChannelInterface {
16+
17+
protected LoggerChannelInterface $channel;
18+
protected SamlauthDebugReactor $reactor;
19+
20+
public function __construct(LoggerChannelInterface $channel, SamlauthDebugReactor $reactor) {
21+
$this->channel = $channel;
22+
$this->reactor = $reactor;
23+
}
24+
25+
// --- PSR-3 methods ---
26+
public function emergency(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::EMERGENCY, $message, $context); }
27+
public function alert(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::ALERT, $message, $context); }
28+
public function critical(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::CRITICAL, $message, $context); }
29+
public function error(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::ERROR, $message, $context); }
30+
public function warning(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::WARNING, $message, $context); }
31+
public function notice(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::NOTICE, $message, $context); }
32+
public function info(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::INFO, $message, $context); }
33+
public function debug(\Stringable|string $message, array $context = []): void { $this->log(LogLevel::DEBUG, $message, $context); }
34+
public function log($level, \Stringable|string $message, array $context = []): void {
35+
if ($level === LogLevel::DEBUG) {
36+
try {
37+
$this->reactor->handleDebugMessage((string) $message, $context);
38+
}
39+
catch (\Throwable $e) {
40+
$this->channel->error('ubc_cwl_auth reactor error: @msg', ['@msg' => $e->getMessage()]);
41+
}
42+
}
43+
$this->channel->log($level, $message, $context);
44+
}
45+
46+
// --- Drupal-specific methods ---
47+
public function setRequestStack(?RequestStack $requestStack = null): void {
48+
if (method_exists($this->channel, 'setRequestStack')) {
49+
$this->channel->setRequestStack($requestStack);
50+
}
51+
}
52+
53+
public function setCurrentUser(?AccountInterface $account = null): void {
54+
if (method_exists($this->channel, 'setCurrentUser')) {
55+
$this->channel->setCurrentUser($account);
56+
}
57+
}
58+
59+
public function setLoggers(array $loggers = []): void {
60+
if (method_exists($this->channel, 'setLoggers')) {
61+
$this->channel->setLoggers($loggers);
62+
}
63+
}
64+
65+
public function addLogger(LoggerInterface $logger, $priority = 0): void {
66+
if (method_exists($this->channel, 'addLogger')) {
67+
$this->channel->addLogger($logger, $priority);
68+
}
69+
}
70+
71+
public function getName(): string {
72+
if (method_exists($this->channel, 'getName')) {
73+
return $this->channel->getName();
74+
}
75+
return '';
76+
}
77+
}

ubc_cwl_auth.links.menu.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ubc_cwl_auth.settings:
2+
title: 'UBC CWL Auth Settings'
3+
description: 'Configure settings for UBC CWL Auth.'
4+
route_name: ubc_cwl_auth.settings
5+
parent: system.admin_config_system

ubc_cwl_auth.routing.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
ubc_cwl_auth.ubc_cwl_redirect:
2-
path: '/custom-403'
1+
ubc_cwl_auth.settings:
2+
path: '/admin/config/system/ubc_cwl_auth'
33
defaults:
4-
_controller: '\Drupal\ubc_cwl_auth\Controller\AccessDeniedController::redirectToCwlLogin'
5-
_title: 'Redirecting...'
4+
_form: '\Drupal\ubc_cwl_auth\Form\SettingsForm'
5+
_title: 'UBC CWL Auth Settings'
66
requirements:
7-
_permission: 'access content'
7+
_permission: 'administer site configuration'

ubc_cwl_auth.services.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,22 @@ services:
66
- '@current_user'
77
tags:
88
- { name: event_subscriber }
9+
10+
ubc_cwl_auth.logger_factory_decorator:
11+
class: Drupal\ubc_cwl_auth\Logger\FactoryDecorator
12+
decorates: 'logger.factory'
13+
arguments: ['@ubc_cwl_auth.logger_factory_decorator.inner', '@ubc_cwl_auth.samlauth_debug_reactor']
14+
public: false
15+
16+
ubc_cwl_auth.samlauth_debug_reactor:
17+
class: Drupal\ubc_cwl_auth\Debug\SamlauthDebugReactor
18+
arguments:
19+
- '@config.factory'
20+
public: false
21+
22+
cache.ubc_cwl_auth:
23+
class: Drupal\Core\Cache\CacheBackendInterface
24+
tags:
25+
- { name: cache.bin }
26+
factory: cache_factory:get
27+
arguments: ['ubc_cwl_auth']

0 commit comments

Comments
 (0)