diff --git a/lib/Alchemy/Phrasea/Controller/Api/OAuth2Controller.php b/lib/Alchemy/Phrasea/Controller/Api/OAuth2Controller.php index 5e131418ee..11dcf4e7f4 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/OAuth2Controller.php +++ b/lib/Alchemy/Phrasea/Controller/Api/OAuth2Controller.php @@ -13,8 +13,12 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application\Helper\DispatcherAware; use Alchemy\Phrasea\Authentication\Context; use Alchemy\Phrasea\Authentication\Exception\AccountLockedException; +use Alchemy\Phrasea\Authentication\Exception\NotAuthenticatedException; use Alchemy\Phrasea\Authentication\Exception\RequireCaptchaException; use Alchemy\Phrasea\Authentication\Phrasea\PasswordAuthenticationInterface; +use Alchemy\Phrasea\Authentication\Provider\ProviderInterface; +use Alchemy\Phrasea\Authentication\ProvidersCollection; +use Alchemy\Phrasea\Authentication\SuggestionFinder; use Alchemy\Phrasea\Controller\Controller; use Alchemy\Phrasea\Core\Configuration\PropertyAccess; use Alchemy\Phrasea\Core\Event\PostAuthenticate; @@ -22,6 +26,9 @@ use Alchemy\Phrasea\Core\Event\PreAuthenticate; use Alchemy\Phrasea\Core\PhraseaEvents; use Alchemy\Phrasea\Model\Manipulator\ApiAccountManipulator; use Alchemy\Phrasea\Model\Repositories\ApiApplicationRepository; +use Alchemy\Phrasea\Model\Repositories\UserRepository; +use Alchemy\Phrasea\Model\Repositories\UsrAuthProviderRepository; +use InvalidArgumentException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; @@ -103,7 +110,7 @@ class OAuth2Controller extends Controller return $this->app->redirectPath('oauth2_authorize', array_merge(array('error' => 'account-locked'), $params)); } - $user = $this->app['repo.users']->find($usrId); + $user = $this->getUserRepository()->find($usrId); $this->getAuthenticator()->openAccount($user); $event = new PostAuthenticate($request, new Response(), $user, $context); $this->dispatch(PhraseaEvents::POST_AUTHENTICATE, $event); @@ -115,6 +122,8 @@ class OAuth2Controller extends Controller } } + $account = $this->oAuth2Adapter->updateAccount($this->getAuthenticatedUser()); + //check if current client is already authorized by current user $clients = $appRepository->findAuthorizedAppsByUser($this->getAuthenticatedUser()); @@ -125,8 +134,6 @@ class OAuth2Controller extends Controller } } - $account = $this->oAuth2Adapter->updateAccount($this->getAuthenticatedUser()); - $params['account_id'] = $account->getId(); if (!$appAuthorized && $actionAccept === null) { @@ -231,6 +238,8 @@ class OAuth2Controller extends Controller $this->oAuth2Adapter->setClient($client); + $account = $this->oAuth2Adapter->updateAccount($this->getAuthenticatedUser()); + //check if current client is already authorized by current user $clients = $appRepository->findAuthorizedAppsByUser($this->getAuthenticatedUser()); $appAuthorized = false; @@ -242,8 +251,6 @@ class OAuth2Controller extends Controller } } - $account = $this->oAuth2Adapter->updateAccount($this->getAuthenticatedUser()); - $params['account_id'] = $account->getId(); //if native app show template @@ -279,7 +286,7 @@ class OAuth2Controller extends Controller throw new HttpException(400, 'This route requires the use of the https scheme: ' . $config->get(['main', 'api_require_ssl']), null, ['content-type' => 'application/json']); } - $this->oAuth2Adapter->grantAccessToken($request); + $this->oAuth2Adapter->grantAccessToken(); ob_flush(); flush(); @@ -340,4 +347,12 @@ class OAuth2Controller extends Controller { return $this->app['authentication.suggestion-finder']; } + + /** + * @return UserRepository + */ + private function getUserRepository() + { + return $this->app['repo.users']; + } } diff --git a/lib/Alchemy/Phrasea/Model/Repositories/UserRepository.php b/lib/Alchemy/Phrasea/Model/Repositories/UserRepository.php index 9d66c73285..d122922091 100644 --- a/lib/Alchemy/Phrasea/Model/Repositories/UserRepository.php +++ b/lib/Alchemy/Phrasea/Model/Repositories/UserRepository.php @@ -22,6 +22,18 @@ use Alchemy\Phrasea\Model\Entities\User; */ class UserRepository extends EntityRepository { + /** + * Finds an User by its primary key / identifier. + * + * @inheritdoc + * + * @return User|null + */ + public function find($id, $lockMode = null, $lockVersion = null) + { + return parent::find($id, $lockMode, $lockVersion); + } + /** * Finds admins. *