Skip to content

Commit

Permalink
[Form] Changed FormTypeCsrfExtension to use the form's name as defaul…
Browse files Browse the repository at this point in the history
…t intention
  • Loading branch information
webmozart committed Oct 17, 2013
1 parent 0080399 commit b07c618
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
Expand Up @@ -49,7 +49,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)

$builder
->setAttribute('csrf_factory', $builder->getFormFactory())
->addEventSubscriber(new CsrfValidationListener($options['csrf_field_name'], $options['csrf_provider'], $options['intention']))
->addEventSubscriber(new CsrfValidationListener(
$options['csrf_field_name'],
$options['csrf_provider'],
$options['intention'] ?: $builder->getName()
))
;
}

Expand All @@ -64,7 +68,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
{
if ($options['csrf_protection'] && !$view->parent && $options['compound']) {
$factory = $form->getConfig()->getAttribute('csrf_factory');
$data = $options['csrf_provider']->generateCsrfToken($options['intention']);
$data = $options['csrf_provider']->generateCsrfToken($options['intention'] ?: $form->getName());

$csrfForm = $factory->createNamed($options['csrf_field_name'], 'hidden', $data, array(
'mapped' => false,
Expand All @@ -83,7 +87,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'csrf_protection' => $this->defaultEnabled,
'csrf_field_name' => $this->defaultFieldName,
'csrf_provider' => $this->defaultCsrfProvider,
'intention' => 'unknown',
'intention' => null,
));
}

Expand Down
Expand Up @@ -129,6 +129,24 @@ public function testGenerateCsrfToken()
$this->assertEquals('token', $view['csrf']->vars['value']);
}

public function testGenerateCsrfTokenUsesFormNameAsIntentionByDefault()
{
$this->csrfProvider->expects($this->once())
->method('generateCsrfToken')
->with('FORM_NAME')
->will($this->returnValue('token'));

$view = $this->factory
->createNamed('FORM_NAME', 'form', null, array(
'csrf_field_name' => 'csrf',
'csrf_provider' => $this->csrfProvider,
'compound' => true,
))
->createView();

$this->assertEquals('token', $view['csrf']->vars['value']);
}

public function provideBoolean()
{
return array(
Expand Down Expand Up @@ -169,6 +187,37 @@ public function testValidateTokenOnBindIfRootAndCompound($valid)
$this->assertSame($valid, $form->isValid());
}

/**
* @dataProvider provideBoolean
*/
public function testValidateTokenOnBindIfRootAndCompoundUsesFormNameAsIntentionByDefault($valid)
{
$this->csrfProvider->expects($this->once())
->method('isCsrfTokenValid')
->with('FORM_NAME', 'token')
->will($this->returnValue($valid));

$form = $this->factory
->createNamedBuilder('FORM_NAME', 'form', null, array(
'csrf_field_name' => 'csrf',
'csrf_provider' => $this->csrfProvider,
'compound' => true,
))
->add('child', 'text')
->getForm();

$form->bind(array(
'child' => 'foobar',
'csrf' => 'token',
));

// Remove token from data
$this->assertSame(array('child' => 'foobar'), $form->getData());

// Validate accordingly
$this->assertSame($valid, $form->isValid());
}

public function testFailIfRootAndCompoundAndTokenMissing()
{
$this->csrfProvider->expects($this->never())
Expand Down

0 comments on commit b07c618

Please sign in to comment.