|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\Authentication\DI; |
|
6
|
|
|
|
|
7
|
|
|
use Kdyby; |
|
8
|
|
|
use Nette; |
|
9
|
|
|
use SixtyEightPublishers; |
|
10
|
|
|
|
|
11
|
|
|
final class AuthenticationExtensionAdapter extends SixtyEightPublishers\User\DI\AbstractExtensionAdapter implements |
|
12
|
|
|
Kdyby\Doctrine\DI\ITargetEntityProvider, |
|
13
|
|
|
Kdyby\Translation\DI\ITranslationProvider |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
protected static $defaults = [ |
|
17
|
|
|
'enabled' => FALSE, |
|
18
|
|
|
'authenticator' => SixtyEightPublishers\User\Authentication\Authenticator\Authenticator::class, |
|
19
|
|
|
'csrf_token_factory' => SixtyEightPublishers\User\Authentication\Csrf\CsrfTokenFactory::class, |
|
20
|
|
|
'register_controls' => FALSE, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** @var NULL|string */ |
|
24
|
|
|
private $authenticatorName; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function processConfig(array $config, \ArrayObject $sharedData): array |
|
30
|
|
|
{ |
|
31
|
|
|
Nette\Utils\Validators::assertField($config, 'enabled', 'bool'); |
|
32
|
|
|
Nette\Utils\Validators::assertField($config, 'authenticator', 'string|' . Nette\DI\Statement::class); |
|
33
|
|
|
Nette\Utils\Validators::assertField($config, 'csrf_token_factory', 'string|' . Nette\DI\Statement::class); |
|
34
|
|
|
Nette\Utils\Validators::assertField($config, 'register_controls', 'bool'); |
|
35
|
|
|
|
|
36
|
|
|
if (FALSE === $config['enabled']) { |
|
37
|
|
|
$this->stopPropagation(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
View Code Duplication |
if (!is_subclass_of( |
|
|
|
|
|
|
41
|
|
|
$sharedData[SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::SHARED_DATA_USER_CLASS_NAME], |
|
42
|
|
|
SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser::class, |
|
43
|
|
|
TRUE |
|
44
|
|
|
) |
|
45
|
|
|
) { |
|
46
|
|
|
throw new SixtyEightPublishers\User\Common\Exception\ConfigurationException(sprintf( |
|
47
|
|
|
'Your User entity must implement interface %s', |
|
48
|
|
|
SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser::class |
|
49
|
|
|
)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $config; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function loadConfiguration(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$config = $this->getConfig(); |
|
61
|
|
|
$builder = $this->getContainerBuilder(); |
|
62
|
|
|
$authenticator = $config['authenticator']; |
|
63
|
|
|
$csrfTokenFactory = $config['csrf_token_factory']; |
|
64
|
|
|
|
|
65
|
|
|
# authenticator |
|
66
|
|
|
if (!is_string($authenticator) || !Nette\Utils\Strings::startsWith($authenticator, '@')) { |
|
67
|
|
|
$builder->addDefinition($this->prefix('authenticator')) |
|
68
|
|
|
->setType(Nette\Security\IAuthenticator::class) |
|
69
|
|
|
->setFactory($authenticator); |
|
70
|
|
|
|
|
71
|
|
|
$this->authenticatorName = $this->prefix('authenticator'); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->authenticatorName = Nette\Utils\Strings::substring($authenticator, 1); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (isset($builder->getAliases()['nette.authenticator'])) { |
|
77
|
|
|
$builder->removeAlias('nette.authenticator'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$builder->addAlias('nette.authenticator', $this->authenticatorName); |
|
81
|
|
|
|
|
82
|
|
|
# CSRF |
|
83
|
|
|
if (!is_string($csrfTokenFactory) || !Nette\Utils\Strings::startsWith($csrfTokenFactory, '@')) { |
|
84
|
|
|
$builder->addDefinition($this->prefix('csrf_token_factory')) |
|
85
|
|
|
->setType(SixtyEightPublishers\User\Authentication\Csrf\ICsrfTokenFactory::class) |
|
86
|
|
|
->setFactory($csrfTokenFactory); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
# controls |
|
90
|
|
|
if (TRUE === $config['register_controls']) { |
|
91
|
|
|
$builder->addDefinition($this->prefix('control.sign_in')) |
|
92
|
|
|
->setImplement(SixtyEightPublishers\User\Authentication\Control\SignIn\ISignInControlFactory::class) |
|
93
|
|
|
->setFactory(SixtyEightPublishers\User\Authentication\Control\SignIn\SignInControl::class); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
# queries |
|
97
|
|
|
$builder->addDefinition($this->prefix('query_factory.authenticator')) |
|
98
|
|
|
->setType(SixtyEightPublishers\User\Authentication\Query\IAuthenticatorQueryFactory::class) |
|
99
|
|
|
->setFactory(SixtyEightPublishers\User\Authentication\Query\AuthenticatorQueryFactory::class); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/**************** interface \Kdyby\Doctrine\DI\ITargetEntityProvider ****************/ |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getTargetEntityMappings(): array |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser::class => $this->getSharedData(SixtyEightPublishers\User\Common\DI\CommonExtensionAdapter::SHARED_DATA_USER_CLASS_NAME), |
|
111
|
|
|
]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/**************** interface \Kdyby\Translation\DI\ITranslationProvider ****************/ |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function getTranslationResources(): array |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$config = $this->getConfig(); |
|
122
|
|
|
|
|
123
|
|
|
return TRUE === $config['register_controls'] |
|
124
|
|
|
? [ __DIR__ . '/../locale' ] |
|
125
|
|
|
: []; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.