Passed
Push — main ( 131f20...c1d479 )
by Rafael
51:37
created

Controller::isLogged()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
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 Alxarafe\Base\Database;
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
34
    public $db = null;
35
    public $username;
36
37
    public function __construct()
38
    {
39
        parent::__construct();
40
41
        if (!static::connectDb($this->config->db)) {
42
            throw new \Exception('Cannot connect to database.');
43
        }
44
45
        $this->db = new Database($this->config->db);
46
47
        if (!Auth::isLogged()) {
48
            dump('Usuario no identificado');
49
            header('Location: ' . BASE_URL . '/index.php?module=Admin&controller=Auth');
50
        }
51
52
        $this->username = Auth::$user->name;
53
    }
54
}
55