MetadataBag.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Drupal\Core\Session;
  3. use Drupal\Core\Site\Settings;
  4. use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag as SymfonyMetadataBag;
  5. /**
  6. * Provides a container for application specific session metadata.
  7. */
  8. class MetadataBag extends SymfonyMetadataBag {
  9. /**
  10. * The key used to store the CSRF token seed in the session.
  11. */
  12. const CSRF_TOKEN_SEED = 's';
  13. /**
  14. * Constructs a new metadata bag instance.
  15. *
  16. * @param \Drupal\Core\Site\Settings $settings
  17. * The settings instance.
  18. */
  19. public function __construct(Settings $settings) {
  20. $update_threshold = $settings->get('session_write_interval', 180);
  21. parent::__construct('_sf2_meta', $update_threshold);
  22. }
  23. /**
  24. * Set the CSRF token seed.
  25. *
  26. * @param string $csrf_token_seed
  27. * The per-session CSRF token seed.
  28. */
  29. public function setCsrfTokenSeed($csrf_token_seed) {
  30. $this->meta[static::CSRF_TOKEN_SEED] = $csrf_token_seed;
  31. }
  32. /**
  33. * Get the CSRF token seed.
  34. *
  35. * @return string|null
  36. * The per-session CSRF token seed or null when no value is set.
  37. */
  38. public function getCsrfTokenSeed() {
  39. if (isset($this->meta[static::CSRF_TOKEN_SEED])) {
  40. return $this->meta[static::CSRF_TOKEN_SEED];
  41. }
  42. }
  43. /**
  44. * Clear the CSRF token seed.
  45. */
  46. public function clearCsrfTokenSeed() {
  47. unset($this->meta[static::CSRF_TOKEN_SEED]);
  48. }
  49. }