|
3 | 3 | namespace Contributte\Http\DI; |
4 | 4 |
|
5 | 5 | use Contributte\Http\Auth\BasicAuthenticator; |
6 | | -use LogicException; |
7 | 6 | use Nette\DI\CompilerExtension; |
8 | 7 | use Nette\Http\IRequest; |
9 | 8 | use Nette\Http\IResponse; |
10 | 9 | use Nette\PhpGenerator\ClassType; |
11 | | -use Nette\Utils\Validators; |
| 10 | +use Nette\Schema\Expect; |
| 11 | +use Nette\Schema\Schema; |
| 12 | +use stdClass; |
12 | 13 |
|
| 14 | +/** |
| 15 | + * @property-read stdClass $config |
| 16 | + */ |
13 | 17 | class BasicAuthExtension extends CompilerExtension |
14 | 18 | { |
15 | 19 |
|
16 | | - /** @var mixed[] */ |
17 | | - private $defaults = [ |
18 | | - 'enabled' => false, |
19 | | - 'title' => 'Restrict zone', |
20 | | - 'users' => [], |
21 | | - ]; |
| 20 | + public function getConfigSchema(): Schema |
| 21 | + { |
| 22 | + return Expect::structure([ |
| 23 | + 'enabled' => Expect::bool(false), |
| 24 | + 'title' => Expect::string('Restricted zone'), |
| 25 | + 'users' => Expect::arrayOf(Expect::structure([ |
| 26 | + 'password' => Expect::string()->required(), |
| 27 | + 'unsecured' => Expect::bool(false), |
| 28 | + ]))->min(1), |
| 29 | + ]); |
| 30 | + } |
22 | 31 |
|
23 | | - /** |
24 | | - * Register services |
25 | | - */ |
26 | 32 | public function loadConfiguration(): void |
27 | 33 | { |
28 | 34 | $builder = $this->getContainerBuilder(); |
29 | | - $config = $this->validateConfig($this->defaults); |
30 | | - |
31 | | - Validators::assertField($config, 'enabled', 'bool'); |
32 | | - Validators::assertField($config, 'users', 'array'); |
| 35 | + $config = $this->config; |
33 | 36 |
|
34 | 37 | // Skip if its disabled |
35 | | - if ($config['enabled'] !== true) return; |
36 | | - |
37 | | - // Throws if there's no user |
38 | | - if ($config['users'] === []) throw new LogicException('You have to define any user or disable extension'); |
| 38 | + if (!$config->enabled) { |
| 39 | + return; |
| 40 | + } |
39 | 41 |
|
40 | 42 | $def = $builder->addDefinition($this->prefix('authenticator')) |
41 | 43 | ->setType(BasicAuthenticator::class) |
42 | | - ->setArguments([$config['title']]); |
| 44 | + ->setArguments([$config->title]); |
43 | 45 |
|
44 | | - foreach ($config['users'] as $user => $values) { |
45 | | - if (is_string($values)) { |
46 | | - trigger_error('Usage of `$username => $password` is deprecated, use `$username => ["password" => $password]` instead', E_USER_DEPRECATED); |
47 | | - $password = $values; |
48 | | - $unsecured = true; |
49 | | - } else { |
50 | | - $password = $values['password']; |
51 | | - $unsecured = $values['unsecured'] ?? false; |
52 | | - } |
53 | | - $def->addSetup('addUser', [$user, $password, $unsecured]); |
| 46 | + foreach ($config->users as $user => $values) { |
| 47 | + $def->addSetup('addUser', [$user, $values->password, $values->unsecured]); |
54 | 48 | } |
55 | 49 | } |
56 | 50 |
|
57 | | - /** |
58 | | - * Decorate initialize |
59 | | - */ |
60 | 51 | public function afterCompile(ClassType $class): void |
61 | 52 | { |
62 | | - $config = $this->validateConfig($this->defaults); |
| 53 | + $config = $this->config; |
63 | 54 |
|
64 | | - // Skip if its disabled or no user defined |
65 | | - if ($config['enabled'] !== true || $config['users'] === []) return; |
| 55 | + // Skip if its disabled |
| 56 | + if (!$config->enabled) { |
| 57 | + return; |
| 58 | + } |
66 | 59 |
|
67 | 60 | $initialize = $class->methods['initialize']; |
68 | 61 | $initialize->addBody('$this->getService(?)->authenticate($this->getByType(?), $this->getByType(?));', [ |
|
0 commit comments