From d8cf16b5c0683751b6b7190b95819e20bb4f28d2 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 19 Aug 2024 14:58:32 +0200 Subject: [PATCH] refactor: Migrate away from deprecated `ILogger` to PSR-3 Mostly replace `ILogger` with `LoggerInterface` and some minor cleanup (constructor property promotion). Some places used the deprecated `logException`, this is easy to migrate: Simply use the appropriate log level on the logger and place the exception under the `exception` key in the context. Signed-off-by: Ferdinand Thiessen --- lib/Compliance/HistoryCompliance.php | 35 ++++--------------- lib/Validator/HIBPValidator.php | 27 +++++--------- .../lib/Compliance/HistoryComplianceTest.php | 26 +++++++------- 3 files changed, 28 insertions(+), 60 deletions(-) diff --git a/lib/Compliance/HistoryCompliance.php b/lib/Compliance/HistoryCompliance.php index ab56cb44..787ec993 100644 --- a/lib/Compliance/HistoryCompliance.php +++ b/lib/Compliance/HistoryCompliance.php @@ -12,43 +12,22 @@ use OCA\Password_Policy\PasswordPolicyConfig; use OCP\IConfig; use OCP\IL10N; -use OCP\ILogger; use OCP\IUser; use OCP\IUserSession; use OCP\PreConditionNotMetException; use OCP\Security\IHasher; +use Psr\Log\LoggerInterface; class HistoryCompliance implements IAuditor, IUpdatable { - /** @var string */ - protected $uid; - - /** @var PasswordPolicyConfig */ - private $policyConfig; - /** @var IConfig */ - private $config; - /** @var IUserSession */ - private $session; - /** @var IHasher */ - private $hasher; - /** @var IL10N */ - private $l; - /** @var ILogger */ - private $logger; public function __construct( - PasswordPolicyConfig $policyConfig, - IConfig $config, - IUserSession $session, - IHasher $hasher, - IL10N $l, - ILogger $logger + protected PasswordPolicyConfig $policyConfig, + protected IConfig $config, + protected IUserSession $session, + protected IHasher $hasher, + protected IL10N $l, + protected LoggerInterface $logger ) { - $this->policyConfig = $policyConfig; - $this->config = $config; - $this->session = $session; - $this->hasher = $hasher; - $this->l = $l; - $this->logger = $logger; } /** diff --git a/lib/Validator/HIBPValidator.php b/lib/Validator/HIBPValidator.php index 473371eb..f6c8c62e 100644 --- a/lib/Validator/HIBPValidator.php +++ b/lib/Validator/HIBPValidator.php @@ -12,27 +12,16 @@ use OCA\Password_Policy\PasswordPolicyConfig; use OCP\Http\Client\IClientService; use OCP\IL10N; -use OCP\ILogger; +use Psr\Log\LoggerInterface; class HIBPValidator implements IValidator { - /** @var PasswordPolicyConfig */ - private $config; - /** @var IL10N */ - private $l; - /** @var IClientService */ - private $clientService; - /** @var ILogger */ - private $logger; - - public function __construct(PasswordPolicyConfig $config, - IL10N $l, - IClientService $clientService, - ILogger $logger) { - $this->config = $config; - $this->l = $l; - $this->clientService = $clientService; - $this->logger = $logger; + public function __construct( + private PasswordPolicyConfig $config, + private IL10N $l, + private IClientService $clientService, + private LoggerInterface $logger, + ) { } public function validate(string $password): void { @@ -54,7 +43,7 @@ public function validate(string $password): void { ] ); } catch (\Exception $e) { - $this->logger->logException($e, ['level' => ILogger::INFO]); + $this->logger->info('Could not connect to HaveIBeenPwned API', ['exception' => $e]); return; } diff --git a/tests/lib/Compliance/HistoryComplianceTest.php b/tests/lib/Compliance/HistoryComplianceTest.php index 916fae38..d8e8fad0 100644 --- a/tests/lib/Compliance/HistoryComplianceTest.php +++ b/tests/lib/Compliance/HistoryComplianceTest.php @@ -31,24 +31,19 @@ use OCA\Password_Policy\PasswordPolicyConfig; use OCP\IConfig; use OCP\IL10N; -use OCP\ILogger; use OCP\IUser; use OCP\IUserSession; use OCP\Security\IHasher; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; class HistoryComplianceTest extends TestCase { - /** @var HistoryCompliance */ - protected $instance; - /** @var PasswordPolicyConfig|MockObject */ - protected $policyConfig; - /** @var IConfig|MockObject */ - protected $config; - /** @var IUserSession|MockObject */ - protected $session; - /** @var IHasher|MockObject */ - protected $hasher; + protected HistoryCompliance $instance; + protected PasswordPolicyConfig&MockObject $policyConfig; + protected IConfig&MockObject $config; + protected IUserSession&MockObject $session; + protected IHasher&MockObject $hasher; public function setUp(): void { parent::setUp(); @@ -58,13 +53,18 @@ public function setUp(): void { $this->session = $this->createMock(IUserSession::class); $this->hasher = $this->createMock(IHasher::class); + /** @var IL10N&MockObject */ + $l10n = $this->createMock(IL10N::class); + /** @var LoggerInterface&MockObject */ + $logger = $this->createMock(LoggerInterface::class); + $this->instance = new HistoryCompliance( $this->policyConfig, $this->config, $this->session, $this->hasher, - $this->createMock(IL10N::class), - $this->createMock(ILogger::class) + $l10n, + $logger, ); }