1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OsLabSecurityApiBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) OsLab <https://github.com/OsLab> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OsLab\SecurityApiBundle\DependencyInjection\Security\UserProvider; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface; |
16
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
17
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* InMemoryApiFactory creates services for the memory api provider. |
22
|
|
|
* |
23
|
|
|
* @author Michael COULLERET <[email protected]> |
24
|
|
|
* @author Florent DESPIERRES <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class InMemoryApiFactory implements UserProviderFactoryInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Creates the userProvider. |
30
|
|
|
* |
31
|
|
|
* @param ContainerBuilder $container Instance of container. |
32
|
|
|
* @param string $id The service id for concrete user provider. |
33
|
|
|
* @param array $config Configuration of current factory. |
34
|
|
|
*/ |
35
|
|
|
public function create(ContainerBuilder $container, $id, $config) |
36
|
|
|
{ |
37
|
|
|
$definition = $container->setDefinition($id, new DefinitionDecorator('oslab_security_api.security.user.provider')); |
38
|
|
|
|
39
|
|
|
foreach ($config['users'] as $username => $user) { |
40
|
|
|
$userId = $id.'_'.$username; |
41
|
|
|
|
42
|
|
|
$container |
43
|
|
|
->setDefinition($userId, new DefinitionDecorator('security.user.provider.in_memory.user')) |
44
|
|
|
->setArguments(array($username, (string) $user['password'], $user['roles'])) |
45
|
|
|
; |
46
|
|
|
|
47
|
|
|
$definition->addMethodCall('createUser', array(new Reference($userId))); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns the key of current factory. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getKey() |
57
|
|
|
{ |
58
|
|
|
return 'memory_api'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds configuration nodes to current user provider. |
63
|
|
|
* |
64
|
|
|
* @param NodeDefinition $node |
65
|
|
|
*/ |
66
|
|
|
public function addConfiguration(NodeDefinition $node) |
67
|
|
|
{ |
68
|
|
|
$node |
|
|
|
|
69
|
|
|
->fixXmlConfig('user') |
70
|
|
|
->children() |
71
|
|
|
->arrayNode('users') |
72
|
|
|
->useAttributeAsKey('name') |
73
|
|
|
->prototype('array') |
74
|
|
|
->children() |
75
|
|
|
->scalarNode('password')->defaultValue(uniqid('', true))->end() |
76
|
|
|
->arrayNode('roles') |
77
|
|
|
->beforeNormalization()->ifString()->then(function ($v) { |
78
|
|
|
return preg_split('/\s*,\s*/', $v); |
79
|
|
|
}) |
80
|
|
|
->end() |
81
|
|
|
->prototype('scalar')->end() |
82
|
|
|
->end() |
83
|
|
|
->end() |
84
|
|
|
->end() |
85
|
|
|
->end() |
86
|
|
|
->end() |
87
|
|
|
; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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: