|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpDraft\Domain\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Silex; |
|
6
|
|
|
use Silex\Application; |
|
7
|
|
|
use PhpDraft\Domain\Entities\LoginUser; |
|
8
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
9
|
|
|
|
|
10
|
|
|
class LoginUserRepository { |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Silex\Application Application |
|
13
|
|
|
*/ |
|
14
|
|
|
private $app; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(Application $app) { |
|
17
|
|
|
$this->app = $app; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function Load($email) { |
|
21
|
|
|
$user = new LoginUser(); |
|
22
|
|
|
$email = strtolower($email); |
|
23
|
|
|
|
|
24
|
|
|
$load_stmt = $this->app['db']->prepare("SELECT * FROM users WHERE email = ? LIMIT 1"); |
|
25
|
|
|
$load_stmt->setFetchMode(\PDO::FETCH_INTO, $user); |
|
26
|
|
|
$load_stmt->bindParam(1, $email); |
|
27
|
|
|
|
|
28
|
|
|
if (!$load_stmt->execute()) { |
|
29
|
|
|
throw new \Exception(sprintf('Email "%s" does not exist.', $email)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (!$load_stmt->fetch()) { |
|
33
|
|
|
throw new \Exception(sprintf('Email "%s" does not exist.', $email)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $user; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function LoadById($id) { |
|
40
|
|
|
$user = new LoginUser(); |
|
41
|
|
|
|
|
42
|
|
|
$id = (int)$id; |
|
43
|
|
|
|
|
44
|
|
|
$load_stmt = $this->app['db']->prepare("SELECT * FROM users WHERE id = ? LIMIT 1"); |
|
45
|
|
|
$load_stmt->setFetchMode(\PDO::FETCH_INTO, $user); |
|
46
|
|
|
$load_stmt->bindParam(1, $id); |
|
47
|
|
|
|
|
48
|
|
|
if (!$load_stmt->execute()) { |
|
49
|
|
|
throw new \Exception(sprintf('User #%s does not exist.', $id)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (!$load_stmt->fetch()) { |
|
53
|
|
|
throw new \Exception(sprintf('User #%s does not exist.', $id)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $user; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function LoadPublicById($id) { |
|
60
|
|
|
$user = new LoginUser(); |
|
61
|
|
|
|
|
62
|
|
|
$id = (int)$id; |
|
63
|
|
|
|
|
64
|
|
|
$load_stmt = $this->app['db']->prepare("SELECT id, name FROM users WHERE id = ? LIMIT 1"); |
|
65
|
|
|
$load_stmt->setFetchMode(\PDO::FETCH_INTO, $user); |
|
66
|
|
|
$load_stmt->bindParam(1, $id); |
|
67
|
|
|
|
|
68
|
|
|
if (!$load_stmt->execute()) { |
|
69
|
|
|
throw new \Exception(sprintf('User #%s does not exist.', $id)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (!$load_stmt->fetch()) { |
|
73
|
|
|
throw new \Exception(sprintf('User #%s does not exist.', $id)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
unset($user->enabled); |
|
77
|
|
|
unset($user->email); |
|
78
|
|
|
unset($user->password); |
|
79
|
|
|
unset($user->salt); |
|
80
|
|
|
unset($user->roles); |
|
81
|
|
|
unset($user->verificationKey); |
|
82
|
|
|
|
|
83
|
|
|
$user = $this->_ScrubUser($user); |
|
84
|
|
|
|
|
85
|
|
|
return $user; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function LoadAll() { |
|
89
|
|
|
$load_stmt = $this->app['db']->prepare("SELECT * FROM users"); |
|
90
|
|
|
$load_stmt->setFetchMode(\PDO::FETCH_CLASS, 'PhpDraft\Domain\Entities\LoginUser'); |
|
91
|
|
|
|
|
92
|
|
|
$users = array(); |
|
93
|
|
|
|
|
94
|
|
|
if (!$load_stmt->execute()) { |
|
95
|
|
|
throw new \Exception("Unable to load users."); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
while ($user = $load_stmt->fetch()) { |
|
99
|
|
|
|
|
100
|
|
|
$users[] = $this->_ScrubUser($user); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $users; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function Create(LoginUser $user) { |
|
107
|
|
|
$insert_stmt = $this->app['db']->prepare("INSERT INTO users |
|
108
|
|
|
(id, email, password, salt, name, roles, verificationKey, creationTime) |
|
109
|
|
|
VALUES |
|
110
|
|
|
(NULL, ?, ?, ?, ?, ?, ?, UTC_TIMESTAMP())"); |
|
111
|
|
|
|
|
112
|
|
|
$email = strtolower($user->email); |
|
113
|
|
|
$roles = implode(',', $user->roles); |
|
114
|
|
|
|
|
115
|
|
|
$insert_stmt->bindParam(1, $email); |
|
116
|
|
|
$insert_stmt->bindParam(2, $user->password); |
|
117
|
|
|
$insert_stmt->bindParam(3, $user->salt); |
|
118
|
|
|
$insert_stmt->bindParam(4, $user->name); |
|
119
|
|
|
$insert_stmt->bindParam(5, $roles); |
|
120
|
|
|
$insert_stmt->bindParam(6, $user->verificationKey); |
|
121
|
|
|
|
|
122
|
|
|
if (!$insert_stmt->execute()) { |
|
123
|
|
|
throw new \Exception("Unable to create user."); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$user->id = (int)$this->app['db']->lastInsertId(); |
|
127
|
|
|
|
|
128
|
|
|
return $user; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function Update(LoginUser $user) { |
|
132
|
|
|
$update_stmt = $this->app['db']->prepare("UPDATE users |
|
133
|
|
|
SET email = ?, password = ?, salt = ?, |
|
134
|
|
|
name = ?, roles = ?, verificationKey = ?, enabled = ? |
|
135
|
|
|
WHERE id = ?"); |
|
136
|
|
|
|
|
137
|
|
|
$update_stmt->bindParam(1, $user->email); |
|
138
|
|
|
$update_stmt->bindParam(2, $user->password); |
|
139
|
|
|
$update_stmt->bindParam(3, $user->salt); |
|
140
|
|
|
$update_stmt->bindParam(4, $user->name); |
|
141
|
|
|
$update_stmt->bindParam(5, $user->roles); |
|
142
|
|
|
$update_stmt->bindParam(6, $user->verificationKey); |
|
143
|
|
|
$update_stmt->bindParam(7, $user->enabled); |
|
144
|
|
|
$update_stmt->bindParam(8, $user->id); |
|
145
|
|
|
|
|
146
|
|
|
$result = $update_stmt->execute(); |
|
147
|
|
|
|
|
148
|
|
|
if ($result == false) { |
|
149
|
|
|
throw new \Exception("Unable to update user."); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $user; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function EraseVerificationKey($user_email) { |
|
156
|
|
|
$update_stmt = $this->app['db']->prepare("UPDATE users |
|
157
|
|
|
SET verificationKey = NULL |
|
158
|
|
|
WHERE email = ?"); |
|
159
|
|
|
|
|
160
|
|
|
$update_stmt->bindParam(1, $user_email); |
|
161
|
|
|
|
|
162
|
|
|
$result = $update_stmt->execute(); |
|
163
|
|
|
|
|
164
|
|
|
if ($result == false) { |
|
165
|
|
|
throw new \Exception("Unable to erase verification key for user."); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function Delete(LoginUser $user) { |
|
172
|
|
|
$delete_stmt = $this->app['db']->prepare("DELETE FROM users WHERE id = ?"); |
|
173
|
|
|
$delete_stmt->bindParam(1, $user->id); |
|
174
|
|
|
|
|
175
|
|
|
if (!$delete_stmt->execute()) { |
|
176
|
|
|
throw new \Exception("Unable to delete user #$user->id"); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function NameIsUnique($name, $id = null) { |
|
183
|
|
|
$name = strtolower($name); |
|
184
|
|
|
if ($id == null) { |
|
185
|
|
|
$name_stmt = $this->app['db']->prepare("SELECT name FROM users WHERE name LIKE ?"); |
|
186
|
|
|
$name_stmt->bindParam(1, $name); |
|
187
|
|
|
} else { |
|
188
|
|
|
$name_stmt = $this->app['db']->prepare("SELECT name FROM users WHERE name LIKE ? AND id <> ?"); |
|
189
|
|
|
$name_stmt->bindParam(1, $name); |
|
190
|
|
|
$name_stmt->bindParam(2, $id); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if (!$name_stmt->execute()) { |
|
194
|
|
|
throw new \Exception(sprintf('Name %s is invalid', $name)); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $name_stmt->rowCount() == 0; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function EmailExists($email, $id = null) { |
|
201
|
|
|
if ($id == null) { |
|
202
|
|
|
$email_stmt = $this->app['db']->prepare("SELECT email FROM users WHERE email = ?"); |
|
203
|
|
|
$email_stmt->bindParam(1, $email); |
|
204
|
|
|
} else { |
|
205
|
|
|
$email_stmt = $this->app['db']->prepare("SELECT email FROM users WHERE email = ? AND id <> ?"); |
|
206
|
|
|
$email_stmt->bindParam(1, $email); |
|
207
|
|
|
$email_stmt->bindParam(2, $id); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if (!$email_stmt->execute()) { |
|
211
|
|
|
throw new \Exception(sprintf('Email "%s" is invalid', $email)); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return $email_stmt->rowCount() == 1; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function EmailIsUnique($email) { |
|
218
|
|
|
$email = strtolower($email); |
|
219
|
|
|
$email_stmt = $this->app['db']->prepare("SELECT email FROM users WHERE email = ? LIMIT 1"); |
|
220
|
|
|
$email_stmt->bindParam(1, $email); |
|
221
|
|
|
|
|
222
|
|
|
if (!$email_stmt->execute()) { |
|
223
|
|
|
throw new \Exception(sprintf('Email %s is invalid', $email)); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $email_stmt->rowCount() == 0; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function SearchCommissioners($searchTerm) { |
|
230
|
|
|
$searchTerm = "%$searchTerm%"; |
|
231
|
|
|
$search_stmt = $this->app['db']->prepare("SELECT id, name FROM users WHERE name LIKE ?"); |
|
232
|
|
|
|
|
233
|
|
|
$search_stmt->setFetchMode(\PDO::FETCH_CLASS, 'PhpDraft\Domain\Entities\LoginUser'); |
|
234
|
|
|
$search_stmt->bindParam(1, $searchTerm); |
|
235
|
|
|
|
|
236
|
|
|
$users = array(); |
|
237
|
|
|
|
|
238
|
|
|
if (!$search_stmt->execute()) { |
|
239
|
|
|
throw new \Exception("Unable to load users"); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
while ($user = $search_stmt->fetch()) { |
|
243
|
|
|
unset($user->enabled); |
|
244
|
|
|
unset($user->email); |
|
245
|
|
|
unset($user->password); |
|
246
|
|
|
unset($user->salt); |
|
247
|
|
|
unset($user->roles); |
|
248
|
|
|
unset($user->verificationKey); |
|
249
|
|
|
|
|
250
|
|
|
$users[] = $this->_ScrubUser($user); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $users; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function VerificationMatches($email, $verificationKey) { |
|
257
|
|
|
$email = strtolower($email); |
|
258
|
|
|
$verification_stmt = $this->app['db']->prepare("SELECT email, verificationKey FROM users WHERE email = ? AND verificationKey = ? LIMIT 1"); |
|
259
|
|
|
$verification_stmt->bindParam(1, $email); |
|
260
|
|
|
$verification_stmt->bindParam(2, $verificationKey); |
|
261
|
|
|
|
|
262
|
|
|
if (!$verification_stmt->execute()) { |
|
263
|
|
|
throw new \Exception('Verification is invalid.'); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $verification_stmt->rowCount() == 1; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function GetRoles() { |
|
270
|
|
|
$roles = array(); |
|
271
|
|
|
|
|
272
|
|
|
$roles['ROLE_COMMISH'] = "Commissioner"; |
|
273
|
|
|
$roles['ROLE_ADMIN'] = "Administrator"; |
|
274
|
|
|
|
|
275
|
|
|
return $roles; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
private function _ScrubUser(LoginUser $user) { |
|
279
|
|
|
unset($user->password); |
|
280
|
|
|
unset($user->salt); |
|
281
|
|
|
unset($user->verificationKey); |
|
282
|
|
|
|
|
283
|
|
|
return $user; |
|
284
|
|
|
} |
|
285
|
|
|
} |