Passed
Pull Request — master (#16)
by Anton
03:45
created

Auth::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Cadmium\System\Modules\Auth
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace Modules {
11
12
	use DB, Session;
13
14
	abstract class Auth {
15
16
		private static $admin = false, $auth = null, $user = null;
17
18
		/**
19
		 * Initialize the session
20
		 *
21
		 * @property string $section : an active section (SECTION_SITE or SECTION_ADMIN)
22
		 *
23
		 * @return bool : true on success or false on failure
24
		 */
25
26
		public static function init(string $section) : bool {
27
28
			self::$admin = ($section === SECTION_ADMIN);
29
30
			self::$auth = null; self::$user = Entitizer::get(TABLE_USERS);
31
32
			# Check session code
33
34
			if (!is_string($code = Session::get('code'))) return false;
35
36
			# Authorize
37
38
			if (false === ($result = Auth\Utils\Connector\Session::authorize($code, self::$admin))) return false;
39
40
			# Update auth
41
42
			$result['auth']->edit(['time' => REQUEST_TIME]);
43
44
			# Update user
45
46
			$result['user']->edit(['time_logged' => REQUEST_TIME]);
47
48
			# Set entities
49
50
			self::$auth = $result['auth']; self::$user = $result['user'];
51
52
			# ------------------------
53
54
			return true;
55
		}
56
57
		/**
58
		 * Delete the session
59
		 *
60
		 * @return bool : true on success or false on failure
61
		 */
62
63
		public static function logout() : bool {
64
65
			if ((null === self::$user) || (0 === self::$user->id)) return false;
66
67
			# Remove auth entry from db
68
69
			self::$auth->remove();
0 ignored issues
show
Bug introduced by
The method remove cannot be called on self::$auth (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
71
			# Remove session variable
72
73
			Session::delete('code');
74
75
			# Reset entities
76
77
			self::$auth = null; self::$user = Entitizer::get(TABLE_USERS);
78
79
			# ------------------------
80
81
			return true;
82
		}
83
84
		/**
85
		 * Check whether the initial registration is required (the users table is empty and auto_increment is set to 0)
86
		 */
87
88
		public static function isInitial() : bool {
89
90
			DB::send("SHOW TABLE STATUS WHERE name LIKE '" . TABLE_USERS . "'");
91
92
			if (!(DB::getLast() && (null !== ($data = DB::getLast()->getRow())))) return false;
93
94
			# ------------------------
95
96
			return (($data['Rows'] == 0) && ($data['Auto_increment'] == 1));
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
97
		}
98
99
		/**
100
		 * Check whether the authorization mode is set to admin
101
		 */
102
103
		public static function isAdmin() : bool {
104
105
			return self::$admin;
106
		}
107
108
		/**
109
		 * Check whether the authorization was successful
110
		 */
111
112
		public static function isLogged() : bool {
113
114
			return ((null !== self::$user) && (0 !== self::$user->id));
115
		}
116
117
		/**
118
		 * Get the user param
119
		 *
120
		 * @return mixed|null : the param value or null if the session was not initialized
121
		 */
122
123
		public static function get(string $name) {
124
125
			return (self::$user->$name ?? null);
126
		}
127
128
		/**
129
		 * Return the user entity object
130
		 *
131
		 * @return Modules\Entitizer\Entity\User|false : the user entity object or false if the session was not initialized
132
		 */
133
134
		public static function getUser() {
135
136
			return (self::$user ?? false);
137
		}
138
	}
139
}
140