diff options
author | Lukas Reschke <lukas@statuscode.ch> | 2016-10-01 20:15:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-01 20:15:51 +0200 |
commit | 793d3868c22d8d5f96aa1d8c7213bc0722421316 (patch) | |
tree | 26d0b3e288ef1a3038ef8678baf888a1956c92dc /core | |
parent | da03018add9d4dcd1ff0bd00089a61a06e619a13 (diff) | |
parent | a1e4b17ff44cc4476574b8d335ac3fdf2c2dc561 (diff) | |
download | nextcloud-server-793d3868c22d8d5f96aa1d8c7213bc0722421316.tar.gz nextcloud-server-793d3868c22d8d5f96aa1d8c7213bc0722421316.zip |
Merge pull request #1578 from nextcloud/use-more-magic-di
Use magic DI for core controllers
Diffstat (limited to 'core')
-rw-r--r-- | core/Application.php | 112 | ||||
-rw-r--r-- | core/Controller/LoginController.php | 8 | ||||
-rw-r--r-- | core/Controller/LostController.php | 19 | ||||
-rw-r--r-- | core/Controller/TokenController.php | 105 | ||||
-rw-r--r-- | core/routes.php | 1 |
5 files changed, 21 insertions, 224 deletions
diff --git a/core/Application.php b/core/Application.php index 9a6d0878fee..97b1e1d37f1 100644 --- a/core/Application.php +++ b/core/Application.php @@ -1,6 +1,7 @@ <?php /** * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> * * @author Bernhard Posselt <dev@bernhard-posselt.com> * @author Christoph Wurst <christoph@owncloud.com> @@ -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,12 @@ 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(); - }); } - } diff --git a/core/Controller/LoginController.php b/core/Controller/LoginController.php index 083f4bb0518..884eea8869e 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 b1111559a6c..bfc24bd1e0f 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 deleted file mode 100644 index 6e3ff50fa1d..00000000000 --- a/core/Controller/TokenController.php +++ /dev/null @@ -1,105 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@owncloud.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ - -namespace OC\Core\Controller; - -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\Security\ISecureRandom; - -class TokenController extends Controller { - /** @var UserManager */ - private $userManager; - /** @var IProvider */ - private $tokenProvider; - /** @var TwoFactorAuthManager */ - private $twoFactorAuthManager; - /** @var ISecureRandom */ - private $secureRandom; - - /** - * @param string $appName - * @param IRequest $request - * @param UserManager $userManager - * @param IProvider $tokenProvider - * @param TwoFactorAuthManager $twoFactorAuthManager - * @param ISecureRandom $secureRandom - */ - public function __construct($appName, - IRequest $request, - UserManager $userManager, - IProvider $tokenProvider, - TwoFactorAuthManager $twoFactorAuthManager, - ISecureRandom $secureRandom) { - parent::__construct($appName, $request); - $this->userManager = $userManager; - $this->tokenProvider = $tokenProvider; - $this->secureRandom = $secureRandom; - $this->twoFactorAuthManager = $twoFactorAuthManager; - } - - /** - * Generate a new access token clients can authenticate with - * - * @PublicPage - * @NoCSRFRequired - * - * @param string $user - * @param string $password - * @param string $name the name of the client - * @return JSONResponse - */ - public function generateToken($user, $password, $name = 'unknown client') { - if (is_null($user) || is_null($password)) { - $response = new JSONResponse(); - $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); - return $response; - } - $loginName = $user; - $user = $this->userManager->checkPassword($loginName, $password); - if ($user === false) { - $response = new JSONResponse(); - $response->setStatus(Http::STATUS_UNAUTHORIZED); - return $response; - } - - if ($this->twoFactorAuthManager->isTwoFactorAuthenticated($user)) { - $resp = new JSONResponse(); - $resp->setStatus(Http::STATUS_UNAUTHORIZED); - return $resp; - } - - $token = $this->secureRandom->generate(128); - $this->tokenProvider->generateToken($token, $user->getUID(), $loginName, $password, $name, IToken::PERMANENT_TOKEN); - return new JSONResponse([ - 'token' => $token, - ]); - } - -} diff --git a/core/routes.php b/core/routes.php index b04b0db4ce7..337f6fb27c3 100644 --- a/core/routes.php +++ b/core/routes.php @@ -48,7 +48,6 @@ $application->registerRoutes($this, [ ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'], ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'], ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'], - ['name' => 'token#generateToken', 'url' => '/token/generate', 'verb' => 'POST'], ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'], ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'], ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'], |