InMemoryFactory.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider;
  11. use Symfony\Component\Config\Definition\Builder\NodeDefinition;
  12. use Symfony\Component\DependencyInjection\ChildDefinition;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Parameter;
  15. /**
  16. * InMemoryFactory creates services for the memory provider.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Christophe Coevoet <stof@notk.org>
  20. */
  21. class InMemoryFactory implements UserProviderFactoryInterface
  22. {
  23. public function create(ContainerBuilder $container, string $id, array $config)
  24. {
  25. $definition = $container->setDefinition($id, new ChildDefinition('security.user.provider.in_memory'));
  26. $defaultPassword = new Parameter('container.build_id');
  27. $users = [];
  28. foreach ($config['users'] as $username => $user) {
  29. $users[$username] = ['password' => null !== $user['password'] ? (string) $user['password'] : $defaultPassword, 'roles' => $user['roles']];
  30. }
  31. $definition->addArgument($users);
  32. }
  33. public function getKey()
  34. {
  35. return 'memory';
  36. }
  37. public function addConfiguration(NodeDefinition $node)
  38. {
  39. $node
  40. ->fixXmlConfig('user')
  41. ->children()
  42. ->arrayNode('users')
  43. ->useAttributeAsKey('name')
  44. ->normalizeKeys(false)
  45. ->prototype('array')
  46. ->children()
  47. ->scalarNode('password')->defaultNull()->end()
  48. ->arrayNode('roles')
  49. ->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
  50. ->prototype('scalar')->end()
  51. ->end()
  52. ->end()
  53. ->end()
  54. ->end()
  55. ->end()
  56. ;
  57. }
  58. }