|
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 Alxarafe\Base\Controller; |
|
20
|
|
|
|
|
21
|
|
|
// use Illuminate\Database\Capsule\Manager as DB; |
|
22
|
|
|
use Alxarafe\Base\Controller\Trait\DbTrait; |
|
23
|
|
|
use Alxarafe\Lib\Auth; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class Controller. Controller is the general purpose controller and requires the user to be authenticated. |
|
27
|
|
|
* |
|
28
|
|
|
* @package Alxarafe\Base |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class Controller extends ViewController |
|
31
|
|
|
{ |
|
32
|
|
|
use DbTrait; |
|
33
|
|
|
private static $config=null; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
static::connectDb(); |
|
38
|
|
|
|
|
39
|
|
|
// $db = DB::connection()->getPdo(); |
|
40
|
|
|
|
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
$this->checkLogin(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function checkLogin() |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->action === 'logout') { |
|
48
|
|
|
return $this->doLogout(); |
|
49
|
|
|
} |
|
50
|
|
|
if ($this->action !== 'login') { |
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
$this->doLogin(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function doLogout() |
|
57
|
|
|
{ |
|
58
|
|
|
Auth::logout(); |
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function doLogin() |
|
63
|
|
|
{ |
|
64
|
|
|
$username = $_POST['username']; |
|
65
|
|
|
$password = $_POST['password']; |
|
66
|
|
|
$ok = Auth::login($username, $password); |
|
67
|
|
|
if ($ok) { |
|
68
|
|
|
$this->message = 'Login ok'; |
|
69
|
|
|
} else { |
|
70
|
|
|
$this->alert = 'Login KO'; |
|
71
|
|
|
} |
|
72
|
|
|
return $ok; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function index(bool $executeActions = true): bool |
|
76
|
|
|
{ |
|
77
|
|
|
$log = $this->isLogged(); |
|
78
|
|
|
if (!$log) { |
|
79
|
|
|
$login = $this->doLogin(); |
|
80
|
|
|
if (!$login) { |
|
81
|
|
|
$this->template = 'auth/login'; |
|
82
|
|
|
$this->action = 'index'; |
|
83
|
|
|
$executeActions = false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return parent::index($executeActions); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function isLogged() |
|
90
|
|
|
{ |
|
91
|
|
|
if (!isset($this->username)) { |
|
92
|
|
|
$this->username = Auth::isLogged(); |
|
93
|
|
|
} |
|
94
|
|
|
return isset($this->username); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|