Skip to content

Commit

Permalink
persist UserProvider, fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ojhaujjwal committed Aug 28, 2014
1 parent d92d328 commit 2c759a0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/Grant/Oauth2Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ZfrOAuth2\Server\Grant\AuthorizationServerAwareTrait;
use Hrevert\OauthClient\Entity\UserProvider;
use Hrevert\OauthClient\Model\UserInterface;
use Doctrine\Common\Persistence\ObjectManager;

class Oauth2Client extends AbstractGrant implements AuthorizationServerAwareInterface
{
Expand Down Expand Up @@ -48,6 +49,11 @@ class Oauth2Client extends AbstractGrant implements AuthorizationServerAwareInte
*/
protected $options;

/**
* @var ObjectManager
*/
protected $objectManager;

/**
* Constructor
*
Expand All @@ -56,14 +62,16 @@ class Oauth2Client extends AbstractGrant implements AuthorizationServerAwareInte
* @param ProviderManagerInterface $providerManager
* @param UserProviderManagerInterface $userProviderManager
* @param ServiceLocatorInterface $providerClients
* @param ObjectManager $objectManager
*/
public function __construct(
TokenService $accessTokenService,
TokenService $refreshTokenService,
ProviderManagerInterface $providerManager,
UserProviderManagerInterface $userProviderManager,
ServiceLocatorInterface $providerClients,
ModuleOptions $options
ModuleOptions $options,
ObjectManager $objectManager
)
{
$this->accessTokenService = $accessTokenService;
Expand All @@ -72,6 +80,7 @@ public function __construct(
$this->userProviderManager = $userProviderManager;
$this->providerClients = $providerClients;
$this->options = $options;
$this->objectManager = $objectManager;
}

/**
Expand All @@ -96,7 +105,7 @@ public function createTokenResponse(HttpRequest $request, Client $client = null,
}

if ($providerAuthorizationCode === null) {
throw OAuth2Exception::invalidRequest('Provider access token is missing');
throw OAuth2Exception::invalidRequest('Provider authorization code is missing');
}

$provider = $this->providerManager->findByName($providerName);
Expand Down Expand Up @@ -138,6 +147,9 @@ public function createTokenResponse(HttpRequest $request, Client $client = null,

$userProvider->setProviderUid($userDetails->uid);
$userProvider->setProvider($provider);

$this->objectManager->persist($userProvider);
$this->objectManager->flush();
}

// Everything is okey, we can start tokens generation!
Expand Down
21 changes: 17 additions & 4 deletions tests/src/Grant/Oauth2ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ protected function createOauth2ClientGrant()
->disableOriginalConstructor()
->getMock();

$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');

$grant = new Oauth2Client(
$accessTokenService,
$refreshTokenService,
$providerManager,
$userProviderManager,
$providerClients,
$options
$options,
$objectManager
);

$grant->setAuthorizationServer($authorizationServer);
Expand All @@ -55,7 +58,8 @@ protected function createOauth2ClientGrant()
$userProviderManager,
$providerClients,
$authorizationServer,
$options
$options,
$objectManager
];
}

Expand Down Expand Up @@ -122,7 +126,8 @@ public function testGetExceptionWhenProviderAuthorizationCodeIsInvalid()
$userProviderManager,
$providerClients,
$authorizationServer,
$options
$options,
$objectManager
) = $this->createOauth2ClientGrant();

$request = $this->getMock('Zend\Http\Request');
Expand Down Expand Up @@ -181,7 +186,8 @@ public function testCanCreateTokenResponse($hasRefreshGrant, UserProviderInterfa
$userProviderManager,
$providerClients,
$authorizationServer,
$options
$options,
$objectManager
) = $this->createOauth2ClientGrant();

$request = $this->getMock('Zend\Http\Request');
Expand Down Expand Up @@ -246,6 +252,13 @@ public function testCanCreateTokenResponse($hasRefreshGrant, UserProviderInterfa
$userProvider->expects($this->once())
->method('setProvider')
->with($provider);

$objectManager->expects($this->once())
->method('persist')
->with($userProvider);

$objectManager->expects($this->once())
->method('flush');
}

$owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface');
Expand Down

0 comments on commit 2c759a0

Please sign in to comment.