|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 François Kooman <[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
|
|
|
namespace fkooman\RemoteStorage\OAuth\Storage; |
|
20
|
|
|
|
|
21
|
|
|
use fkooman\RemoteStorage\OAuth\Approval; |
|
22
|
|
|
use fkooman\RemoteStorage\OAuth\ApprovalStorageInterface; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
|
|
25
|
|
|
class PdoApprovalStorage extends PdoBaseStorage implements ApprovalStorageInterface |
|
26
|
|
|
{ |
|
27
|
|
|
public function __construct(PDO $db, $dbPrefix = '') |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($db, $dbPrefix); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function storeApproval(Approval $approval) |
|
33
|
|
|
{ |
|
34
|
|
|
$stmt = $this->db->prepare( |
|
35
|
|
|
sprintf( |
|
36
|
|
|
'INSERT INTO %s (user_id, client_id, response_type, scope) VALUES(:user_id, :client_id, :response_type, :scope)', |
|
37
|
|
|
$this->dbPrefix.'approval' |
|
38
|
|
|
) |
|
39
|
|
|
); |
|
40
|
|
|
$stmt->bindValue(':user_id', $approval->getUserId(), PDO::PARAM_STR); |
|
41
|
|
|
$stmt->bindValue(':client_id', $approval->getClientId(), PDO::PARAM_STR); |
|
42
|
|
|
$stmt->bindValue(':response_type', $approval->getResponseType(), PDO::PARAM_STR); |
|
43
|
|
|
$stmt->bindValue(':scope', $approval->getScope(), PDO::PARAM_STR); |
|
44
|
|
|
$stmt->execute(); |
|
45
|
|
|
|
|
46
|
|
|
return 1 === $stmt->rowCount(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function isApproved(Approval $approval) |
|
50
|
|
|
{ |
|
51
|
|
|
$stmt = $this->db->prepare( |
|
52
|
|
|
sprintf( |
|
53
|
|
|
'SELECT user_id, client_id, response_type, scope FROM %s WHERE user_id = :user_id AND client_id = :client_id AND response_type = :response_type AND scope = :scope', |
|
54
|
|
|
$this->dbPrefix.'approval' |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
$stmt->bindValue(':user_id', $approval->getUserId(), PDO::PARAM_STR); |
|
58
|
|
|
$stmt->bindValue(':client_id', $approval->getClientId(), PDO::PARAM_STR); |
|
59
|
|
|
$stmt->bindValue(':response_type', $approval->getResponseType(), PDO::PARAM_STR); |
|
60
|
|
|
$stmt->bindValue(':scope', $approval->getScope(), PDO::PARAM_STR); |
|
61
|
|
|
$stmt->execute(); |
|
62
|
|
|
$result = $stmt->fetch(PDO::FETCH_ASSOC); |
|
63
|
|
|
if (false === $result) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function createTableQueries($dbPrefix) |
|
71
|
|
|
{ |
|
72
|
|
|
return [ |
|
73
|
|
|
sprintf( |
|
74
|
|
|
'CREATE TABLE IF NOT EXISTS %s ( |
|
75
|
|
|
user_id VARCHAR(255) NOT NULL, |
|
76
|
|
|
client_id VARCHAR(255) NOT NULL, |
|
77
|
|
|
response_type VARCHAR(255) NOT NULL, |
|
78
|
|
|
scope VARCHAR(255) NOT NULL, |
|
79
|
|
|
UNIQUE (user_id, client_id, response_type, scope) |
|
80
|
|
|
)', |
|
81
|
|
|
$dbPrefix.'approval' |
|
82
|
|
|
), |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|