1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace KleijnWeb\SwaggerBundle\DependencyInjection; |
9
|
|
|
|
10
|
|
|
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface; |
11
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
12
|
|
|
use Symfony\Component\Config\FileLocator; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author John Kleijn <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class SwaggerRequestAuthorizationFactory implements SecurityFactoryInterface |
21
|
|
|
{ |
22
|
|
|
public function getPosition() |
23
|
|
|
{ |
24
|
|
|
return 'remember_me'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getKey() |
28
|
|
|
{ |
29
|
|
|
return 'swagger'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function addConfiguration(NodeDefinition $node) |
33
|
|
|
{ |
34
|
|
|
$node |
|
|
|
|
35
|
|
|
->children() |
36
|
|
|
->booleanNode('rbac')->defaultFalse()->end() |
37
|
|
|
->end(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint) |
41
|
|
|
{ |
42
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
43
|
|
|
|
44
|
|
|
$loader->load('security/request_voting.yml'); |
45
|
|
|
|
46
|
|
|
if (isset($config['rbac']) && $config['rbac']) { |
47
|
|
|
$loader->load('security/rbac.yml'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$listenerId = 'swagger.security.listener.request_authorization'; |
51
|
|
|
|
52
|
|
|
return ['swagger.security.provider.noop', $listenerId, null]; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: