Issues (67)

src/User/HTMLForm/UserLoginForm.php (3 issues)

1
<?php
2
3
namespace Alfs18\User\HTMLForm;
4
5
use Alfs18\User\User;
6
use Anax\HTMLForm\FormModel;
7
use Psr\Container\ContainerInterface;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class UserLoginForm extends FormModel
13
{
14
    /**
15
     * Constructor injects with DI container.
16
     *
17
     * @param Psr\Container\ContainerInterface $di a service container
0 ignored issues
show
The type Alfs18\User\HTMLForm\Psr...iner\ContainerInterface was not found. Did you mean Psr\Container\ContainerInterface? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct(ContainerInterface $di)
20
    {
21
        parent::__construct($di);
22
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
                "legend" => "User Login",
27
                "class" => "login"
28
            ],
29
            [
30
                "user" => [
31
                    "type"        => "text",
32
                    //"description" => "Here you can place a description.",
33
                    //"placeholder" => "Here is a placeholder",
34
                ],
35
36
                "password" => [
37
                    "type"        => "password",
38
                    //"description" => "Here you can place a description.",
39
                    //"placeholder" => "Here is a placeholder",
40
                ],
41
42
                "submit" => [
43
                    "type" => "submit",
44
                    "value" => "Login",
45
                    "callback" => [$this, "callbackSubmit"]
46
                ],
47
            ]
48
        );
49
    }
50
51
52
53
    /**
54
     * Callback for submit-button which should return true if it could
55
     * carry out its work and false if something failed.
56
     *
57
     * @return boolean true if okey, false if something went wrong.
58
     */
59
    public function callbackSubmit()
60
    {
61
        // $this->form->addOutput(
62
        //     "Trying to login as: "
63
        //     . $this->form->value("user")
64
        //     . "<br>Password is kept a secret..."
65
        //     //. $this->form->value("password")
66
        // );
67
        //
68
        // // Remember values during resubmit, useful when failing (return false)
69
        // // and asking the user to resubmit the form.
70
        // $this->form->rememberValues();
71
        //
72
        // return true;
73
74
        $res = new User();
75
        // Get values from the submitted form
76
77
        $acronym       = $res->changeCharacter($this->form->value("user"));
78
        $password      = $res->changeCharacter($this->form->value("password"));
79
80
81
        // Try to login
82
        $db = $this->di->get("dbqb");
83
        $db->connect();
84
        $user = $db->select("password")
85
                   ->from("User")
86
                   ->where("acronym = ?")
87
                   ->execute([$acronym])
88
                   ->fetch();
89
90
        // $user is null if user is not found
91
        if (!$user || !password_verify($password, $user->password)) {
0 ignored issues
show
$password of type array is incompatible with the type string expected by parameter $password of password_verify(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        if (!$user || !password_verify(/** @scrutinizer ignore-type */ $password, $user->password)) {
Loading history...
92
            var_dump($acronym);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($acronym) looks like debug code. Are you sure you do not want to remove it?
Loading history...
93
            var_dump($password);
94
           $this->form->rememberValues();
95
           $this->form->addOutput("User $acronym or password $password did not match.");
96
           return false;
97
        }
98
99
        // $_SESSION["status"] = "Logga ut";
100
        // $_SESSION["status_url"] = "user/logout";
101
        $_SESSION["status"] = [
102
            "text" => "Profil",
103
            "url" => "user/profile",
104
            "title" => "Profil",
105
            "submenu" => [
106
                "items" => [
107
                    [
108
                        "text" => "Logga ut",
109
                        "url" => "user/logout",
110
                        "title" => "Logga ut",
111
                    ],
112
                ],
113
            ],
114
        ];
115
        $_SESSION["acronym"] = $acronym;
116
        // $this->form->addOutput("User logged in.");
117
        return true;
118
    }
119
}
120