LoginForm::callbackSubmit()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.125

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 10
cts 20
cp 0.5
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 3
nop 0
crap 4.125
1
<?php
2
3
namespace Almrooth\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Almrooth\Comment\User;
8
9
/**
10
 * Example of FormModel implementation.
11
 */
12
class LoginForm 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 2
                "use_fieldset" => false,
27 2
            ],
28
            [
29
                "username" => [
30 2
                    "type"          => "text",
31 2
                    "label"         => "Användarnamn",
32 2
                    "validation"    => ["not_empty"],
33 2
                ],
34
                        
35
                "password" => [
36 2
                    "type"          => "password",
37 2
                    "label"         => "Lösenord",
38 2
                    "validation"    => ["not_empty"],
39 2
                ],
40
41
                "submit" => [
42 2
                    "type" => "submit",
43 2
                    "value" => "Logga in",
44 2
                    "callback" => [$this, "callbackSubmit"],
45
                    "class" => "btn"
46 2
                ],
47
            ]
48 2
        );
49 2
    }
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 1
    public function callbackSubmit()
59
    {
60
        // Get values from the submitted form
61 1
        $username       = $this->form->value("username");
62 1
        $password       = $this->form->value("password");
63
64
        // Connect to databse and se if user exists and if so compare password
65 1
        $user = new User();
66 1
        $user->setDb($this->di->get("db"));
67 1
        $res = $user->verifyPassword($username, $password);
68
69 1
        if (!$res) {
70 1
            $this->form->rememberValues();
71 1
            $this->form->addOutput("User or password did not match.");
72 1
            return false;
73
        }
74
75
        // Get the user
76
        $user->find("username", $username);
77
78
        // Check if user is deleted
79
        if (isset($user->deleted)) {
80
            $this->form->rememberValues();
81
            $this->form->addOutput("User is inactive. Contact administrator.");
82
            return false;
83
        }
84
85
        // Save user to session
86
        $this->di->get("session")->set("user_id", $user->id);
87
        $this->di->get("session")->set("username", $user->username);
88
        $this->di->get("session")->set("user_role", $user->role);
89
90
        // Redirect to profile page
91
        $this->di->get("response")->redirect($this->di->get("url")->create("comment"));
92
    }
93
}
94