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