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'); |
|
|
|
|
41
|
|
|
throw new \Exception('Cannot connect to database.'); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->db = new Database($this->config->db); |
45
|
|
|
|
46
|
|
|
if (!User::exists()) { |
47
|
|
|
die('Create table'); |
|
|
|
|
48
|
|
|
User::createTable(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (User::count() === 0) { |
52
|
|
|
die('Create admin'); |
|
|
|
|
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
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.