1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2021-2021 Rafael San José Tovar <[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
|
|
|
* (at your option) 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 Alxarafe\Core\Helpers; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Core\Base\Singleton; |
22
|
|
|
use Alxarafe\Core\Singletons\DebugTool; |
23
|
|
|
use Alxarafe\Database\Engine; |
24
|
|
|
use Alxarafe\Modules\Main\Controllers\Login; |
25
|
|
|
use Alxarafe\Modules\Main\Models\Users; |
26
|
|
|
use DebugBar\DebugBarException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Auth |
30
|
|
|
* |
31
|
|
|
* @package Alxarafe\Helpers |
32
|
|
|
*/ |
33
|
|
|
class Auth extends Singleton |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* TODO: Undocumented |
38
|
|
|
*/ |
39
|
|
|
const COOKIE_EXPIRATION = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* TODO: Undocumented |
43
|
|
|
* |
44
|
|
|
* @var string|null |
45
|
|
|
*/ |
46
|
|
|
private $user = null; |
47
|
|
|
|
48
|
|
|
private $users; |
49
|
|
|
|
50
|
|
|
private $debug; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Auth constructor. |
54
|
|
|
*/ |
55
|
|
|
public function __construct(string $index = 'main') |
56
|
|
|
{ |
57
|
|
|
parent::__construct($index); |
58
|
|
|
$this->users = new Users(); |
59
|
|
|
$this->getCookieUser(); |
60
|
|
|
$this->debug = DebugTool::getInstance(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* TODO: Undocummented |
65
|
|
|
*/ |
66
|
|
|
private function getCookieUser() |
67
|
|
|
{ |
68
|
|
|
if ($this->user === null) { |
69
|
|
|
if (isset($_COOKIE['user'])) { |
70
|
|
|
$this->user = $_COOKIE['user']; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* TODO: Undocummented |
77
|
|
|
* Esto no puede ser porque invoca a Login y carga el controlador. |
78
|
|
|
*/ |
79
|
|
|
public function login() |
80
|
|
|
{ |
81
|
|
|
// dump(debug_backtrace()); |
82
|
|
|
new Login(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws DebugBarException |
87
|
|
|
*/ |
88
|
|
|
public function logout() |
89
|
|
|
{ |
90
|
|
|
$this->debug->addMessage('messages', 'Auth::Logout(): ' . ($this->user === null ? 'There was no identified user.' : 'User' . $this->user . ' has successfully logged out')); |
91
|
|
|
$this->user = null; |
92
|
|
|
$this->clearCookieUser(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* TODO: Undocummented |
97
|
|
|
*/ |
98
|
|
|
private function clearCookieUser() |
99
|
|
|
{ |
100
|
|
|
setcookie('user', ''); |
101
|
|
|
unset($_COOKIE['user']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* TODO: Undocumented |
106
|
|
|
* |
107
|
|
|
* @return string|null |
108
|
|
|
*/ |
109
|
|
|
public function getUser(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->user; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Try login for user and password. |
116
|
|
|
* Dolibarr uses serveral systems |
117
|
|
|
* |
118
|
|
|
* @param $user |
119
|
|
|
* @param $password |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
* @throws DebugBarException |
123
|
|
|
* @see dol_hash in "htdocs/core/lib/security.lib.php" |
124
|
|
|
* |
125
|
|
|
*/ |
126
|
|
|
public function setUser($user, $password): bool |
127
|
|
|
{ |
128
|
|
|
$username_field = 'login'; // Alxarafe use 'username', but Dolibarr use 'login' |
129
|
|
|
$password_field = 'pass_crypted'; // Alxarafe use 'password', but Dolibarr use 'pass_crypted' |
130
|
|
|
$encrypt_method = "password_hash"; // Alxarafe use 'md5', but Dolibarr use a function called dol_hash |
131
|
|
|
|
132
|
|
|
$_user = Engine::select("SELECT * FROM {$this->users->tableName} WHERE $username_field='$user';"); |
133
|
|
|
if (count($_user) > 0 && password_verify($password, $_user[0][$password_field])) { |
134
|
|
|
$this->user = $user; |
135
|
|
|
setcookie('user', $user); |
136
|
|
|
$this->debug->addMessage('SQL', "$user autenticado"); |
137
|
|
|
} else { |
138
|
|
|
$this->user = null; |
139
|
|
|
setcookie('user', ''); |
140
|
|
|
unset($_COOKIE['user']); |
141
|
|
|
if (isset($_user[0])) { |
142
|
|
|
$this->debug->addMessage('SQL', "Comprobado {$encrypt_method}:" . $encrypt_method($password, PASSWORD_DEFAULT) . ', en fichero: ' . $_user[0][$password_field]); |
143
|
|
|
} else { |
144
|
|
|
$this->debug->addMessage('SQL', "Comprobado {$encrypt_method}:" . $encrypt_method($password, PASSWORD_DEFAULT) . ', en fichero no existe usuario ' . $user); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return $this->user != null; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|