|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mafd16\User; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\Configure\ConfigureInterface; |
|
6
|
|
|
use \Anax\Configure\ConfigureTrait; |
|
7
|
|
|
use \Anax\DI\InjectionAwareInterface; |
|
8
|
|
|
use \Anax\Di\InjectionAwareTrait; |
|
9
|
|
|
use \Anax\User\HTMLForm\UserLoginForm; |
|
10
|
|
|
use \Anax\User\HTMLForm\CreateUserForm; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A controller class. |
|
14
|
|
|
*/ |
|
15
|
|
|
class UserController implements |
|
16
|
|
|
ConfigureInterface, |
|
17
|
|
|
InjectionAwareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use ConfigureTrait, |
|
20
|
|
|
InjectionAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Protected variables |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $view; |
|
28
|
|
|
protected $pageRender; |
|
29
|
|
|
protected $request; |
|
30
|
|
|
protected $response; |
|
31
|
|
|
protected $session; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Set services to variables |
|
36
|
|
|
* |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->view = $this->di->get("view"); |
|
41
|
|
|
$this->pageRender = $this->di->get("pageRender"); |
|
42
|
|
|
$this->request = $this->di->get("request"); |
|
43
|
|
|
$this->response = $this->di->get("response"); |
|
44
|
|
|
$this->session = $this->di->get("session"); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Description. |
|
50
|
|
|
* |
|
51
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
52
|
|
|
* |
|
53
|
|
|
* @throws Exception |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function getIndex() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$title = "A index page"; |
|
60
|
|
|
$data = [ |
|
61
|
|
|
"content" => "An index page", |
|
62
|
|
|
]; |
|
63
|
|
|
$this->view->add("default2/article", $data); |
|
64
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Description. |
|
70
|
|
|
* |
|
71
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
72
|
|
|
* |
|
73
|
|
|
* @throws Exception |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
View Code Duplication |
public function getLogin() |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$title = "A login page"; |
|
80
|
|
|
$data = [ |
|
81
|
|
|
"message" => "", |
|
82
|
|
|
]; |
|
83
|
|
|
$this->view->add("user/crud/login", $data); |
|
84
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Description. |
|
90
|
|
|
* |
|
91
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
92
|
|
|
* |
|
93
|
|
|
* @throws Exception |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
public function getCreateUser($message = null) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$title = "A create user page"; |
|
100
|
|
|
$data = [ |
|
101
|
|
|
"message" => $message, |
|
102
|
|
|
]; |
|
103
|
|
|
$this->view->add("user/crud/create", $data); |
|
104
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Description. |
|
110
|
|
|
* |
|
111
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
112
|
|
|
* |
|
113
|
|
|
* @throws Exception |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
public function postCreatingUser() |
|
118
|
|
|
{ |
|
119
|
|
|
// Get POST-variables |
|
120
|
|
|
$acronym = $this->request->getPost("name"); |
|
121
|
|
|
$email = $this->request->getPost("email"); |
|
122
|
|
|
$password = $this->request->getPost("password"); |
|
123
|
|
|
$passwordagain = $this->request->getPost("passwordagain"); |
|
124
|
|
|
|
|
125
|
|
|
if ($password !== $passwordagain) { |
|
126
|
|
|
$message = "<p>Passwords did not match!</p>"; |
|
127
|
|
|
$this->getCreateUser($message); |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
$newUser = (object) [ |
|
131
|
|
|
"acronym" => $acronym, |
|
132
|
|
|
"password" => $password, |
|
133
|
|
|
"email" => $email, |
|
134
|
|
|
]; |
|
135
|
|
|
$createdUser = $this->di->get("user")->createUser($newUser); |
|
136
|
|
|
if (!$createdUser) { |
|
137
|
|
|
$message = "<p>User " . $acronym . " already exists!</p>"; |
|
138
|
|
|
$this->getCreateUser($message); |
|
139
|
|
|
return; |
|
140
|
|
|
} |
|
141
|
|
|
// Save user to session |
|
142
|
|
|
$this->di->get("user")->saveToSession($createdUser); |
|
143
|
|
|
// Redirect back to profile |
|
144
|
|
|
$this->response->redirect("user/profile"); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Description. |
|
150
|
|
|
* |
|
151
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
152
|
|
|
* |
|
153
|
|
|
* @throws Exception |
|
154
|
|
|
* |
|
155
|
|
|
* @return void |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getUserProfile() |
|
158
|
|
|
{ |
|
159
|
|
|
$title = "A profile page"; |
|
160
|
|
|
// Get user from db |
|
161
|
|
|
$id = $this->session->get("my_user_id"); |
|
162
|
|
|
$user = $this->di->get("user")->getUserFromDatabase("id", $id); |
|
163
|
|
|
|
|
164
|
|
|
$data = [ |
|
165
|
|
|
"user" => $user, |
|
166
|
|
|
]; |
|
167
|
|
|
$this->view->add("user/crud/profile", $data); |
|
168
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Description. |
|
174
|
|
|
* |
|
175
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
176
|
|
|
* |
|
177
|
|
|
* @throws Exception |
|
178
|
|
|
* |
|
179
|
|
|
* @return void |
|
180
|
|
|
*/ |
|
181
|
|
|
public function validateUser() |
|
182
|
|
|
{ |
|
183
|
|
|
// Get POST-variables |
|
184
|
|
|
$acronym = $this->request->getPost("name"); |
|
185
|
|
|
$password = $this->request->getPost("password"); |
|
186
|
|
|
// Get the user from DB |
|
187
|
|
|
$user = $this->di->get("user")->getUserFromDatabase("acronym", $acronym); |
|
188
|
|
|
|
|
189
|
|
|
if ($user->deleted) { |
|
190
|
|
|
$title = "A login page"; |
|
191
|
|
|
$data = [ |
|
192
|
|
|
"message" => $user->acronym . " were deleted " . $user->deleted, |
|
193
|
|
|
]; |
|
194
|
|
|
$this->view->add("user/crud/login", $data); |
|
195
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
// Validate password against database |
|
199
|
|
|
$valid = $user->verifyPassword($acronym, $password); |
|
200
|
|
|
// if true, save user id to session and goto profile |
|
201
|
|
|
if ($valid) { |
|
202
|
|
|
// Save user id to session |
|
203
|
|
|
$this->di->get("user")->saveToSession($user); |
|
204
|
|
|
$this->response->redirect("user/profile"); |
|
205
|
|
|
} else { |
|
206
|
|
|
// if false goto login |
|
207
|
|
|
$title = "A login page"; |
|
208
|
|
|
$data = [ |
|
209
|
|
|
"acronym" => $acronym, |
|
210
|
|
|
"password" => $password, |
|
211
|
|
|
"valid" => $valid, |
|
212
|
|
|
"message" => "Name or password was incorrect!", |
|
213
|
|
|
]; |
|
214
|
|
|
$this->view->add("user/crud/login", $data); |
|
215
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Description. |
|
222
|
|
|
* |
|
223
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
224
|
|
|
* |
|
225
|
|
|
* @throws Exception |
|
226
|
|
|
* |
|
227
|
|
|
* @return void |
|
228
|
|
|
*/ |
|
229
|
|
View Code Duplication |
public function updateGetUserProfile($message = null) |
|
|
|
|
|
|
230
|
|
|
{ |
|
231
|
|
|
$title = "Update user"; |
|
232
|
|
|
// Get user from db |
|
233
|
|
|
$id = $this->session->get("my_user_id"); |
|
234
|
|
|
$user = $this->di->get("user")->getUserFromDatabase("id", $id); |
|
235
|
|
|
$data = [ |
|
236
|
|
|
"user" => $user, |
|
237
|
|
|
"message" => $message, |
|
238
|
|
|
]; |
|
239
|
|
|
$this->view->add("user/crud/update", $data); |
|
240
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Description. |
|
246
|
|
|
* |
|
247
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
248
|
|
|
* |
|
249
|
|
|
* @throws Exception |
|
250
|
|
|
* |
|
251
|
|
|
* @return void |
|
252
|
|
|
*/ |
|
253
|
|
|
public function updatePostUserProfile() |
|
254
|
|
|
{ |
|
255
|
|
|
// Get POST-variables |
|
256
|
|
|
$email = $this->request->getPost("email"); |
|
257
|
|
|
$password = $this->request->getPost("password"); |
|
258
|
|
|
$passwordagain = $this->request->getPost("passwordagain"); |
|
259
|
|
|
|
|
260
|
|
|
if ($password !== $passwordagain) { |
|
261
|
|
|
$message = "<p>Passwords did not match!</p>"; |
|
262
|
|
|
$this->updateGetUserProfile($message); |
|
263
|
|
|
} |
|
264
|
|
|
// Get user id from session |
|
265
|
|
|
$id = $this->session->get("my_user_id"); |
|
266
|
|
|
// Update user |
|
267
|
|
|
$update = (object) [ |
|
268
|
|
|
"password" => $password, |
|
269
|
|
|
"email" => $email, |
|
270
|
|
|
]; |
|
271
|
|
|
$user = $this->di->get("user")->updateUserInDatabase($id, $update); |
|
272
|
|
|
$this->di->get("user")->saveToSession($user); |
|
273
|
|
|
// Redirect back to profile |
|
274
|
|
|
$this->response->redirect("user/profile"); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Description. |
|
280
|
|
|
* |
|
281
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
282
|
|
|
* |
|
283
|
|
|
* @throws Exception |
|
284
|
|
|
* |
|
285
|
|
|
* @return void |
|
286
|
|
|
*/ |
|
287
|
|
|
public function getLogout() |
|
288
|
|
|
{ |
|
289
|
|
|
$this->di->get("user")->logoutUser(); |
|
290
|
|
|
// Redirect back to login |
|
291
|
|
|
$this->response->redirect("user/login"); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Description. |
|
297
|
|
|
* |
|
298
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
299
|
|
|
* |
|
300
|
|
|
* @throws Exception |
|
301
|
|
|
* |
|
302
|
|
|
* @return void |
|
303
|
|
|
*/ |
|
304
|
|
|
public function getAdmin() |
|
305
|
|
|
{ |
|
306
|
|
|
$title = "An admin page"; |
|
307
|
|
|
// Get users from db |
|
308
|
|
|
$user = new User(); |
|
309
|
|
|
$user->setDb($this->di->get("db")); |
|
310
|
|
|
$data = [ |
|
311
|
|
|
"users" => $user->findAll(), |
|
312
|
|
|
]; |
|
313
|
|
|
$this->view->add("user/admin/index", $data); |
|
314
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Description. |
|
320
|
|
|
* |
|
321
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
322
|
|
|
* |
|
323
|
|
|
* @throws Exception |
|
324
|
|
|
* |
|
325
|
|
|
* @return void |
|
326
|
|
|
*/ |
|
327
|
|
View Code Duplication |
public function getAdminUpdateUser($message = "", $userId = null) |
|
|
|
|
|
|
328
|
|
|
{ |
|
329
|
|
|
$title = "Admin update user"; |
|
330
|
|
|
// Get user id from GET variable |
|
331
|
|
|
$userId = isset($userId) ? $userId : $this->request->getGet("id"); |
|
332
|
|
|
|
|
333
|
|
|
$user = $this->di->get("user")->getUserFromDatabase("id", $userId); |
|
334
|
|
|
$data = [ |
|
335
|
|
|
"user" => $user, |
|
336
|
|
|
"message" => $message, |
|
337
|
|
|
]; |
|
338
|
|
|
$this->view->add("user/admin/update", $data); |
|
339
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Description. |
|
345
|
|
|
* |
|
346
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
347
|
|
|
* |
|
348
|
|
|
* @throws Exception |
|
349
|
|
|
* |
|
350
|
|
|
* @return void |
|
351
|
|
|
*/ |
|
352
|
|
View Code Duplication |
public function postAdminUpdateUser() |
|
|
|
|
|
|
353
|
|
|
{ |
|
354
|
|
|
// Get POST-variables |
|
355
|
|
|
$email = $this->request->getPost("email"); |
|
356
|
|
|
$password = $this->request->getPost("password"); |
|
357
|
|
|
$passwordagain = $this->request->getPost("passwordagain"); |
|
358
|
|
|
$admin = $this->request->getPost("admin"); |
|
359
|
|
|
$userId = $this->request->getPost("user_id"); |
|
360
|
|
|
if ($password !== $passwordagain) { |
|
361
|
|
|
$message = "<p>Passwords did not match!</p>"; |
|
362
|
|
|
$this->getAdminUpdateUser($message, $userId); |
|
363
|
|
|
} |
|
364
|
|
|
// Update user |
|
365
|
|
|
$update = (object) [ |
|
366
|
|
|
"password" => $password, |
|
367
|
|
|
"email" => $email, |
|
368
|
|
|
"admin" => $admin, |
|
369
|
|
|
]; |
|
370
|
|
|
$this->di->get("user")->updateUserInDatabase($userId, $update); |
|
371
|
|
|
// Redirect back to admin page |
|
372
|
|
|
$this->response->redirect("user/admin"); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
|
|
376
|
|
|
/** |
|
377
|
|
|
* Description. |
|
378
|
|
|
* |
|
379
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
380
|
|
|
* |
|
381
|
|
|
* @throws Exception |
|
382
|
|
|
* |
|
383
|
|
|
* @return void |
|
384
|
|
|
*/ |
|
385
|
|
View Code Duplication |
public function getAdminCreateUser($message = null) |
|
|
|
|
|
|
386
|
|
|
{ |
|
387
|
|
|
$title = "Admin create user page"; |
|
388
|
|
|
$data = [ |
|
389
|
|
|
"message" => $message, |
|
390
|
|
|
]; |
|
391
|
|
|
$this->view->add("user/admin/create", $data); |
|
392
|
|
|
$this->pageRender->renderPage(["title" => $title]); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Description. |
|
398
|
|
|
* |
|
399
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
400
|
|
|
* |
|
401
|
|
|
* @throws Exception |
|
402
|
|
|
* |
|
403
|
|
|
* @return void |
|
404
|
|
|
*/ |
|
405
|
|
View Code Duplication |
public function postAdminCreateUser() |
|
|
|
|
|
|
406
|
|
|
{ |
|
407
|
|
|
// Get POST-variables |
|
408
|
|
|
$acronym = $this->request->getPost("name"); |
|
409
|
|
|
$email = $this->request->getPost("email"); |
|
410
|
|
|
$admin = $this->request->getPost("admin"); |
|
411
|
|
|
$password = $this->request->getPost("password"); |
|
412
|
|
|
$passwordagain = $this->request->getPost("passwordagain"); |
|
413
|
|
|
if ($password !== $passwordagain) { |
|
414
|
|
|
$message = "<p>Passwords did not match!</p>"; |
|
415
|
|
|
$this->getAdminCreateUser($message); |
|
416
|
|
|
return; |
|
417
|
|
|
} |
|
418
|
|
|
// Create new user |
|
419
|
|
|
$newUser = (object) [ |
|
420
|
|
|
"acronym" => $acronym, |
|
421
|
|
|
"password" => $password, |
|
422
|
|
|
"email" => $email, |
|
423
|
|
|
"admin" => $admin, |
|
424
|
|
|
]; |
|
425
|
|
|
$this->di->get("user")->createUser($newUser); |
|
426
|
|
|
// Redirect back to admin |
|
427
|
|
|
$this->response->redirect("user/admin"); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* Description. |
|
433
|
|
|
* |
|
434
|
|
|
* @param datatype $variable Description |
|
|
|
|
|
|
435
|
|
|
* |
|
436
|
|
|
* @throws Exception |
|
437
|
|
|
* |
|
438
|
|
|
* @return void |
|
439
|
|
|
*/ |
|
440
|
|
|
public function getAdminDeleteUser() |
|
441
|
|
|
{ |
|
442
|
|
|
// Get user to delete from GET variable |
|
443
|
|
|
$userToDelete = $this->request->getGet("id"); |
|
444
|
|
|
// Delete user |
|
445
|
|
|
$this->di->get("user")->deleteUser($userToDelete); |
|
446
|
|
|
// Redirect back to admin |
|
447
|
|
|
$this->response->redirect("user/admin"); |
|
448
|
|
|
} |
|
449
|
|
|
} |
|
450
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.