UserLoginForm::callbackSubmit()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 40
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.6284

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 40
ccs 10
cts 17
cp 0.5881
crap 3.6284
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\User\HTMLForm;
4
5
use \Anax\User\User;
6
use \Anax\HTMLForm\FormModel;
7
use \Anax\DI\DIInterface;
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 2
    public function __construct(DIInterface $di)
20
    {
21 2
        parent::__construct($di);
22
23 2
        $this->form->create(
24
            [
25 2
                "id" => __CLASS__,
26
                "legend" => "Logga in"
27 2
            ],
28
            [
29
                "user" => [
30 2
                    "type"        => "text",
31 2
                    "class"       => "input",
32
                    //"description" => "Here you can place a description.",
33
                    //"placeholder" => "Here is a placeholder",
34 2
                ],
35
36
                "password" => [
37 2
                    "type"        => "password",
38 2
                    "class"       => "input",
39
                    //"description" => "Here you can place a description.",
40
                    //"placeholder" => "Here is a placeholder",
41 2
                ],
42
43
                "submit" => [
44 2
                    "type" => "submit",
45 2
                    "value" => "Login",
46 2
                    "class" => "formButton",
47 2
                    "callback" => [$this, "callbackSubmit"]
48 2
                ],
49
            ]
50 2
        );
51 2
    }
52
53
54
55
    /**
56
     * Callback for submit-button which should return true if it could
57
     * carry out its work and false if something failed.
58
     *
59
     * @return boolean true if okey, false if something went wrong.
60
     */
61 1
    public function callbackSubmit()
62
    {
63
        // Get values from the submitted form
64 1
        $acronym       = $this->form->value("user");
65 1
        $password      = $this->form->value("password");
66
67
        // Try to login
68
        // $db = $this->di->get("db");
69
        // $db->connect();
70
        // $user = $db->select("password")
71
        //            ->from("User")
72
        //            ->where("acronym = ?")
73
        //            ->executeFetch([$acronym]);
74
        //
75
        // // $user is false if user is not found
76
        // if (!$user || !password_verify($password, $user->password)) {
77
        //    $this->form->rememberValues();
78
        //    $this->form->addOutput("User or password did not match.");
79
        //    return false;
80
        // }
81
82 1
        $user = new User();
83 1
        $user->setDb($this->di->get("db"));
84 1
        $res = $user->verifyPassword($acronym, $password);
85
86 1
        if (!$res) {
87 1
            $this->form->rememberValues();
88 1
            $this->form->addOutput("Fel användarnamn eller lösenord");
89 1
            return false;
90
        }
91
92
        //$this->form->addOutput("User " . $user->acronym . " logged in.");
93
        $this->di->get("session")->set("username", $user->acronym);
94
        $this->di->get("session")->set("email", $user->email);
95
        if ($user->acronym == "admin") {
96
            $this->di->get("session")->set("admin", $user->acronym);
97
        }
98
        $this->di->get("response")->redirect("user/profile");
99
        return true;
100
    }
101
}
102