1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palladium\Mapper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* SQL code for locating identity data by token and updating last usage. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Palladium\Component\DataMapper; |
10
|
|
|
use Palladium\Entity as Entity; |
11
|
|
|
use PDO; |
12
|
|
|
|
13
|
|
|
class Identity extends DataMapper |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param Entity\Identity $entity |
18
|
|
|
*/ |
19
|
1 |
|
public function store(Entity\Identity $entity) |
20
|
|
|
{ |
21
|
1 |
|
$sql = "UPDATE {$this->table} |
22
|
|
|
SET used_on = :used, |
23
|
|
|
status = :status |
24
|
|
|
WHERE identity_id = :id"; |
25
|
|
|
|
26
|
1 |
|
$statement = $this->connection->prepare($sql); |
27
|
|
|
|
28
|
1 |
|
$statement->bindValue(':id', $entity->getId()); |
29
|
1 |
|
$statement->bindValue(':used', $entity->getLastUsed()); |
30
|
1 |
|
$statement->bindValue(':status', $entity->getStatus()); |
31
|
|
|
|
32
|
1 |
|
$statement->execute(); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Entity\Identity $entity |
38
|
|
|
*/ |
39
|
1 |
|
public function remove(Entity\Identity $entity) |
40
|
|
|
{ |
41
|
1 |
|
$sql = "DELETE FROM {$this->table} WHERE identity_id = :id"; |
42
|
1 |
|
$statement = $this->connection->prepare($sql); |
43
|
|
|
|
44
|
1 |
|
$statement->bindValue(':id', $entity->getId()); |
45
|
1 |
|
$statement->execute(); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Entity\Identity $entity |
51
|
|
|
*/ |
52
|
3 |
|
public function fetch(Entity\Identity $entity) |
53
|
|
|
|
54
|
|
|
{ |
55
|
3 |
|
if ($entity->getId()) { |
56
|
1 |
|
$this->fetchById($entity); |
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$this->fetchByToken($entity); |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
1 |
|
private function fetchById(Entity\Identity $entity) |
65
|
|
|
{ |
66
|
|
|
$sql = "SELECT identity_id AS id, |
67
|
|
|
parent_id AS parentId, |
68
|
|
|
account_id AS accountId, |
69
|
|
|
status AS status, |
70
|
|
|
hash AS hash, |
71
|
|
|
token_expires_on AS tokenEndOfLife |
72
|
1 |
|
FROM {$this->table} |
73
|
|
|
WHERE identity_id = :id"; |
74
|
|
|
|
75
|
1 |
|
$statement = $this->connection->prepare($sql); |
76
|
|
|
|
77
|
1 |
|
$statement->bindValue(':id', $entity->getId()); |
78
|
|
|
|
79
|
1 |
|
$statement->execute(); |
80
|
|
|
|
81
|
1 |
|
$data = $statement->fetch(PDO::FETCH_ASSOC); |
82
|
|
|
|
83
|
1 |
|
if ($data) { |
84
|
1 |
|
$this->applyValues($entity, $data); |
85
|
|
|
} |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
2 |
|
private function fetchByToken(Entity\Identity $entity) |
90
|
|
|
{ |
91
|
|
|
$sql = "SELECT identity_id AS id, |
92
|
|
|
parent_id AS parentId, |
93
|
|
|
account_id AS accountId, |
94
|
|
|
status AS status, |
95
|
|
|
hash AS hash, |
96
|
|
|
token_expires_on AS tokenEndOfLife, |
97
|
|
|
token_payload AS tokenPayload |
98
|
2 |
|
FROM {$this->table} |
99
|
|
|
WHERE token = :token |
100
|
|
|
AND token_action = :action |
101
|
|
|
AND token_expires_on > :expires"; |
102
|
|
|
|
103
|
2 |
|
$statement = $this->connection->prepare($sql); |
104
|
|
|
|
105
|
2 |
|
$statement->bindValue(':token', $entity->getToken()); |
106
|
2 |
|
$statement->bindValue(':action', $entity->getTokenAction()); |
107
|
2 |
|
$statement->bindValue(':expires', $entity->getTokenEndOfLife()); |
108
|
|
|
|
109
|
2 |
|
$statement->execute(); |
110
|
|
|
|
111
|
2 |
|
$data = $statement->fetch(PDO::FETCH_ASSOC); |
112
|
|
|
|
113
|
2 |
|
if ($data) { |
114
|
2 |
|
if ($data['tokenPayload'] !== null) { |
115
|
1 |
|
$data['tokenPayload'] = json_decode($data['tokenPayload'], true); |
116
|
|
|
} |
117
|
2 |
|
$this->applyValues($entity, $data); |
118
|
|
|
} |
119
|
2 |
|
} |
120
|
|
|
} |
121
|
|
|
|