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