1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine\AccessToken; |
5
|
|
|
|
6
|
|
|
use Kdyby\Doctrine\EntityManager; |
7
|
|
|
use Kdyby\Doctrine\EntityRepository; |
8
|
|
|
use Kdyby\Doctrine\QueryObject; |
9
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
10
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
11
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
12
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
13
|
|
|
|
14
|
|
|
class AccessTokenRepository implements AccessTokenRepositoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EntityManager |
18
|
|
|
*/ |
19
|
|
|
private $entityManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityRepository |
23
|
|
|
*/ |
24
|
|
|
private $repository; |
25
|
|
|
|
26
|
|
|
public function __construct(EntityManager $entityManager) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->entityManager = $entityManager; |
29
|
|
|
$this->repository = $entityManager->getRepository(AccessTokenEntity::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param ClientEntityInterface $clientEntity |
34
|
|
|
* @param ScopeEntityInterface[] $scopes |
35
|
|
|
* @param string|null $userIdentifier |
36
|
|
|
* @return AccessTokenEntity |
37
|
|
|
*/ |
38
|
|
|
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) |
39
|
|
|
{ |
40
|
|
|
$accessToken = new AccessTokenEntity(); |
41
|
|
|
$accessToken->setClient($clientEntity); |
42
|
|
|
foreach ($scopes as $scope) { |
43
|
|
|
$accessToken->addScope($scope); |
44
|
|
|
} |
45
|
|
|
$accessToken->setUserIdentifier($userIdentifier); |
46
|
|
|
return $accessToken; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param AccessTokenEntityInterface $accessTokenEntity |
51
|
|
|
*/ |
52
|
|
|
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) |
53
|
|
|
{ |
54
|
|
|
$this->entityManager->persist($accessTokenEntity); |
55
|
|
|
$this->entityManager->flush(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $tokenId |
60
|
|
|
*/ |
61
|
|
|
public function revokeAccessToken($tokenId) |
62
|
|
|
{ |
63
|
|
|
$this->repository->fetchOne($this->createQuery()->byIdentifier($tokenId))->setRevoked(true); |
64
|
|
|
$this->entityManager->flush(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $tokenId |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isAccessTokenRevoked($tokenId) |
72
|
|
|
{ |
73
|
|
|
return $this->repository->fetchOne($this->createQuery()->byIdentifier($tokenId))->isRevoked(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return QueryObject |
78
|
|
|
*/ |
79
|
|
|
protected function createQuery(): QueryObject |
80
|
|
|
{ |
81
|
|
|
return new AccessTokenQuery(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.