|
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\Authentication as Entity; |
|
11
|
|
|
|
|
12
|
|
|
class PasswordIdentity extends SqlMapper |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param Entity\PasswordIdentity $entity |
|
17
|
|
|
*/ |
|
18
|
|
|
public function exists(Entity\PasswordIdentity $entity) |
|
19
|
|
|
{ |
|
20
|
|
|
$table = $this->config['accounts']['identities']; |
|
21
|
|
|
|
|
22
|
|
|
$sql = "SELECT 1 |
|
23
|
|
|
FROM {$table} |
|
24
|
|
|
WHERE type = :type |
|
25
|
|
|
AND fingerprint = :fingerprint |
|
26
|
|
|
AND identifier = :identifier |
|
27
|
|
|
AND (expires_on IS NULL OR expires_on > NOW())"; |
|
28
|
|
|
|
|
29
|
|
|
$statement = $this->connection->prepare($sql); |
|
30
|
|
|
|
|
31
|
|
|
$statement->bindValue(':type', Entity\Identity::TYPE_PASSWORD); |
|
32
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
33
|
|
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
|
34
|
|
|
|
|
35
|
|
|
$statement->execute(); |
|
36
|
|
|
$data = $statement->fetch(); |
|
37
|
|
|
|
|
38
|
|
|
return empty($data) === false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Entity\PasswordIdentity $entity |
|
44
|
|
|
*/ |
|
45
|
|
View Code Duplication |
public function fetch(Entity\PasswordIdentity $entity) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$table = $this->config['accounts']['identities']; |
|
48
|
|
|
|
|
49
|
|
|
$sql = "SELECT identity_id AS id, |
|
50
|
|
|
user_id AS userId, |
|
51
|
|
|
hash AS hash, |
|
52
|
|
|
status AS status, |
|
53
|
|
|
token AS token, |
|
54
|
|
|
token_action AS tokenAction, |
|
55
|
|
|
UNIX_TIMESTAMP(token_expires_on) AS tokenEndOfLife |
|
56
|
|
|
FROM $table |
|
57
|
|
|
WHERE type = :type |
|
58
|
|
|
AND fingerprint = :fingerprint |
|
59
|
|
|
AND identifier = :identifier"; |
|
60
|
|
|
|
|
61
|
|
|
$statement = $this->connection->prepare($sql); |
|
62
|
|
|
|
|
63
|
|
|
$statement->bindValue(':type', $entity->getType()); |
|
64
|
|
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
|
65
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
66
|
|
|
|
|
67
|
|
|
$statement->execute(); |
|
68
|
|
|
|
|
69
|
|
|
$data = $statement->fetch(); |
|
70
|
|
|
|
|
71
|
|
|
if ($data) { |
|
72
|
|
|
$this->applyValues($entity, $data); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param Entity\PasswordIdentity $entity |
|
79
|
|
|
*/ |
|
80
|
|
|
public function store(Entity\PasswordIdentity $entity) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($entity->getId() === null) { |
|
83
|
|
|
$this->createIdentity($entity); |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->updateIdentity($entity); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
private function createIdentity(Entity\PasswordIdentity $entity) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$table = $this->config['accounts']['identities']; |
|
94
|
|
|
|
|
95
|
|
|
$sql = "INSERT INTO {$table} |
|
96
|
|
|
(type, status, identifier, fingerprint, hash, token, token_action, token_expires_on ) |
|
97
|
|
|
VALUES (:type, :status, :identifier, :fingerprint, :hash, :token, :action, FROM_UNIXTIME(:token_eol))"; |
|
98
|
|
|
|
|
99
|
|
|
$statement = $this->connection->prepare($sql); |
|
100
|
|
|
|
|
101
|
|
|
$statement->bindValue(':type', Entity\Identity::TYPE_PASSWORD); |
|
102
|
|
|
$statement->bindValue(':status', Entity\Identity::STATUS_NEW); |
|
103
|
|
|
$statement->bindValue(':identifier', $entity->getIdentifier()); |
|
104
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
105
|
|
|
$statement->bindValue(':hash', $entity->getHash()); |
|
106
|
|
|
$statement->bindValue(':token', $entity->getToken()); |
|
107
|
|
|
$statement->bindValue(':action', $entity->getTokenAction()); |
|
108
|
|
|
$statement->bindValue(':token_eol', $entity->getTokenEndOfLife()); |
|
109
|
|
|
|
|
110
|
|
|
$statement->execute(); |
|
111
|
|
|
|
|
112
|
|
|
$entity->setId($this->connection->lastInsertId()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
private function updateIdentity(Entity\PasswordIdentity $entity) |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
$table = $this->config['accounts']['identities']; |
|
119
|
|
|
|
|
120
|
|
|
$sql = "UPDATE {$table} |
|
121
|
|
|
SET hash = :hash, |
|
122
|
|
|
status = :status, |
|
123
|
|
|
expires_on = FROM_UNIXTIME(:expires), |
|
124
|
|
|
token = :token, |
|
125
|
|
|
token_action = :action, |
|
126
|
|
|
token_expires_on = FROM_UNIXTIME(:token_eol) |
|
127
|
|
|
WHERE identity_id = :id"; |
|
128
|
|
|
|
|
129
|
|
|
$statement = $this->connection->prepare($sql); |
|
130
|
|
|
|
|
131
|
|
|
$statement->bindValue(':id', $entity->getId()); |
|
132
|
|
|
$statement->bindValue(':hash', $entity->getHash()); |
|
133
|
|
|
$statement->bindValue(':status', $entity->getStatus()); |
|
134
|
|
|
$statement->bindValue(':expires', $entity->getExpiresOn()); |
|
135
|
|
|
$statement->bindValue(':token', $entity->getToken()); |
|
136
|
|
|
$statement->bindValue(':action', $entity->getTokenAction()); |
|
137
|
|
|
$statement->bindValue(':token_eol', $entity->getTokenEndOfLife()); |
|
138
|
|
|
|
|
139
|
|
|
$statement->execute(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.