Skip to content

Commit 409d0fe

Browse files
committed
ODM v2 support
1 parent c9ef29d commit 409d0fe

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public function getConfigTreeBuilder()
3838
->values(['orm', 'odm'])
3939
->defaultValue('orm')
4040
->end()
41+
->enumNode('odm_version')
42+
->values([1, 2])
43+
->defaultValue(1)
44+
->end()
4145
->integerNode('token_lifetime')
4246
->defaultValue('864000') //10 days
4347
->end()

DependencyInjection/TokenAuthenticationExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function load(array $configs, ContainerBuilder $container)
2828
$container->setParameter('token_authentication.error_codes', $config['error_codes']);
2929
$container->setParameter('token_authentication.token_field', $config['token_field']);
3030
$container->setParameter('token_authentication.platform', $config['platform']);
31+
$container->setParameter('token_authentication.odm_version', $config['odm_version']);
3132

3233
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3334
$loader->load('services.yml');

Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515

1616
access_token_helper:
1717
class: Youshido\TokenAuthenticationBundle\Service\Helper\AccessTokenHelper
18-
arguments: ["@universal_object_manager", "%token_authentication.token_lifetime%", '%token_authentication.platform%']
18+
arguments: ["@universal_object_manager", "%token_authentication.token_lifetime%", '%token_authentication.platform%', '%token_authentication.odm_version%']
1919

2020
token_exception_listener:
2121
class: Youshido\TokenAuthenticationBundle\Service\Listener\ExceptionListener

Service/Helper/AccessTokenHelper.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Youshido\TokenAuthenticationBundle\Service\Helper;
99

1010
use Doctrine\Common\Persistence\ObjectManager;
11-
use Symfony\Component\DependencyInjection\ContainerInterface;
1211
use Youshido\TokenAuthenticationBundle\Model\AccessTokenInterface;
1312
use Youshido\TokenAuthenticationBundle\Service\UniversalObjectManager;
1413

@@ -23,11 +22,15 @@ class AccessTokenHelper
2322
/** @var string */
2423
private $platform;
2524

26-
public function __construct($om, $tokenLifetime, $platform)
25+
/** @var int */
26+
private $odmVersion;
27+
28+
public function __construct($om, $tokenLifetime, $platform, $odmVersion)
2729
{
2830
$this->om = $om;
2931
$this->tokenLifetime = $tokenLifetime;
3032
$this->platform = $platform;
33+
$this->odmVersion = $odmVersion;
3134
}
3235

3336
/**
@@ -93,7 +96,16 @@ public function expireToken(AccessTokenInterface $accessToken)
9396
public function findTokenByModelId($id)
9497
{
9598
if ($this->platform === UniversalObjectManager::PLATFORM_ODM) {
96-
$id = new \MongoId($id);
99+
switch ($this->odmVersion) {
100+
case 1:
101+
$id = new \MongoId($id);
102+
break;
103+
case 2:
104+
$id = new \MongoDB\BSON\ObjectId($id);
105+
break;
106+
default:
107+
throw new \UnexpectedValueException('Unknown ODM version');
108+
}
97109
}
98110

99111
return $this->om->getRepository('TokenAuthenticationBundle:AccessToken')->findOneBy(['modelId' => $id]);

0 commit comments

Comments
 (0)