Passed
Push — master ( 25a926...39145a )
by
unknown
20:30
created

MfaProviderPass::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Core\DependencyInjection;
19
20
use Psr\Container\ContainerInterface;
21
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TYPO3\CMS\Core\Dependenc...ection\ContainerBuilder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
use Symfony\Component\DependencyInjection\Definition;
24
use Symfony\Component\DependencyInjection\Reference;
25
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifest;
26
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry;
27
use TYPO3\CMS\Core\Service\DependencyOrderingService;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Compiler pass to register tagged MFA providers
32
 *
33
 * @internal
34
 */
35
final class MfaProviderPass implements CompilerPassInterface
36
{
37
    protected string $tagName;
38
39
    public function __construct(string $tagName)
40
    {
41
        $this->tagName = $tagName;
42
    }
43
44
    public function process(ContainerBuilder $container): void
45
    {
46
        $mfaProviderRegistryDefinition = $container->findDefinition(MfaProviderRegistry::class);
47
        $providers = [];
48
49
        foreach ($container->findTaggedServiceIds($this->tagName) as $id => $tags) {
50
            $definition = $container->findDefinition($id);
51
            if (!$definition->isAutoconfigured() || $definition->isAbstract()) {
52
                continue;
53
            }
54
55
            $definition->setPublic(true);
56
57
            foreach ($tags as $attributes) {
58
                $identifier = $attributes['identifier'] ?? $id;
59
                $providers[$identifier] = [
60
                    'title' => $attributes['title'] ?? '',
61
                    'description' => $attributes['description'] ?? '',
62
                    'setupInstructions' => $attributes['setupInstructions'] ?? '',
63
                    'iconIdentifier' => $attributes['icon'] ?? '',
64
                    'isDefaultProviderAllowed' => (bool)($attributes['defaultProviderAllowed'] ?? true),
65
                    'before' => GeneralUtility::trimExplode(',', $attributes['before'] ?? '', true),
66
                    'after'  => GeneralUtility::trimExplode(',', $attributes['after'] ?? '', true),
67
                    'serviceName' => $id
68
                ];
69
            }
70
        }
71
72
        foreach ((new DependencyOrderingService())->orderByDependencies($providers) as $identifier => $properties) {
73
            $manifest = new Definition(MfaProviderManifest::class);
74
            $manifest->setArguments([
75
                $identifier,
76
                $properties['title'],
77
                $properties['description'],
78
                $properties['setupInstructions'],
79
                $properties['iconIdentifier'],
80
                $properties['isDefaultProviderAllowed'],
81
                $properties['serviceName'],
82
                new Reference(ContainerInterface::class)
83
            ]);
84
            $manifest->setShared(false);
85
86
            $mfaProviderRegistryDefinition->addMethodCall('registerProvider', [$manifest]);
87
        }
88
    }
89
}
90