[!!!][TASK] Clean up ObjectManager injection of CommandController 94/44694/14
authorMathias Brodala <mbrodala@pagemachine.de>
Fri, 20 Nov 2015 16:52:22 +0000 (17:52 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Sat, 5 Mar 2016 22:56:54 +0000 (23:56 +0100)
The ObjectManager injection method was misused to initialize
a lot of objects which are now instantiated separately.

Resolves: #71521
Releases: master
Change-Id: I6509ac064ec39140b841a258c46ad5a021039972
Reviewed-on: https://review.typo3.org/44694
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Breaking-71521-PropertyUserAuthenticationRemovedFromCommandController.rst [new file with mode: 0644]
typo3/sysext/core/Documentation/Changelog/master/Important-71521-InternalChangesInCommandController.rst [new file with mode: 0644]
typo3/sysext/extbase/Classes/Mvc/Controller/CommandController.php
typo3/sysext/extbase/Tests/Unit/Mvc/Controller/CommandControllerTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-71521-PropertyUserAuthenticationRemovedFromCommandController.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-71521-PropertyUserAuthenticationRemovedFromCommandController.rst
new file mode 100644 (file)
index 0000000..ce1c8a8
--- /dev/null
@@ -0,0 +1,22 @@
+=============================================================================
+Breaking: #71521 - Property userAuthentication removed from CommandController
+=============================================================================
+
+Description
+===========
+
+The property ``$userAuthentication`` was removed from the Extbase ``CommandController`` class and
+has been migrated to the newly introduced ``getBackendUserAuthentication()`` method.
+
+
+Impact
+======
+
+All command controllers deriving from ``CommandController`` with see a fatal error when accessing
+properties or methods of the removed ``$userAuthentication`` property.
+
+
+Migration
+=========
+
+Use the newly introduced ``getBackendUserAuthentication()`` method.
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Important-71521-InternalChangesInCommandController.rst b/typo3/sysext/core/Documentation/Changelog/master/Important-71521-InternalChangesInCommandController.rst
new file mode 100644 (file)
index 0000000..13aec26
--- /dev/null
@@ -0,0 +1,10 @@
+=========================================================
+Important: #71521 - Internal changes in CommandController
+=========================================================
+
+Description
+===========
+
+The ``CommandController::processRequest()`` method has been changed to initialize arguments and output.
+
+If this method was overridden without calling the parent method, these changes must be copied to prevent errors.
index 6feaa99..df710aa 100644 (file)
@@ -15,16 +15,20 @@ namespace TYPO3\CMS\Extbase\Mvc\Controller;
  */
 
 use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
+use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Extbase\Mvc\Cli\CommandArgumentDefinition;
 use TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput;
 use TYPO3\CMS\Extbase\Mvc\Cli\Request;
 use TYPO3\CMS\Extbase\Mvc\Cli\Response;
+use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
 use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException;
 use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchCommandException;
 use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
 use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException;
 use TYPO3\CMS\Extbase\Mvc\RequestInterface;
 use TYPO3\CMS\Extbase\Mvc\ResponseInterface;
+use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
+use TYPO3\CMS\Extbase\Reflection\ReflectionService;
 
 /**
  * A controller which processes requests from the command line
@@ -64,22 +68,17 @@ class CommandController implements CommandControllerInterface
     protected $requestAdminPermissions = false;
 
     /**
-     * @var AbstractUserAuthentication
-     */
-    protected $userAuthentication;
-
-    /**
-     * @var \TYPO3\CMS\Extbase\Reflection\ReflectionService
+     * @var ReflectionService
      */
     protected $reflectionService;
 
     /**
-     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
+     * @var ObjectManagerInterface
      */
     protected $objectManager;
 
     /**
-     * @var \TYPO3\CMS\Extbase\Mvc\Cli\ConsoleOutput
+     * @var ConsoleOutput
      */
     protected $output;
 
@@ -87,18 +86,15 @@ class CommandController implements CommandControllerInterface
      * @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager
      * @return void
      */
-    public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager)
+    public function injectObjectManager(ObjectManagerInterface $objectManager)
     {
         $this->objectManager = $objectManager;
-        $this->arguments = $this->objectManager->get(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class);
-        $this->userAuthentication = isset($GLOBALS['BE_USER']) ? $GLOBALS['BE_USER'] : null;
-        $this->output = $this->objectManager->get(ConsoleOutput::class);
     }
 
     /**
      * @param \TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService
      */
-    public function injectReflectionService(\TYPO3\CMS\Extbase\Reflection\ReflectionService $reflectionService)
+    public function injectReflectionService(ReflectionService $reflectionService)
     {
         $this->reflectionService = $reflectionService;
     }
@@ -134,6 +130,8 @@ class CommandController implements CommandControllerInterface
         $this->response = $response;
 
         $this->commandMethodName = $this->resolveCommandMethodName();
+        $this->output = $this->objectManager->get(ConsoleOutput::class);
+        $this->arguments = $this->objectManager->get(Arguments::class);
         $this->initializeCommandMethodArguments();
         $this->mapRequestArgumentsToControllerArguments();
         $this->callCommandMethod();
@@ -168,7 +166,6 @@ class CommandController implements CommandControllerInterface
      */
     protected function initializeCommandMethodArguments()
     {
-        $this->arguments->removeAll();
         $methodParameters = $this->reflectionService->getMethodParameters(get_class($this), $this->commandMethodName);
 
         foreach ($methodParameters as $parameterName => $parameterInfo) {
@@ -271,11 +268,14 @@ class CommandController implements CommandControllerInterface
      */
     protected function ensureAdminRoleIfRequested()
     {
-        if (!$this->requestAdminPermissions || !$this->userAuthentication || !isset($this->userAuthentication->user['admin'])) {
+        $userAuthentication = $this->getBackendUserAuthentication();
+
+        if (!$this->requestAdminPermissions || $userAuthentication === null || !isset($userAuthentication->user['admin'])) {
             return null;
         }
-        $originalRole = $this->userAuthentication->user['admin'];
-        $this->userAuthentication->user['admin'] = 1;
+
+        $originalRole = $userAuthentication->user['admin'];
+        $userAuthentication->user['admin'] = 1;
         return $originalRole;
     }
 
@@ -286,8 +286,10 @@ class CommandController implements CommandControllerInterface
      */
     protected function restoreUserRole($originalRole)
     {
-        if ($originalRole !== null) {
-            $this->userAuthentication->user['admin'] = $originalRole;
+        $userAuthentication = $this->getBackendUserAuthentication();
+
+        if ($originalRole !== null && $userAuthentication !== null) {
+            $userAuthentication->user['admin'] = $originalRole;
         }
     }
 
@@ -359,4 +361,14 @@ class CommandController implements CommandControllerInterface
         $this->response->send();
         exit($exitCode);
     }
+
+    /**
+     * Returns the global BackendUserAuthentication object.
+     *
+     * @return BackendUserAuthentication|null
+     */
+    protected function getBackendUserAuthentication()
+    {
+        return isset($GLOBALS['BE_USER']) ? $GLOBALS['BE_USER'] : null;
+    }
 }
index ba95ed6..93fdd24 100644 (file)
@@ -94,7 +94,7 @@ class CommandControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
                         }
                     }
                 ));
-        $this->commandController->_set('userAuthentication', $mockedUserAuthentication);
+        $GLOBALS['BE_USER'] = $mockedUserAuthentication;
         $this->commandController->_set('arguments', array());
         $this->commandController->_set('commandMethodName', 'dummyCommand');
         $this->commandController->_set('requestAdminPermissions', true);