Skip to content

Commit 2f60dd3

Browse files
committed
Pass repo settings directly to the hasher
1 parent c3af037 commit 2f60dd3

File tree

6 files changed

+45
-43
lines changed

6 files changed

+45
-43
lines changed

src/bundle/Core/DependencyInjection/Configuration/Parser/Repository/PasswordHash.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
namespace Ibexa\Bundle\Core\DependencyInjection\Configuration\Parser\Repository;
1010

11-
use Ibexa\Bundle\Core\DependencyInjection\Configuration\AbstractParser;
1211
use Ibexa\Bundle\Core\DependencyInjection\Configuration\RepositoryConfigParserInterface;
13-
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface;
1412
use Ibexa\Contracts\Core\Repository\Values\User\User;
1513
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
1614

@@ -29,7 +27,7 @@
2927
* update_type_on_change: false
3028
* ```
3129
*/
32-
final class PasswordHash extends AbstractParser implements RepositoryConfigParserInterface
30+
final class PasswordHash implements RepositoryConfigParserInterface
3331
{
3432
public function addSemanticConfig(NodeBuilder $nodeBuilder): void
3533
{
@@ -40,6 +38,7 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder): void
4038
->integerNode('default_type')
4139
->info('Default password hash type, see the constants in Ibexa\Contracts\Core\Repository\Values\User\User.')
4240
->example('!php/const:Ibexa\Contracts\Core\Repository\Values\User\User::PASSWORD_HASH_PHP_DEFAULT')
41+
->defaultValue(User::PASSWORD_HASH_PHP_DEFAULT)
4342
->validate()
4443
->ifTrue(static function ($value): bool {
4544
$hashType = (int) $value;
@@ -58,26 +57,9 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder): void
5857
->booleanNode('update_type_on_change')
5958
->info('Whether the password hash type should be changed when the password is changed if it differs from the default type.')
6059
->example('false')
60+
->defaultFalse()
6161
->end()
6262
->end()
6363
->end();
6464
}
65-
66-
/**
67-
* @param array<string, mixed> $scopeSettings
68-
*/
69-
public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer): void
70-
{
71-
if (!isset($scopeSettings['password_hash'])) {
72-
return;
73-
}
74-
75-
$settings = $scopeSettings['password_hash'];
76-
if (isset($settings['default_type'])) {
77-
$contextualizer->setContextualParameter('password_hash.default_type', $currentScope, $settings['default_type']);
78-
}
79-
if (isset($settings['update_type_on_change'])) {
80-
$contextualizer->setContextualParameter('password_hash.update_type_on_change', $currentScope, $settings['update_type_on_change']);
81-
}
82-
}
8365
}

src/contracts/Repository/PasswordHashService.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010

1111
interface PasswordHashService
1212
{
13+
/**
14+
* Sets the default password hash type.
15+
*
16+
* @param int $defaultHashType The default password hash type, one of Ibexa\Contracts\Core\Repository\Values\User\User::SUPPORTED_PASSWORD_HASHES.
17+
*/
18+
public function setDefaultHashType(int $defaultHashType): void;
19+
20+
/**
21+
* Sets whether the password hash type should be updated when the password is changed.
22+
*
23+
* @param bool $updateTypeOnChange Whether to update the password hash type on change.
24+
*/
25+
public function setUpdateTypeOnChange(bool $updateTypeOnChange): void;
26+
1327
/**
1428
* Returns default password hash type.
1529
*

src/lib/Base/Container/ApiLoader/RepositoryFactory.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Ibexa\Contracts\Core\Repository\PermissionService;
1919
use Ibexa\Contracts\Core\Repository\Repository;
2020
use Ibexa\Contracts\Core\Repository\Validator\ContentValidator;
21+
use Ibexa\Contracts\Core\Repository\Values\User\User;
2122
use Ibexa\Contracts\Core\Search\Handler as SearchHandler;
2223
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
2324
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
@@ -98,6 +99,11 @@ public function buildRepository(
9899
): Repository {
99100
$config = $this->repositoryConfigurationProvider->getRepositoryConfig();
100101

102+
if (isset($config['password_hash'])) {
103+
$passwordHashService->setDefaultHashType($config['password_hash']['default_type'] ?? User::PASSWORD_HASH_PHP_DEFAULT);
104+
$passwordHashService->setUpdateTypeOnChange($config['password_hash']['update_type_on_change'] ?? false);
105+
}
106+
101107
return new CoreRepository(
102108
$persistenceHandler,
103109
$searchHandler,

src/lib/Repository/User/PasswordHashService.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Ibexa\Core\Repository\User;
1010

11-
use Ibexa\Contracts\Core\Container\ApiLoader\RepositoryConfigurationProviderInterface;
1211
use Ibexa\Contracts\Core\Repository\PasswordHashService as PasswordHashServiceInterface;
1312
use Ibexa\Contracts\Core\Repository\Values\User\User;
1413
use Ibexa\Core\Repository\User\Exception\PasswordHashTypeNotCompiled;
@@ -19,11 +18,26 @@
1918
*/
2019
final class PasswordHashService implements PasswordHashServiceInterface
2120
{
22-
private RepositoryConfigurationProviderInterface $repositoryConfigurationProvider;
21+
private int $defaultHashType;
2322

24-
public function __construct(RepositoryConfigurationProviderInterface $repositoryConfigurationProvider)
23+
private bool $updateTypeOnChange;
24+
25+
public function __construct(
26+
int $defaultHashType = User::PASSWORD_HASH_PHP_DEFAULT,
27+
bool $updateTypeOnChange = false
28+
) {
29+
$this->defaultHashType = $defaultHashType;
30+
$this->updateTypeOnChange = $updateTypeOnChange;
31+
}
32+
33+
public function setDefaultHashType(int $defaultHashType): void
34+
{
35+
$this->defaultHashType = $defaultHashType;
36+
}
37+
38+
public function setUpdateTypeOnChange(bool $updateTypeOnChange): void
2539
{
26-
$this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
40+
$this->updateTypeOnChange = $updateTypeOnChange;
2741
}
2842

2943
public function getSupportedHashTypes(): array
@@ -38,9 +52,7 @@ public function isHashTypeSupported(int $hashType): bool
3852

3953
public function getDefaultHashType(): int
4054
{
41-
$config = $this->repositoryConfigurationProvider->getRepositoryConfig();
42-
43-
return $config['password_hash']['default_type'];
55+
return $this->defaultHashType;
4456
}
4557

4658
public function createPasswordHash(
@@ -107,8 +119,6 @@ public function isValidPassword(
107119

108120
public function shouldPasswordHashTypeBeUpdatedOnChange(): bool
109121
{
110-
$config = $this->repositoryConfigurationProvider->getRepositoryConfig();
111-
112-
return $config['password_hash']['update_type_on_change'];
122+
return $this->updateTypeOnChange;
113123
}
114124
}

src/lib/Resources/settings/fieldtype_services.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ services:
6060

6161
Ibexa\Core\FieldType\FieldTypeRegistry: ~
6262

63-
Ibexa\Core\Repository\User\PasswordHashService:
64-
arguments:
65-
$repositoryConfigurationProvider: '@Ibexa\Core\Base\Container\ApiLoader\RepositoryConfigurationProvider'
63+
Ibexa\Core\Repository\User\PasswordHashService: ~
6664

6765
Ibexa\Contracts\Core\Repository\PasswordHashService:
6866
alias: Ibexa\Core\Repository\User\PasswordHashService

tests/lib/Repository/User/PasswordHashServiceTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
namespace Ibexa\Tests\Core\Repository\User;
1010

1111
use Ibexa\Contracts\Core\Repository\Values\User\User;
12-
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
13-
use Ibexa\Core\Base\Container\ApiLoader\RepositoryConfigurationProvider;
1412
use Ibexa\Core\Repository\User\Exception\UnsupportedPasswordHashType;
1513
use Ibexa\Core\Repository\User\PasswordHashService;
1614
use Ibexa\Tests\Bundle\Core\ApiLoader\BaseRepositoryConfigurationProviderTestCase;
@@ -23,13 +21,7 @@ final class PasswordHashServiceTest extends BaseRepositoryConfigurationProviderT
2321

2422
protected function setUp(): void
2523
{
26-
$repositories = [
27-
'legacy' => $this->buildNormalizedSingleRepositoryConfig('legacy'),
28-
];
29-
30-
$configResolver = $this->createMock(ConfigResolverInterface::class);
31-
$repositoryConfigurationProvider = new RepositoryConfigurationProvider($configResolver, $repositories);
32-
$this->passwordHashService = new PasswordHashService($repositoryConfigurationProvider);
24+
$this->passwordHashService = new PasswordHashService();
3325
}
3426

3527
public function testGetSupportedHashTypes(): void

0 commit comments

Comments
 (0)