Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Migrate some deprecated functions #516

Merged
merged 1 commit into from
Mar 14, 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
43 changes: 15 additions & 28 deletions lib/ComplianceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,30 @@
use OCA\Password_Policy\Compliance\IAuditor;
use OCA\Password_Policy\Compliance\IEntryControl;
use OCA\Password_Policy\Compliance\IUpdatable;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException;
use OCP\IConfig;
use OCP\ILogger;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class ComplianceService {
/** @var IAppContainer */
protected $container;
/** @var ILogger */
protected $logger;

protected const COMPLIANCERS = [
HistoryCompliance::class,
Expiration::class,
];
/** @var IUserSession */
private $userSession;
/** @var IConfig */
private $config;
/** @var ISession */
private $session;

public function __construct(
IAppContainer $container,
ILogger $logger,
IUserSession $userSession,
IConfig $config,
ISession $session
private ContainerInterface $container,
private LoggerInterface $logger,
private IUserSession $userSession,
private IConfig $config,
private ISession $session,
private IUserManager $userManager,
) {
$this->container = $container;
$this->logger = $logger;
$this->userSession = $userSession;
$this->config = $config;
$this->session = $session;
}

public function update(IUser $user, string $password) {
Expand All @@ -94,9 +81,9 @@ public function entryControl(string $loginName, ?string $password) {
/** @var IEntryControl $instance */
foreach ($this->getInstance(IEntryControl::class) as $instance) {
try {
$user = \OC::$server->getUserManager()->get($uid);
$user = $this->userManager->get($uid);

if (!($user instanceof IUser)) {
if ($user === null) {
break;
}

Expand All @@ -113,13 +100,13 @@ public function entryControl(string $loginName, ?string $password) {
protected function getInstance($interface) {
foreach (self::COMPLIANCERS as $compliance) {
try {
$instance = $this->container->query($compliance);
$instance = $this->container->get($compliance);
if (!$instance instanceof $interface) {
continue;
}
} catch (QueryException $e) {
} catch (ContainerExceptionInterface $e) {
//ignore and continue
$this->logger->logException($e, ['level' => ILogger::INFO]);
$this->logger->info('Could not query compliance', ['compliance' => $compliance, 'exception' => $e]);
continue;
}

Expand Down
24 changes: 10 additions & 14 deletions lib/PasswordValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@
use OCA\Password_Policy\Validator\NumericCharacterValidator;
use OCA\Password_Policy\Validator\SpecialCharactersValidator;
use OCA\Password_Policy\Validator\UpperCaseLoweCaseValidator;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException;
use OCP\ILogger;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class PasswordValidator {

/** @var IAppContainer */
private $container;
/** @var ILogger */
private $logger;

public function __construct(IAppContainer $container, ILogger $logger) {
$this->container = $container;
$this->logger = $logger;
public function __construct(
private ContainerInterface $container,
private LoggerInterface $logger,
) {
}

/**
Expand All @@ -68,10 +64,10 @@ public function validate(string $password): void {
foreach ($validators as $validator) {
try {
/** @var IValidator $instance */
$instance = $this->container->query($validator);
} catch (QueryException $e) {
$instance = $this->container->get($validator);
} catch (ContainerExceptionInterface $e) {
//ignore and continue
$this->logger->logException($e, ['level' => ILogger::INFO]);
$this->logger->info('Could not get validator from container', ['validator' => $validator, 'exception' => $e]);
continue;
}

Expand Down
22 changes: 7 additions & 15 deletions lib/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,23 @@
namespace OCA\Password_Policy;

use OCP\AppFramework\Http\TemplateResponse;
use OCP\IInitialStateService;
use OCP\AppFramework\Services\IInitialState;
use OCP\Settings\ISettings;
use OCP\Util;

class Settings implements ISettings {
private $appName;

/** @var PasswordPolicyConfig */
private $config;

/** @var IInitialStateService */
private $initialStateService;

public function __construct(string $appName,
PasswordPolicyConfig $config,
IInitialStateService $initialStateService) {
$this->appName = $appName;
$this->config = $config;
$this->initialStateService = $initialStateService;
public function __construct(
private string $appName,
private PasswordPolicyConfig $config,
private IInitialState $initialStateService,
) {
}

public function getForm(): TemplateResponse {
Util::addScript($this->appName, 'password_policy-settings');

$this->initialStateService->provideInitialState($this->appName, 'config', [
$this->initialStateService->provideInitialState('config', [
'minLength' => $this->config->getMinLength(),
'enforceNonCommonPassword' => $this->config->getEnforceNonCommonPassword(),
'enforceUpperLowerCase' => $this->config->getEnforceUpperLowerCase(),
Expand Down
18 changes: 9 additions & 9 deletions tests/lib/PasswordValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
use OCA\Password_Policy\Validator\NumericCharacterValidator;
use OCA\Password_Policy\Validator\SpecialCharactersValidator;
use OCA\Password_Policy\Validator\UpperCaseLoweCaseValidator;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\QueryException;
use OCP\ILogger;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

class PasswordValidatorTest extends TestCase {

/** @var IAppContainer|MockObject */
/** @var ContainerInterface|MockObject */
private $container;

/** @var ILogger|MockObject */
/** @var LoggerInterface|MockObject */
private $logger;

/** @var PasswordValidator */
Expand All @@ -50,8 +50,8 @@ class PasswordValidatorTest extends TestCase {
protected function setUp(): void {
parent::setUp();

$this->container = $this->createMock(IAppContainer::class);
$this->logger = $this->createMock(ILogger::class);
$this->container = $this->createMock(ContainerInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->validator = new PasswordValidator($this->container, $this->logger);
}
Expand All @@ -66,7 +66,7 @@ public function testValidate() {
HIBPValidator::class,
];

$this->container->method('query')
$this->container->method('get')
->willReturnCallback(function ($class) use (&$validators) {
if (($key = array_search($class, $validators)) !== false) {
$validator = $this->createMock(IValidator::class);
Expand All @@ -79,7 +79,7 @@ public function testValidate() {
return $validator;
}

throw new QueryException();
throw $this->createMock(ContainerExceptionInterface::class);
});

$this->logger->expects($this->never())->method($this->anything());
Expand Down
Loading