|
@@ -11,195 +11,93 @@ namespace Symfony\Component\Form;
|
|
|
* file that was distributed with this source code.
|
|
|
*/
|
|
|
|
|
|
-use Symfony\Component\Form\FieldFactory\FieldFactoryInterface;
|
|
|
-use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
|
|
|
+use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
|
|
|
+use Symfony\Component\Form\Exception\FormException;
|
|
|
use Symfony\Component\Validator\ValidatorInterface;
|
|
|
|
|
|
/**
|
|
|
* Default implementaton of FormContextInterface
|
|
|
*
|
|
|
+ * This class is immutable by design.
|
|
|
+ *
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
|
|
|
*/
|
|
|
class FormContext implements FormContextInterface
|
|
|
{
|
|
|
/**
|
|
|
- * The locale used by new forms
|
|
|
- * @var string
|
|
|
- */
|
|
|
- protected static $locale = 'en';
|
|
|
-
|
|
|
- /**
|
|
|
- * The validator used in the new form
|
|
|
- * @var ValidatorInterface
|
|
|
- */
|
|
|
- protected $validator = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * The validation group(s) validated in the new form
|
|
|
- * @var string|array
|
|
|
- */
|
|
|
- protected $validationGroups = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * The field factory used for automatically creating fields in the form
|
|
|
- * @var FieldFactoryInterface
|
|
|
- */
|
|
|
- protected $fieldFactory = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * The provider used to generate and validate CSRF tokens
|
|
|
+ * The options used in new forms
|
|
|
* @var array
|
|
|
*/
|
|
|
- protected $csrfProvider = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * Whether the new form should be CSRF protected
|
|
|
- * @var Boolean
|
|
|
- */
|
|
|
- protected $csrfProtection = false;
|
|
|
-
|
|
|
- /**
|
|
|
- * The field name used for the CSRF protection
|
|
|
- * @var string
|
|
|
- */
|
|
|
- protected $csrfFieldName = '_token';
|
|
|
-
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- public function validator(ValidatorInterface $validator)
|
|
|
- {
|
|
|
- $this->validator = $validator;
|
|
|
-
|
|
|
- return $this;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- public function validationGroups($validationGroups)
|
|
|
- {
|
|
|
- $this->validationGroups = null === $validationGroups ? $validationGroups : (array) $validationGroups;
|
|
|
-
|
|
|
- return $this;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- public function fieldFactory(FieldFactoryInterface $fieldFactory)
|
|
|
- {
|
|
|
- $this->fieldFactory = $fieldFactory;
|
|
|
-
|
|
|
- return $this;
|
|
|
- }
|
|
|
+ protected $options = null;
|
|
|
|
|
|
/**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- public function csrfProtection($enabled)
|
|
|
- {
|
|
|
- $this->csrfProtection = $enabled;
|
|
|
-
|
|
|
- return $this;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- public function csrfFieldName($name)
|
|
|
+ * Builds a context with default values
|
|
|
+ *
|
|
|
+ * By default, CSRF protection is enabled. In this case you have to provide
|
|
|
+ * a CSRF secret in the second parameter of this method. A recommended
|
|
|
+ * value is a generated value with at least 32 characters and mixed
|
|
|
+ * letters, digits and special characters.
|
|
|
+ *
|
|
|
+ * If you don't want to use CSRF protection, you can leave the CSRF secret
|
|
|
+ * empty and set the third parameter to false.
|
|
|
+ *
|
|
|
+ * @param ValidatorInterface $validator The validator for validating
|
|
|
+ * forms
|
|
|
+ * @param string $csrfSecret The secret to be used for
|
|
|
+ * generating CSRF tokens
|
|
|
+ * @param boolean $csrfProtection Whether forms should be CSRF
|
|
|
+ * protected
|
|
|
+ * @throws FormException When CSRF protection is enabled,
|
|
|
+ * but no CSRF secret is passed
|
|
|
+ */
|
|
|
+ public static function buildDefault(ValidatorInterface $validator, $csrfSecret = null, $csrfProtection = true)
|
|
|
{
|
|
|
- $this->csrfFieldName = $name;
|
|
|
-
|
|
|
- return $this;
|
|
|
- }
|
|
|
+ $options = array(
|
|
|
+ 'csrf_protection' => $csrfProtection,
|
|
|
+ 'validator' => $validator,
|
|
|
+ );
|
|
|
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- public function csrfProvider(CsrfProviderInterface $csrfProvider)
|
|
|
- {
|
|
|
- $this->csrfProvider = $csrfProvider;
|
|
|
+ if ($csrfProtection) {
|
|
|
+ if (empty($csrfSecret)) {
|
|
|
+ throw new FormException('Please provide a CSRF secret when CSRF protection is enabled');
|
|
|
+ }
|
|
|
|
|
|
- return $this;
|
|
|
- }
|
|
|
+ $options['csrf_provider'] = new DefaultCsrfProvider($csrfSecret);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * @inheritDoc
|
|
|
- */
|
|
|
- public function getForm($name, $data = null, array $options = array())
|
|
|
- {
|
|
|
- return new Form(
|
|
|
- $name,
|
|
|
- array_merge(array(
|
|
|
- 'data' => $data,
|
|
|
- 'validator' => $this->validator,
|
|
|
- 'csrf_field_name' => $this->csrfFieldName,
|
|
|
- 'csrf_provider' => $this->csrfProtection ? $this->csrfProvider : null,
|
|
|
- 'validation_groups' => $this->validationGroups,
|
|
|
- 'field_factory' => $this->fieldFactory,
|
|
|
- ), $options)
|
|
|
- );
|
|
|
+ return new static($options);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the validator used in the new form
|
|
|
+ * Constructor
|
|
|
*
|
|
|
- * @return ValidatorInterface The validator instance
|
|
|
- */
|
|
|
- public function getValidator()
|
|
|
- {
|
|
|
- return $this->validator;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Returns the validation groups validated by the new form
|
|
|
+ * Initializes the context with the settings stored in the given
|
|
|
+ * options.
|
|
|
*
|
|
|
- * @return string|array One or more validation groups
|
|
|
+ * @param array $options
|
|
|
*/
|
|
|
- public function getValidationGroups()
|
|
|
+ public function __construct(array $options = array())
|
|
|
{
|
|
|
- return $this->validationGroups;
|
|
|
- }
|
|
|
+ if (isset($options['csrf_protection'])) {
|
|
|
+ if (!$options['csrf_protection']) {
|
|
|
+ // don't include a CSRF provider if CSRF protection is disabled
|
|
|
+ unset($options['csrf_provider']);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Returns the field factory used by the new form
|
|
|
- *
|
|
|
- * @return FieldFactoryInterface The field factory instance
|
|
|
- */
|
|
|
- public function getFieldFactory()
|
|
|
- {
|
|
|
- return $this->fieldFactory;
|
|
|
- }
|
|
|
+ unset($options['csrf_protection']);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Returns whether the new form will be CSRF protected
|
|
|
- *
|
|
|
- * @return Boolean Whether the form will be CSRF protected
|
|
|
- */
|
|
|
- public function isCsrfProtectionEnabled()
|
|
|
- {
|
|
|
- return $this->csrfProtection;
|
|
|
- }
|
|
|
+ $options['context'] = $this;
|
|
|
|
|
|
- /**
|
|
|
- * Returns the field name used for CSRF protection in the new form
|
|
|
- *
|
|
|
- * @return string The CSRF field name
|
|
|
- */
|
|
|
- public function getCsrfFieldName()
|
|
|
- {
|
|
|
- return $this->csrfFieldName;
|
|
|
+ $this->options = $options;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns the CSRF provider used to generate and validate CSRF tokens
|
|
|
- *
|
|
|
- * @return CsrfProviderInterface The provider instance
|
|
|
+ * {@inheritDoc}
|
|
|
*/
|
|
|
- public function getCsrfProvider()
|
|
|
+ public function getOptions()
|
|
|
{
|
|
|
- return $this->csrfProvider;
|
|
|
+ return $this->options;
|
|
|
}
|
|
|
}
|