From 877cb06bfed4524beb62e4cc52f946406ef7d5ea Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 29 Sep 2016 16:38:29 +0200 Subject: [PATCH] Use magic DI for core controllers Signed-off-by: Joas Schilling --- core/Application.php | 113 ++----------------- core/Controller/LoginController.php | 8 +- core/Controller/LostController.php | 19 ++-- core/Controller/TokenController.php | 8 +- tests/Core/Controller/LostControllerTest.php | 14 ++- 5 files changed, 35 insertions(+), 127 deletions(-) diff --git a/core/Application.php b/core/Application.php index 9a6d0878fe..4fa83f0985 100644 --- a/core/Application.php +++ b/core/Application.php @@ -1,6 +1,7 @@ * * @author Bernhard Posselt * @author Christoph Wurst @@ -29,13 +30,8 @@ namespace OC\Core; -use OC\AppFramework\Utility\SimpleContainer; -use OC\AppFramework\Utility\TimeFactory; -use OC\Core\Controller\LoginController; -use OC\Core\Controller\LostController; -use OC\Core\Controller\TokenController; -use OC\Core\Controller\TwoFactorChallengeController; -use OC\Core\Controller\UserController; +use OC\Authentication\Token\DefaultTokenProvider; +use OC\Authentication\Token\IProvider; use OCP\AppFramework\App; use OCP\Util; @@ -46,108 +42,13 @@ use OCP\Util; */ class Application extends App { - /** - * @param array $urlParams - */ - public function __construct(array $urlParams=array()){ - parent::__construct('core', $urlParams); + public function __construct() { + parent::__construct('core'); $container = $this->getContainer(); - - /** - * Controllers - */ - $container->registerService('LostController', function(SimpleContainer $c) { - return new LostController( - $c->query('AppName'), - $c->query('Request'), - $c->query('URLGenerator'), - $c->query('UserManager'), - $c->query('Defaults'), - $c->query('L10N'), - $c->query('Config'), - $c->query('SecureRandom'), - $c->query('DefaultEmailAddress'), - $c->query('IsEncryptionEnabled'), - $c->query('Mailer'), - $c->query('TimeFactory') - ); - }); - $container->registerService('LoginController', function(SimpleContainer $c) { - return new LoginController( - $c->query('AppName'), - $c->query('Request'), - $c->query('UserManager'), - $c->query('Config'), - $c->query('Session'), - $c->query('UserSession'), - $c->query('URLGenerator'), - $c->query('TwoFactorAuthManager'), - $c->query('ServerContainer')->getBruteforceThrottler() - ); - }); - $container->registerService('TwoFactorChallengeController', function (SimpleContainer $c) { - return new TwoFactorChallengeController( - $c->query('AppName'), - $c->query('Request'), - $c->query('TwoFactorAuthManager'), - $c->query('UserSession'), - $c->query('Session'), - $c->query('URLGenerator')); - }); - $container->registerService('TokenController', function(SimpleContainer $c) { - return new TokenController( - $c->query('AppName'), - $c->query('Request'), - $c->query('UserManager'), - $c->query('ServerContainer')->query('OC\Authentication\Token\IProvider'), - $c->query('TwoFactorAuthManager'), - $c->query('SecureRandom') - ); - }); - - /** - * Core class wrappers - */ - $container->registerService('IsEncryptionEnabled', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getEncryptionManager()->isEnabled(); - }); - $container->registerService('URLGenerator', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getURLGenerator(); - }); - $container->registerService('UserManager', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getUserManager(); - }); - $container->registerService('Config', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getConfig(); - }); - $container->registerService('L10N', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getL10N('core'); - }); - $container->registerService('SecureRandom', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getSecureRandom(); - }); - $container->registerService('Session', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getSession(); - }); - $container->registerService('UserSession', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getUserSession(); - }); - $container->registerService('Defaults', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getThemingDefaults(); - }); - $container->registerService('Mailer', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getMailer(); - }); - $container->registerService('TimeFactory', function(SimpleContainer $c) { - return new TimeFactory(); - }); - $container->registerService('DefaultEmailAddress', function() { + $container->registerService('defaultMailAddress', function() { return Util::getDefaultEmailAddress('lostpassword-noreply'); }); - $container->registerService('TwoFactorAuthManager', function(SimpleContainer $c) { - return $c->query('ServerContainer')->getTwoFactorAuthManager(); - }); + $container->registerAlias(IProvider::class, DefaultTokenProvider::class); } - } diff --git a/core/Controller/LoginController.php b/core/Controller/LoginController.php index 083f4bb051..884eea8869 100644 --- a/core/Controller/LoginController.php +++ b/core/Controller/LoginController.php @@ -33,12 +33,14 @@ use OC_Util; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\Authentication\TwoFactorAuth\IProvider; use OCP\IConfig; use OCP\IRequest; use OCP\ISession; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; +use OCP\IUserSession; class LoginController extends Controller { /** @var IUserManager */ @@ -47,7 +49,7 @@ class LoginController extends Controller { private $config; /** @var ISession */ private $session; - /** @var Session */ + /** @var IUserSession|Session */ private $userSession; /** @var IURLGenerator */ private $urlGenerator; @@ -62,7 +64,7 @@ class LoginController extends Controller { * @param IUserManager $userManager * @param IConfig $config * @param ISession $session - * @param Session $userSession + * @param IUserSession $userSession * @param IURLGenerator $urlGenerator * @param Manager $twoFactorManager * @param Throttler $throttler @@ -72,7 +74,7 @@ class LoginController extends Controller { IUserManager $userManager, IConfig $config, ISession $session, - Session $userSession, + IUserSession $userSession, IURLGenerator $urlGenerator, Manager $twoFactorManager, Throttler $throttler) { diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index b1111559a6..bfc24bd1e0 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -33,6 +33,7 @@ namespace OC\Core\Controller; use \OCP\AppFramework\Controller; use \OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Encryption\IManager; use \OCP\IURLGenerator; use \OCP\IRequest; use \OCP\IL10N; @@ -60,8 +61,8 @@ class LostController extends Controller { protected $l10n; /** @var string */ protected $from; - /** @var bool */ - protected $isDataEncrypted; + /** @var IManager */ + protected $encryptionManager; /** @var IConfig */ protected $config; /** @var ISecureRandom */ @@ -80,8 +81,8 @@ class LostController extends Controller { * @param IL10N $l10n * @param IConfig $config * @param ISecureRandom $secureRandom - * @param string $from - * @param string $isDataEncrypted + * @param string $defaultMailAddress + * @param IManager $encryptionManager * @param IMailer $mailer * @param ITimeFactory $timeFactory */ @@ -93,8 +94,8 @@ class LostController extends Controller { IL10N $l10n, IConfig $config, ISecureRandom $secureRandom, - $from, - $isDataEncrypted, + $defaultMailAddress, + IManager $encryptionManager, IMailer $mailer, ITimeFactory $timeFactory) { parent::__construct($appName, $request); @@ -103,8 +104,8 @@ class LostController extends Controller { $this->defaults = $defaults; $this->l10n = $l10n; $this->secureRandom = $secureRandom; - $this->from = $from; - $this->isDataEncrypted = $isDataEncrypted; + $this->from = $defaultMailAddress; + $this->encryptionManager = $encryptionManager; $this->config = $config; $this->mailer = $mailer; $this->timeFactory = $timeFactory; @@ -207,7 +208,7 @@ class LostController extends Controller { * @return array */ public function setPassword($token, $userId, $password, $proceed) { - if ($this->isDataEncrypted && !$proceed) { + if ($this->encryptionManager->isEnabled() && !$proceed) { return $this->error('', array('encryption' => true)); } diff --git a/core/Controller/TokenController.php b/core/Controller/TokenController.php index 6e3ff50fa1..865bae9665 100644 --- a/core/Controller/TokenController.php +++ b/core/Controller/TokenController.php @@ -27,14 +27,14 @@ use OC\AppFramework\Http; use OC\Authentication\Token\IProvider; use OC\Authentication\Token\IToken; use OC\Authentication\TwoFactorAuth\Manager as TwoFactorAuthManager; -use OC\User\Manager as UserManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; +use OCP\IUserManager; use OCP\Security\ISecureRandom; class TokenController extends Controller { - /** @var UserManager */ + /** @var IUserManager */ private $userManager; /** @var IProvider */ private $tokenProvider; @@ -46,14 +46,14 @@ class TokenController extends Controller { /** * @param string $appName * @param IRequest $request - * @param UserManager $userManager + * @param IUserManager $userManager * @param IProvider $tokenProvider * @param TwoFactorAuthManager $twoFactorAuthManager * @param ISecureRandom $secureRandom */ public function __construct($appName, IRequest $request, - UserManager $userManager, + IUserManager $userManager, IProvider $tokenProvider, TwoFactorAuthManager $twoFactorAuthManager, ISecureRandom $secureRandom) { diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index 2e7d6721d5..177e41061a 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -24,6 +24,7 @@ namespace Tests\Core\Controller; use OC\Core\Controller\LostController; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Encryption\IManager; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; @@ -59,6 +60,8 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { private $mailer; /** @var ISecureRandom | PHPUnit_Framework_MockObject_MockObject */ private $secureRandom; + /** @var IManager|PHPUnit_Framework_MockObject_MockObject */ + private $encryptionManager; /** @var ITimeFactory | PHPUnit_Framework_MockObject_MockObject */ private $timeFactory; /** @var IRequest */ @@ -98,6 +101,11 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { ->disableOriginalConstructor()->getMock(); $this->request = $this->getMockBuilder('OCP\IRequest') ->disableOriginalConstructor()->getMock(); + $this->encryptionManager = $this->getMockBuilder(IManager::class) + ->disableOriginalConstructor()->getMock(); + $this->encryptionManager->expects($this->any()) + ->method('isEnabled') + ->willReturn(true); $this->lostController = new LostController( 'Core', $this->request, @@ -108,7 +116,7 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { $this->config, $this->secureRandom, 'lostpassword-noreply@localhost', - true, + $this->encryptionManager, $this->mailer, $this->timeFactory ); @@ -162,8 +170,6 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { public function testResetFormExpiredToken() { - $userId = 'ValidTokenUser'; - $token = '12345:TheOnlyAndOnlyOneTokenToResetThePassword'; $user = $this->getMockBuilder('\OCP\IUser') ->disableOriginalConstructor()->getMock(); $this->userManager @@ -195,8 +201,6 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase { } public function testResetFormValidToken() { - $userId = 'ValidTokenUser'; - $token = '12345:TheOnlyAndOnlyOneTokenToResetThePassword'; $user = $this->getMockBuilder('\OCP\IUser') ->disableOriginalConstructor()->getMock(); $user