InMemoryFactory.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  14. /**
  15. * InMemoryFactory creates services for the memory provider.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Christophe Coevoet <stof@notk.org>
  19. */
  20. class InMemoryFactory implements UserProviderFactoryInterface
  21. {
  22. public function create(ContainerBuilder $container, $id, $config)
  23. {
  24. $definition = $container->setDefinition($id, new DefinitionDecorator('security.user.provider.in_memory'));
  25. $users = array();
  26. foreach ($config['users'] as $username => $user) {
  27. $users[$username] = array('password' => (string) $user['password'], 'roles' => $user['roles']);
  28. }
  29. $definition->addArgument($users);
  30. }
  31. public function getKey()
  32. {
  33. return 'memory';
  34. }
  35. public function addConfiguration(NodeDefinition $node)
  36. {
  37. $node
  38. ->fixXmlConfig('user')
  39. ->children()
  40. ->arrayNode('users')
  41. ->useAttributeAsKey('name')
  42. ->prototype('array')
  43. ->children()
  44. ->scalarNode('password')->defaultValue(uniqid('', true))->end()
  45. ->arrayNode('roles')
  46. ->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
  47. ->prototype('scalar')->end()
  48. ->end()
  49. ->end()
  50. ->end()
  51. ->end()
  52. ->end()
  53. ;
  54. }
  55. }