1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bgy\OAuth2Server\DbalStorage; |
4
|
|
|
|
5
|
|
|
use Bgy\OAuth2\Storage\ClientNotFound; |
6
|
|
|
use Bgy\OAuth2\Storage\ClientStorage; |
7
|
|
|
use Bgy\OAuth2\Client as ClientModel; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Boris Guéry <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class DbalClientStorage implements ClientStorage |
14
|
|
|
{ |
15
|
|
|
private $dbalConnection; |
16
|
|
|
private $tableConfiguration; |
17
|
|
|
|
18
|
|
|
public function __construct(Connection $dbalConnection, TableConfiguration $tableConfiguration) |
19
|
|
|
{ |
20
|
|
|
$this->dbalConnection = $dbalConnection; |
21
|
|
|
$this->tableConfiguration = $tableConfiguration; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function save(ClientModel $client) |
25
|
|
|
{ |
26
|
|
|
$this->dbalConnection->insert( |
27
|
|
|
$this->tableConfiguration->getClientTableName(), |
28
|
|
|
[ |
29
|
|
|
'client_id' => $client->getId(), |
30
|
|
|
'client_secret' => $client->getSecret(), |
31
|
|
|
'redirect_uris' => serialize($client->getRedirectUris()), |
32
|
|
|
'grant_types' => serialize($client->getAllowedGrantTypes()), |
33
|
|
|
|
34
|
|
|
] |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function delete(ClientModel $client) |
39
|
|
|
{ |
40
|
|
|
$this->dbalConnection->delete( |
41
|
|
|
$this->tableConfiguration->getClientTableName(), |
42
|
|
|
['client_id' => $client->getId()] |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function findById($clientId) |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
$qb = $this->dbalConnection->createQueryBuilder(); |
50
|
|
|
$stmt = $qb->select('*') |
51
|
|
|
->from($this->tableConfiguration->getClientTableName()) |
52
|
|
|
->where( |
53
|
|
|
$qb->expr()->like('client_id', ':clientId') |
54
|
|
|
) |
55
|
|
|
->setMaxResults(1) |
56
|
|
|
->setParameter('clientId', $clientId) |
57
|
|
|
->execute() |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
if (1 !== count($rows)) { |
64
|
|
|
|
65
|
|
|
throw new ClientNotFound($clientId); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return new ClientModel( |
69
|
|
|
$rows[0]['client_id'], |
70
|
|
|
$rows[0]['client_secret'], |
71
|
|
|
unserialize($rows[0]['redirect_uris']), |
72
|
|
|
unserialize($rows[0]['allowed_grant_types']) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|