Passed
Push — main ( 131f20...c1d479 )
by Rafael
51:37
created

AuthController::doLogout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Modules\Admin\Controller;
20
21
use Alxarafe\Base\Controller\Trait\DbTrait;
22
use Alxarafe\Base\Controller\ViewController;
23
use Alxarafe\Base\Database;
24
use Alxarafe\Lib\Auth;
25
use Alxarafe\Model\User;
26
27
class AuthController extends ViewController
28
{
29
    use DbTrait;
30
31
    public $username;
32
    public $password;
33
    public $remember;
34
    public $db;
35
36
    public function __construct()
37
    {
38
        parent::__construct();
39
        if (!static::connectDb($this->config->db)) {
40
            die('No database');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
41
            throw new \Exception('Cannot connect to database.');
0 ignored issues
show
Unused Code introduced by
ThrowNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
42
        }
43
44
        $this->db = new Database($this->config->db);
45
46
        if (!User::exists()) {
47
            die('Create table');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
48
            User::createTable();
49
        }
50
51
        if (User::count() === 0) {
52
            die('Create admin');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
53
            User::createAdmin();
54
        }
55
    }
56
57
    public function doIndex()
58
    {
59
        return $this->doLogin();
60
    }
61
62
    public function doLogin()
63
    {
64
        $this->template = 'page/admin/login';
65
66
        $this->username = filter_input(INPUT_POST, 'username');
67
        $this->password = filter_input(INPUT_POST, 'password');
68
        $this->remember = filter_input(INPUT_POST, 'remember') === 'on';
69
70
        $login = filter_input(INPUT_POST, 'action') === 'login';
71
        if (!$login) {
72
            return true;
73
        }
74
75
        if (!Auth::login($this->username, $this->password)) {
76
            static::addAdvice('Usuario o contraseña incorrectos');
77
            return true;
78
        }
79
80
        $this->template = 'page/info';
81
        static::addMessage('Usuario ' . $this->username . ' identificado correctamente');
82
83
        return true;
84
    }
85
86
    public function doLogout()
87
    {
88
        Auth::logout();
89
        return true;
90
    }
91
92
}
93