1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\UnitOfWork; |
6
|
|
|
use OAuth\Client; |
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
9
|
|
|
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; |
10
|
|
|
use OAuth\OAuthUser; |
11
|
|
|
|
12
|
|
|
class ClientRepository extends EntityRepository implements ClientRepositoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param string $clientIdentifier |
16
|
|
|
* @param null|string $grantType |
17
|
|
|
* @param null|string|null $clientSecret |
18
|
|
|
* @param bool $mustValidateSecret |
19
|
|
|
* |
20
|
|
|
* @return ClientEntityInterface|null |
21
|
|
|
*/ |
22
|
6 |
|
public function getClientEntity($clientIdentifier, $grantType = null, $clientSecret = null, $mustValidateSecret = true) |
23
|
|
|
{ |
24
|
|
|
/** @var Client $client */ |
25
|
6 |
|
$client = $this->findOneBy([ |
26
|
6 |
|
'identifier' => $clientIdentifier |
27
|
|
|
]); |
28
|
|
|
|
29
|
6 |
|
if ($client instanceof Client == false) { |
|
|
|
|
30
|
1 |
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
5 |
|
if ($mustValidateSecret === true && $client->isConfidential() === true && $clientSecret != $client->getSecret()) { |
34
|
1 |
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
return $client; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Client $client |
42
|
|
|
* @return Client |
43
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
44
|
|
|
*/ |
45
|
|
|
public function create(Client $client) |
46
|
|
|
{ |
47
|
|
|
$em = $this->getEntityManager(); |
48
|
|
|
$user = $client->getUser(); |
49
|
|
|
if ($em->getUnitOfWork()->getEntityState($user) !== UnitOfWork::STATE_MANAGED) { |
50
|
|
|
/** @var OAuthUser $user */ |
51
|
|
|
$user = $em->merge($user); |
52
|
|
|
$client->setUser($user); |
53
|
|
|
} |
54
|
|
|
$em->persist($client); |
55
|
|
|
$em->flush(); |
56
|
|
|
return $client; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Client $client |
61
|
|
|
* @return Client |
62
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
63
|
|
|
*/ |
64
|
|
|
public function save(Client $client) |
65
|
|
|
{ |
66
|
|
|
$this->_em->flush($client); |
67
|
|
|
return $client; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Client $client |
72
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
73
|
|
|
*/ |
74
|
|
|
public function delete(Client $client) |
75
|
|
|
{ |
76
|
|
|
$this->_em->remove($client); |
77
|
|
|
$this->_em->flush($client); |
78
|
|
|
} |
79
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.