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\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
|
|
|
throw new \Exception('Cannot connect to database.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->db = new Database($this->config->db); |
44
|
|
|
|
45
|
|
|
if (!User::exists()) { |
46
|
|
|
User::createTable(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (User::count() === 0) { |
50
|
|
|
User::createAdmin(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function doIndex() |
55
|
|
|
{ |
56
|
|
|
return $this->doLogin(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function doLogin() |
60
|
|
|
{ |
61
|
|
|
$this->template = 'page/admin/login'; |
62
|
|
|
|
63
|
|
|
$this->username = filter_input(INPUT_POST, 'username'); |
64
|
|
|
$this->password = filter_input(INPUT_POST, 'password'); |
65
|
|
|
$this->remember = filter_input(INPUT_POST, 'remember') === 'on'; |
66
|
|
|
|
67
|
|
|
$login = filter_input(INPUT_POST, 'action') === 'login'; |
68
|
|
|
if (!$login) { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!Auth::login($this->username, $this->password)) { |
73
|
|
|
static::addAdvice($this->langs->trans('ErrorBadLoginPassword')); |
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->template = 'page/info'; |
78
|
|
|
static::addMessage('Usuario ' . $this->username . ' identificado correctamente1'); |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function doLogout() |
84
|
|
|
{ |
85
|
|
|
Auth::logout(); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|