|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* All of the SQL code related to the storage and retrieval of cookie-based identities. |
|
7
|
|
|
* Probably need some cleanup in `store()` method |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
use Palladium\Component\SqlMapper; |
|
11
|
|
|
use Palladium\Entity\Authentication as Entity; |
|
12
|
|
|
|
|
13
|
|
|
class CookieIdentity extends SqlMapper |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Entity\CookieIdentity $entity |
|
18
|
|
|
*/ |
|
19
|
|
|
public function exists(Entity\CookieIdentity $entity) |
|
20
|
|
|
{ |
|
21
|
|
|
$table = $this->config['accounts']['identities']; |
|
22
|
|
|
|
|
23
|
|
|
$sql = "SELECT 1 |
|
24
|
|
|
FROM $table AS Identities |
|
25
|
|
|
WHERE type = :type |
|
26
|
|
|
AND user_id = :user |
|
27
|
|
|
AND identifier = :series |
|
28
|
|
|
AND fingerprint = :fingerprint"; |
|
29
|
|
|
|
|
30
|
|
|
$statement = $this->connection->prepare($sql); |
|
31
|
|
|
|
|
32
|
|
|
$statement->bindValue(':type', $entity->getType()); |
|
33
|
|
|
$statement->bindValue(':user', $entity->getUserId()); |
|
34
|
|
|
$statement->bindValue(':series', $entity->getSeries()); |
|
35
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
36
|
|
|
|
|
37
|
|
|
$statement->execute(); |
|
38
|
|
|
|
|
39
|
|
|
$data = $statement->fetch(); |
|
40
|
|
|
|
|
41
|
|
|
return empty($data) === false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Entity\CookieIdentity $entity |
|
47
|
|
|
*/ |
|
48
|
|
|
public function fetch(Entity\CookieIdentity $entity) |
|
49
|
|
|
{ |
|
50
|
|
|
$table = $this->config['accounts']['identities']; |
|
51
|
|
|
|
|
52
|
|
|
$sql = "SELECT identity_id AS id, |
|
53
|
|
|
hash AS hash, |
|
54
|
|
|
UNIX_TIMESTAMP(expires_on) AS expiresOn |
|
55
|
|
|
FROM $table AS Identities |
|
56
|
|
|
WHERE type = :type |
|
57
|
|
|
AND user_id = :user |
|
58
|
|
|
AND identifier = :series |
|
59
|
|
|
AND fingerprint = :fingerprint |
|
60
|
|
|
AND status = :status"; |
|
61
|
|
|
|
|
62
|
|
|
$statement = $this->connection->prepare($sql); |
|
63
|
|
|
|
|
64
|
|
|
$statement->bindValue(':type', $entity->getType()); |
|
65
|
|
|
$statement->bindValue(':status', $entity->getStatus()); |
|
66
|
|
|
$statement->bindValue(':user', $entity->getUserId()); |
|
67
|
|
|
$statement->bindValue(':series', $entity->getSeries()); |
|
68
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
69
|
|
|
|
|
70
|
|
|
$statement->execute(); |
|
71
|
|
|
|
|
72
|
|
|
$data = $statement->fetch(); |
|
73
|
|
|
|
|
74
|
|
|
if ($data) { |
|
75
|
|
|
$this->applyValues($entity, $data); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
public function store(Entity\CookieIdentity $entity) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($entity->getId() === null) { |
|
83
|
|
|
$this->createCookie($entity); |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->updateCookie($entity); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
private function createCookie(Entity\CookieIdentity $entity) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$table = $this->config['accounts']['identities']; |
|
94
|
|
|
$sql = "INSERT INTO {$table} |
|
95
|
|
|
(user_id, type, status, identifier, fingerprint, hash, expires_on) |
|
96
|
|
|
VALUES (:user, :type, :status, :series, :fingerprint, :hash, FROM_UNIXTIME(:expires))"; |
|
97
|
|
|
|
|
98
|
|
|
$statement = $this->connection->prepare($sql); |
|
99
|
|
|
|
|
100
|
|
|
$statement->bindValue(':user', $entity->getUserId()); |
|
101
|
|
|
$statement->bindValue(':type', $entity->getType()); |
|
102
|
|
|
$statement->bindValue(':status', $entity->getStatus()); |
|
103
|
|
|
$statement->bindValue(':series', $entity->getSeries()); |
|
104
|
|
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
105
|
|
|
$statement->bindValue(':hash', $entity->getHash()); |
|
106
|
|
|
$statement->bindValue(':expires', $entity->getExpiresOn()); |
|
107
|
|
|
|
|
108
|
|
|
$statement->execute(); |
|
109
|
|
|
|
|
110
|
|
|
$entity->setId($this->connection->lastInsertId()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
View Code Duplication |
private function updateCookie(Entity\CookieIdentity $entity) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$table = $this->config['accounts']['identities']; |
|
117
|
|
|
$active = Entity\Identity::STATUS_ACTIVE; |
|
118
|
|
|
|
|
119
|
|
|
$sql = "UPDATE {$table} |
|
120
|
|
|
SET status = :status, |
|
121
|
|
|
hash = :hash, |
|
122
|
|
|
used_on = NOW(), |
|
123
|
|
|
expires_on = FROM_UNIXTIME(:expires) |
|
124
|
|
|
WHERE identity_id = :id |
|
125
|
|
|
AND status = {$active}"; |
|
126
|
|
|
|
|
127
|
|
|
$statement = $this->connection->prepare($sql); |
|
128
|
|
|
|
|
129
|
|
|
$statement->bindValue(':id', $entity->getId()); |
|
130
|
|
|
$statement->bindValue(':status', $entity->getStatus()); |
|
131
|
|
|
$statement->bindValue(':hash', $entity->getHash()); |
|
132
|
|
|
$statement->bindValue(':expires', $entity->getExpiresOn()); |
|
133
|
|
|
|
|
134
|
|
|
$statement->execute(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
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.