TYPO3  7.6
BackendFormProtection.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\FormProtection;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
72 
77 {
84  protected $backendUser;
85 
92  protected $registry;
93 
103  {
104  $this->backendUser = $backendUser;
105  $this->registry = $registry;
106  $this->validationFailedCallback = $validationFailedCallback;
107  if (!$this->isAuthorizedBackendSession()) {
108  throw new \TYPO3\CMS\Core\Error\Exception('A back-end form protection may only be instantiated if there is an active back-end session.', 1285067843);
109  }
110  }
111 
117  protected function retrieveSessionToken()
118  {
119  $this->sessionToken = $this->backendUser->getSessionData('formProtectionSessionToken');
120  if (empty($this->sessionToken)) {
121  $this->sessionToken = $this->generateSessionToken();
122  $this->persistSessionToken();
123  }
124  return $this->sessionToken;
125  }
126 
134  public function persistSessionToken()
135  {
136  $this->backendUser->setAndSaveSessionData('formProtectionSessionToken', $this->sessionToken);
137  }
138 
147  public function setSessionTokenFromRegistry()
148  {
149  $this->sessionToken = $this->registry->get('core', 'formProtectionSessionToken:' . $this->backendUser->user['uid']);
150  if (empty($this->sessionToken)) {
151  throw new \UnexpectedValueException('Failed to restore the session token from the registry.', 1301827270);
152  }
153  return $this->sessionToken;
154  }
155 
163  public function storeSessionTokenInRegistry()
164  {
165  $this->registry->set('core', 'formProtectionSessionToken:' . $this->backendUser->user['uid'], $this->getSessionToken());
166  }
167 
174  {
175  $this->registry->remove('core', 'formProtectionSessionToken:' . $this->backendUser->user['uid']);
176  }
177 
183  protected function isAuthorizedBackendSession()
184  {
185  return !empty($this->backendUser->user['uid']);
186  }
187 }