AuthController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 29
dl 0
loc 77
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A doIndex() 0 3 1
A getControllerName() 0 3 1
A doLogout() 0 4 1
A doLogin() 0 22 3
A getModuleName() 0 3 1
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 CoreModules\Admin\Controller;
20
21
use Alxarafe\Base\Controller\Controller;
22
use Alxarafe\Base\Controller\Trait\DbTrait;
23
use Alxarafe\Lib\Auth;
24
use Alxarafe\Lib\Messages;
25
use Alxarafe\Lib\Trans;
26
27
class AuthController extends Controller
28
{
29
    const MENU = 'admin|auth';
30
    const SIDEBAR_MENU = [
31
        ['option' => 'admin|auth|login', 'url' => 'index.php?module=Admin&controller=Auth&method=login'],
32
        ['option' => 'admin|auth|register', 'url' => 'index.php?module=Admin&controller=Auth&method=register'],
33
        ['option' => 'admin|auth|forgot_password', 'url' => 'index.php?module=Admin&controller=Auth&method=forgotPassword']
34
    ];
35
36
    use DbTrait;
37
38
    public $username;
39
    public $password;
40
    public $remember;
41
42
    public function __construct()
43
    {
44
        parent::__construct();
45
46
//        if (!static::connectDb($this->config->db)) {
47
//            throw new \Exception('Cannot connect to database.');
48
//        }
49
    }
50
51
    /**
52
     * Returns the module name for use in url function
53
     *
54
     * @return string
55
     */
56
    public static function getModuleName(): string
57
    {
58
        return 'Admin';
59
    }
60
61
    /**
62
     * Returns the controller name for use in url function
63
     *
64
     * @return string
65
     */
66
    public static function getControllerName(): string
67
    {
68
        return 'Auth';
69
    }
70
71
    public function doIndex()
72
    {
73
        return $this->doLogin();
74
    }
75
76
    public function doLogin()
77
    {
78
        $this->template = 'page/login';
79
80
        $this->username = filter_input(INPUT_POST, 'username');
81
        $this->password = filter_input(INPUT_POST, 'password');
82
        $this->remember = filter_input(INPUT_POST, 'remember') === 'on';
83
84
        $login = filter_input(INPUT_POST, 'action') === 'login';
85
        if (!$login) {
86
            return true;
87
        }
88
89
        if (!Auth::login($this->username, $this->password)) {
90
            Messages::addAdvice(Trans::_('authenticated_error'));
91
            return true;
92
        }
93
94
        $this->template = 'page/info';
95
        Messages::addMessage(Trans::_('authenticated_user', ['user' => $this->username]));
96
97
        return true;
98
    }
99
100
    public function doLogout()
101
    {
102
        Auth::logout();
103
        return true;
104
    }
105
106
}
107