2011-04-22 16:41:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-04-24 12:59:46 +01:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-04-22 16:41:21 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form\Extension\Csrf;
|
|
|
|
|
2013-10-04 14:25:38 +01:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
|
|
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderAdapter;
|
2014-01-01 08:20:41 +00:00
|
|
|
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
|
|
|
|
use Symfony\Component\Form\AbstractExtension;
|
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
|
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2011-05-18 10:01:52 +01:00
|
|
|
/**
|
2012-05-21 03:26:59 +01:00
|
|
|
* This extension protects forms by using a CSRF token.
|
2013-05-02 10:05:04 +01:00
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-05-18 10:01:52 +01:00
|
|
|
*/
|
2011-04-22 16:41:21 +01:00
|
|
|
class CsrfExtension extends AbstractExtension
|
|
|
|
{
|
2013-05-02 10:05:04 +01:00
|
|
|
/**
|
2013-10-04 14:25:38 +01:00
|
|
|
* @var CsrfTokenManagerInterface
|
2013-05-02 10:05:04 +01:00
|
|
|
*/
|
2013-10-04 14:25:38 +01:00
|
|
|
private $tokenManager;
|
2011-04-22 16:41:21 +01:00
|
|
|
|
2013-05-02 10:05:04 +01:00
|
|
|
/**
|
|
|
|
* @var TranslatorInterface
|
|
|
|
*/
|
|
|
|
private $translator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|string
|
|
|
|
*/
|
|
|
|
private $translationDomain;
|
|
|
|
|
2011-05-18 10:01:52 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2013-10-04 14:25:38 +01:00
|
|
|
* @param CsrfTokenManagerInterface $tokenManager The CSRF token manager
|
|
|
|
* @param TranslatorInterface $translator The translator for translating error messages
|
|
|
|
* @param null|string $translationDomain The translation domain for translating
|
2011-05-18 10:01:52 +01:00
|
|
|
*/
|
2013-10-04 14:25:38 +01:00
|
|
|
public function __construct($tokenManager, TranslatorInterface $translator = null, $translationDomain = null)
|
2011-04-22 16:41:21 +01:00
|
|
|
{
|
2013-10-04 14:25:38 +01:00
|
|
|
if ($tokenManager instanceof CsrfProviderInterface) {
|
|
|
|
$tokenManager = new CsrfProviderAdapter($tokenManager);
|
|
|
|
} elseif (!$tokenManager instanceof CsrfTokenManagerInterface) {
|
|
|
|
throw new UnexpectedTypeException($tokenManager, 'CsrfProviderInterface or CsrfTokenManagerInterface');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->tokenManager = $tokenManager;
|
2013-05-02 10:05:04 +01:00
|
|
|
$this->translator = $translator;
|
|
|
|
$this->translationDomain = $translationDomain;
|
2011-04-22 16:41:21 +01:00
|
|
|
}
|
|
|
|
|
2011-05-18 10:01:52 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-18 10:01:52 +01:00
|
|
|
*/
|
2011-04-22 18:22:26 +01:00
|
|
|
protected function loadTypeExtensions()
|
2011-04-22 16:41:21 +01:00
|
|
|
{
|
2011-04-22 18:22:26 +01:00
|
|
|
return array(
|
2013-10-04 14:25:38 +01:00
|
|
|
new Type\FormTypeCsrfExtension($this->tokenManager, true, '_token', $this->translator, $this->translationDomain),
|
2011-04-22 18:22:26 +01:00
|
|
|
);
|
2011-04-22 16:41:21 +01:00
|
|
|
}
|
|
|
|
}
|