Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Implement new log options #130

Merged
merged 5 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [7.4, 8.1]
php: [7.4, 8.2]

name: PHP${{ matrix.php }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [8.1, 8.0, 7.4, 7.3]
php: [8.2, 8.1, 7.4, 7.3]

name: PHP${{ matrix.php }}

Expand Down
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpunit" version="^9.5.21" installed="9.5.21" location="./tools/phpunit" copy="true"/>
<phar name="php-cs-fixer" version="^3.0" installed="3.13.0" location="./tools/php-cs-fixer" copy="true"/>
<phar name="php-cs-fixer" version="^3.0" installed="3.52.1" location="./tools/php-cs-fixer" copy="true"/>
</phive>
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"require": {
"php": ">=7.2.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"apereo/phpcas": "^1.6"
"apereo/phpcas": "^1.6",
"monolog/monolog": "^2.0|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0|^10.0",
Expand Down
27 changes: 8 additions & 19 deletions src/Subfission/Cas/CasManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CasManager
/**
* @var LoggerInterface|null
*/
private $logger;
private $logger = null;

/**
* Proxy object for the phpCAS global
Expand Down Expand Up @@ -50,22 +50,16 @@ class CasManager
*/
public function __construct(
array $config,
LoggerInterface $logger = null,
PhpCasProxy $casProxy = null,
PhpSessionProxy $sessionProxy = null,
LogoutStrategy $logoutStrategy = null
) {
$this->logger = $logger;
$this->casProxy = $casProxy ?? new PhpCasProxy();
$this->sessionProxy = $sessionProxy ?? new PhpSessionProxy();
$this->logoutStrategy = $logoutStrategy ?? new LogoutStrategy();

$this->parseConfig($config);

if ($this->logger !== null) {
$this->casProxy->setLogger($this->logger);
}

$this->casProxy->setVerbose($this->config['cas_verbose_errors']);

// Fix for PHP 7.2. See http://php.net/manual/en/function.session-name.php
Expand Down Expand Up @@ -104,21 +98,17 @@ public function __construct(
}

/**
* Enable debug mode for CAS
* Sets a PSR-3 compatible logger, or null to disable logging
*
* @param $enabled
* @param LoggerInterface|null $logger
* @return void
*/
protected function enableDebugCas()
public function setLogger(?LoggerInterface $logger = null): void
{
try {
$this->casProxy->setDebug();
} catch (\Exception $e) {
// Fix for depreciation of setDebug
$this->casProxy->setLogger();
}
$this->casProxy->log('Loaded configuration:' . PHP_EOL
. serialize($this->config));
$this->logger = $logger;
$this->casProxy->setLogger($this->logger);
}

/**
* Configure CAS Client|Proxy
*
Expand Down Expand Up @@ -179,7 +169,6 @@ protected function parseConfig(array $config)
'cas_redirect_path' => '',
'cas_enable_saml' => true,
'cas_version' => "2.0",
'cas_debug' => false,
'cas_verbose_errors' => false,
'cas_masquerade' => '',
'cas_session_domain' => '',
Expand Down
9 changes: 8 additions & 1 deletion src/Subfission/Cas/CasServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ public function boot()
public function register()
{
$this->app->singleton('cas', function () {
return new CasManager(config('cas'), \Log::getLogger());
$cas = new CasManager(config('cas'));

/** @var LogFactory $logger */
$logger = resolve(LogFactory::class);

$cas->setLogger($logger->make());

return $cas;
});
}

Expand Down
31 changes: 31 additions & 0 deletions src/Subfission/Cas/LogFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Subfission\Cas;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;

class LogFactory
{
public function make(): ?LoggerInterface
{
$logType = config('cas.cas_log');

// Use the active Laravel logger given by the facade
if (strtolower($logType) === 'laravel') {
return \Log::getLogger();
}

// Assume the value is a path for a log file
// We are not responsible for file system errors at this point
if (!empty($logType)) {
$log = new Logger('phpCAS');
$log->pushHandler(new StreamHandler($logType));
return $log;
}

// Disable logging
return null;
}
}
13 changes: 7 additions & 6 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@

/*
|--------------------------------------------------------------------------
| Enable PHPCas Debug Mode
| Options are:
| 1) true (defaults logfile creation to /tmp/phpCAS.log)
| 2) 'path/to/logfile'
| 3) false
| Sets the log method for phpCAS. phpCAS logs are verbose, multi-line, INFO
| log entries. Consider the implications when choosing a log approach.
|
| null (default) = no logging
| laravel = use the Laravel MonoLog instance
| /path/to/file = create a new log at the given file path
|--------------------------------------------------------------------------
*/
'cas_debug' => env('CAS_DEBUG', false),
'cas_log' => env('CAS_LOG'),

/*
|--------------------------------------------------------------------------
Expand Down
14 changes: 2 additions & 12 deletions tests/CasManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ public function testDoesNotSetLoggerIfNotProvided(): void
$this->makeCasManager();
}

public function testSetsLoggerWhenLoggerIsProvided(): void
{
$logger = $this->createMock(LoggerInterface::class);

$this->casProxy->expects($this->once())->method('setLogger')
->with($this->equalTo($logger));

$this->makeCasManager([], $logger);
}

/**
* @dataProvider setVerboseChecks
*/
Expand Down Expand Up @@ -576,8 +566,8 @@ public function testCheckAuthenticationWhenMasquerading(): void
$this->assertTrue($manager->checkAuthentication());
}

private function makeCasManager(array $config = [], LoggerInterface $logger = null): CasManager
private function makeCasManager(array $config = []): CasManager
{
return new CasManager($config, $logger, $this->casProxy, $this->sessionProxy, $this->logoutStrategy);
return new CasManager($config, $this->casProxy, $this->sessionProxy, $this->logoutStrategy);
}
}
Loading