|
1
|
|
|
<?php namespace Limoncello\Passport\Adaptors\PostgreSql; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2018 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Doctrine\DBAL\Connection; |
|
20
|
|
|
use Doctrine\DBAL\DBALException; |
|
21
|
|
|
use Limoncello\Passport\Contracts\Entities\DatabaseSchemaInterface; |
|
22
|
|
|
use Limoncello\Passport\Exceptions\RepositoryException; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @package Limoncello\Passport |
|
27
|
|
|
*/ |
|
28
|
|
|
class TokenRepository extends \Limoncello\Passport\Repositories\TokenRepository |
|
29
|
|
|
{ |
|
30
|
|
|
use ArrayParserTrait; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $modelClass; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Connection $connection |
|
39
|
|
|
* @param DatabaseSchemaInterface $databaseSchema |
|
40
|
|
|
* @param string $modelClass |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function __construct( |
|
43
|
|
|
Connection $connection, |
|
44
|
|
|
DatabaseSchemaInterface $databaseSchema, |
|
45
|
|
|
string $modelClass = Token::class |
|
46
|
|
|
) { |
|
47
|
4 |
|
$this->setConnection($connection)->setDatabaseSchema($databaseSchema); |
|
48
|
4 |
|
$this->modelClass = $modelClass; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
* |
|
54
|
|
|
* @throws RepositoryException |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function readPassport(string $tokenValue, int $expirationInSeconds): ?array |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
2 |
|
$schema = $this->getDatabaseSchema(); |
|
60
|
2 |
|
$query = $this->getConnection()->createQueryBuilder(); |
|
61
|
2 |
|
$query = $this->addExpirationCondition( |
|
62
|
2 |
|
$query->select(['*']) |
|
63
|
2 |
|
->from($schema->getPassportView()) |
|
64
|
2 |
|
->where($schema->getTokensValueColumn() . '=' . $this->createTypedParameter($query, $tokenValue)) |
|
65
|
2 |
|
->andWhere($query->expr()->eq($this->getDatabaseSchema()->getTokensIsEnabledColumn(), "'1'")), |
|
66
|
2 |
|
$expirationInSeconds, |
|
67
|
2 |
|
$schema->getTokensValueCreatedAtColumn() |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
2 |
|
$statement = $query->execute(); |
|
71
|
1 |
|
$statement->setFetchMode(PDO::FETCH_ASSOC); |
|
72
|
1 |
|
$data = $statement->fetch(); |
|
73
|
|
|
|
|
74
|
1 |
|
$result = null; |
|
75
|
1 |
|
if ($data !== false) { |
|
76
|
1 |
|
$scopesColumn = $schema->getTokensViewScopesColumn(); |
|
77
|
1 |
|
$scopeList = $data[$scopesColumn]; |
|
78
|
1 |
|
$data[$scopesColumn] = $this->parseArray($scopeList); |
|
79
|
1 |
|
$result = $data; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
return $result; |
|
83
|
1 |
|
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (DBALException $exception) { |
|
84
|
1 |
|
$message = 'Passport reading failed.'; |
|
85
|
1 |
|
throw new RepositoryException($message, 0, $exception); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @inheritdoc |
|
91
|
|
|
*/ |
|
92
|
1 |
|
protected function getClassName(): string |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->modelClass; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
100
|
1 |
|
protected function getTableNameForReading(): string |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->getDatabaseSchema()->getTokensView(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|