|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SQL logic for authentication attemps using username/password |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Palladium\Component\SqlMapper; |
|
10
|
|
|
use Palladium\Entity as Entity; |
|
11
|
|
|
use Palladium\Contract\CanPersistIdentity; |
|
12
|
|
|
|
|
13
|
|
|
class EmailIdentity extends SqlMapper implements CanPersistIdentity |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Entity\Identity $entity |
|
18
|
|
|
*/ |
|
19
|
|
|
public function exists(Entity\Identity $entity) |
|
20
|
|
|
{ |
|
21
|
|
|
$table = $this->config['accounts']['identities']; |
|
22
|
|
|
|
|
23
|
|
|
$sql = "SELECT 1 |
|
24
|
|
|
FROM {$table} |
|
25
|
|
|
WHERE type = :type |
|
26
|
|
|
AND fingerprint = :fingerprint |
|
27
|
|
|
AND identifier = :identifier |
|
28
|
|
|
AND (expires_on IS NULL OR expires_on > :now)"; |
|
29
|
|
|
|
|
30
|
|
|
$statement = $this->connection->prepare($sql); |
|
31
|
|
|
|
|
32
|
|
|
$statement->bindValue(':type', Entity\Identity::TYPE_PASSWORD); |
|
33
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
|
|
|
|
|
34
|
|
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
|
|
|
|
|
|
35
|
|
|
$statement->bindValue(':now', time()); |
|
36
|
|
|
|
|
37
|
|
|
$statement->execute(); |
|
38
|
|
|
$data = $statement->fetch(); |
|
39
|
|
|
|
|
40
|
|
|
return empty($data) === false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Entity\Identity $entity |
|
46
|
|
|
*/ |
|
47
|
|
|
public function fetch(Entity\Identity $entity) |
|
48
|
|
|
{ |
|
49
|
|
|
$table = $this->config['accounts']['identities']; |
|
50
|
|
|
|
|
51
|
|
|
$sql = "SELECT identity_id AS id, |
|
52
|
|
|
user_id AS userId, |
|
53
|
|
|
hash AS hash, |
|
54
|
|
|
status AS status, |
|
55
|
|
|
token AS token, |
|
56
|
|
|
token_action AS tokenAction, |
|
57
|
|
|
token_expires_on AS tokenEndOfLife |
|
58
|
|
|
FROM $table |
|
59
|
|
|
WHERE type = :type |
|
60
|
|
|
AND fingerprint = :fingerprint |
|
61
|
|
|
AND identifier = :identifier"; |
|
62
|
|
|
|
|
63
|
|
|
$statement = $this->connection->prepare($sql); |
|
64
|
|
|
|
|
65
|
|
|
$statement->bindValue(':type', $entity->getType()); |
|
66
|
|
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
|
|
|
|
|
|
67
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$statement->execute(); |
|
70
|
|
|
|
|
71
|
|
|
$data = $statement->fetch(); |
|
72
|
|
|
|
|
73
|
|
|
if ($data) { |
|
74
|
|
|
$this->applyValues($entity, $data); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Entity\Identity $entity |
|
81
|
|
|
*/ |
|
82
|
|
|
public function store(Entity\Identity $entity) |
|
83
|
|
|
{ |
|
84
|
|
|
if ($entity->getId() === null) { |
|
85
|
|
|
$this->createIdentity($entity); |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->updateIdentity($entity); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
private function createIdentity(Entity\Identity $entity) |
|
94
|
|
|
{ |
|
95
|
|
|
$table = $this->config['accounts']['identities']; |
|
96
|
|
|
|
|
97
|
|
|
$sql = "INSERT INTO {$table} |
|
98
|
|
|
(type, status, identifier, fingerprint, hash, created_on, token, token_action, token_expires_on ) |
|
99
|
|
|
VALUES (:type, :status, :identifier, :fingerprint, :hash, :created, :token, :action, :token_eol)"; |
|
100
|
|
|
|
|
101
|
|
|
$statement = $this->connection->prepare($sql); |
|
102
|
|
|
|
|
103
|
|
|
$statement->bindValue(':type', Entity\Identity::TYPE_PASSWORD); |
|
104
|
|
|
$statement->bindValue(':status', Entity\Identity::STATUS_NEW); |
|
105
|
|
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
|
|
|
|
|
|
106
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
|
|
|
|
|
107
|
|
|
$statement->bindValue(':hash', $entity->getHash()); |
|
|
|
|
|
|
108
|
|
|
$statement->bindValue(':token', $entity->getToken()); |
|
109
|
|
|
$statement->bindValue(':action', $entity->getTokenAction()); |
|
110
|
|
|
$statement->bindValue(':token_eol', $entity->getTokenEndOfLife()); |
|
111
|
|
|
$statement->bindValue(':created', time()); |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
$statement->execute(); |
|
115
|
|
|
|
|
116
|
|
|
$entity->setId($this->connection->lastInsertId()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
View Code Duplication |
private function updateIdentity(Entity\Identity $entity) |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
$table = $this->config['accounts']['identities']; |
|
123
|
|
|
|
|
124
|
|
|
$sql = "UPDATE {$table} |
|
125
|
|
|
SET hash = :hash, |
|
126
|
|
|
status = :status, |
|
127
|
|
|
expires_on = :expires, |
|
128
|
|
|
token = :token, |
|
129
|
|
|
token_action = :action, |
|
130
|
|
|
token_expires_on = :token_eol |
|
131
|
|
|
WHERE identity_id = :id"; |
|
132
|
|
|
|
|
133
|
|
|
$statement = $this->connection->prepare($sql); |
|
134
|
|
|
|
|
135
|
|
|
$statement->bindValue(':id', $entity->getId()); |
|
136
|
|
|
$statement->bindValue(':hash', $entity->getHash()); |
|
|
|
|
|
|
137
|
|
|
$statement->bindValue(':status', $entity->getStatus()); |
|
138
|
|
|
$statement->bindValue(':expires', $entity->getExpiresOn()); |
|
139
|
|
|
$statement->bindValue(':token', $entity->getToken()); |
|
140
|
|
|
$statement->bindValue(':action', $entity->getTokenAction()); |
|
141
|
|
|
$statement->bindValue(':token_eol', $entity->getTokenEndOfLife()); |
|
142
|
|
|
|
|
143
|
|
|
$statement->execute(); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: