|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\User\HTMLForm; |
|
4
|
|
|
|
|
5
|
|
|
use \Anax\HTMLForm\FormModel; |
|
6
|
|
|
use \Anax\DI\DIInterface; |
|
7
|
|
|
use \Anax\User\User; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Example of FormModel implementation. |
|
11
|
|
|
*/ |
|
12
|
|
|
class UserLoginForm extends FormModel |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor injects with DI container. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Anax\DI\DIInterface $di a service container |
|
18
|
|
|
*/ |
|
19
|
1 |
View Code Duplication |
public function __construct(DIInterface $di) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
1 |
|
parent::__construct($di); |
|
22
|
|
|
|
|
23
|
1 |
|
$this->form->create( |
|
24
|
|
|
[ |
|
25
|
1 |
|
"id" => __CLASS__, |
|
26
|
|
|
"legend" => "Logga in" |
|
27
|
1 |
|
], |
|
28
|
|
|
[ |
|
29
|
|
|
"user" => [ |
|
30
|
1 |
|
"type" => "text", |
|
31
|
|
|
//"description" => "Here you can place a description.", |
|
32
|
|
|
//"placeholder" => "Here is a placeholder", |
|
33
|
1 |
|
], |
|
34
|
|
|
|
|
35
|
|
|
"password" => [ |
|
36
|
1 |
|
"type" => "password", |
|
37
|
|
|
//"description" => "Here you can place a description.", |
|
38
|
|
|
//"placeholder" => "Here is a placeholder", |
|
39
|
1 |
|
], |
|
40
|
|
|
|
|
41
|
|
|
"submit" => [ |
|
42
|
1 |
|
"type" => "submit", |
|
43
|
1 |
|
"value" => "Login", |
|
44
|
1 |
|
"callback" => [$this, "callbackSubmit"] |
|
45
|
1 |
|
], |
|
46
|
|
|
] |
|
47
|
1 |
|
); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Callback for submit-button which should return true if it could |
|
54
|
|
|
* carry out its work and false if something failed. |
|
55
|
|
|
* |
|
56
|
|
|
* @return boolean true if okey, false if something went wrong. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function callbackSubmit() |
|
59
|
|
|
{ |
|
60
|
|
|
// Get values from the submitted form |
|
61
|
|
|
$acronym = $this->form->value("user"); |
|
62
|
|
|
$password = $this->form->value("password"); |
|
63
|
|
|
|
|
64
|
|
|
// Try to login |
|
65
|
|
|
// $db = $this->di->get("db"); |
|
66
|
|
|
// $db->connect(); |
|
67
|
|
|
// $user = $db->select("password") |
|
68
|
|
|
// ->from("User") |
|
69
|
|
|
// ->where("acronym = ?") |
|
70
|
|
|
// ->executeFetch([$acronym]); |
|
71
|
|
|
// |
|
72
|
|
|
// // $user is false if user is not found |
|
73
|
|
|
// if (!$user || !password_verify($password, $user->password)) { |
|
74
|
|
|
// $this->form->rememberValues(); |
|
75
|
|
|
// $this->form->addOutput("User or password did not match."); |
|
76
|
|
|
// return false; |
|
77
|
|
|
// } |
|
78
|
|
|
|
|
79
|
|
|
$user = new User(); |
|
80
|
|
|
$user->setDb($this->di->get("db")); |
|
81
|
|
|
$res = $user->verifyPassword($acronym, $password); |
|
82
|
|
|
|
|
83
|
|
|
if (!$res) { |
|
84
|
|
|
$this->form->rememberValues(); |
|
85
|
|
|
$this->form->addOutput("User or password did not match."); |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->di->get("loginController")->loginUser($user->acronym); |
|
90
|
|
|
|
|
91
|
|
|
$this->form->addOutput("Användare " . $user->acronym . " loggade in."); |
|
92
|
|
|
|
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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.