mod_auth_default::checkLogin()   F
last analyzed

Complexity

Conditions 27
Paths 36

Size

Total Lines 119

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 756

Importance

Changes 0
Metric Value
cc 27
nc 36
nop 3
dl 0
loc 119
rs 3.3333
c 0
b 0
f 0
ccs 0
cts 113
cp 0
crap 756

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
	class mod_auth_default {
3
4
		function __construct($config=Array()) {
5
			$this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
6
		}
7
8
		function authExternalUser($login, $password, $requestedPath = "/") {
0 ignored issues
show
Unused Code introduced by
The parameter $requestedPath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
9
			return false;
10
		}
11
12
		function loadConfig($requestedPath = "/") {
13
		global $ARConfig, $store;
14
			if (!$requestedPath) {
15
				$requestedPath = "/";
16
			}
17
			$_cache = $ARConfig->cache;
18 View Code Duplication
			while ( $requestedPath && $requestedPath!='/' && !$store->exists($requestedPath) ) {
19
				$requestedPath = $store->make_path( $requestedPath, '..' );
20
			}
21
			$site = current($store->call("system.get.phtml", "", $store->get($requestedPath)));
22
			if ($site) {
23
				$site_config = $site->loadUserConfig();
24
				$this->config['siteConfig'] = $site_config['authentication'];
25
			}
26
			$ARConfig->cache = $_cache;
27
			return $this->config['siteConfig'];
28
		}
29
30
		function authUser($login, $password, $ARLoginPath="") {
31
		global $store, $AR;
32
			// Make sure we always have a user.
33
			$this->getUser('public');
34
35
			$criteria = array();
36
			$criteria["object"]["implements"]["="]="puser";
37
			$criteria["login"]["value"]["="]=$login;
38
39
			$siteConfig = $this->loadConfig($ARLoginPath);
40
			foreach ((array)$siteConfig['userdirs'] as $userdir) {
41
42
				$user = current($store->call("system.authenticate.phtml", array("ARPassword" => $password),
43
						$store->find($userdir, $criteria, 1, 0)));
44
				if ($user) {
45
					$ARUserDir = $userdir;
46
					break;
47
				}
48
			}
49
50
			if (!$user) {
0 ignored issues
show
Bug introduced by
The variable $user 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...
51
				$user = $this->authExternalUser($login, $password, $ARLoginPath);
52
				$ARUserDir = $user->parent;
53
			}
54
55
			if ($user) {
56
				if ((!$user->data->config || !$user->data->config->disabled)) {
57
					if ($login !== "public") {
58
						/* welcome to Ariadne :) */
59
						ldSetCredentials($login, $ARUserDir);
0 ignored issues
show
Bug introduced by
The variable $ARUserDir 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...
60
					}
61
					$ARLogin = $user->data->login;
0 ignored issues
show
Unused Code introduced by
$ARLogin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
					$ARPassword = 0;
0 ignored issues
show
Unused Code introduced by
$ARPassword is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
					$AR->user = $user;
64
					$result = true;
65
				} else {
66
					debug("getUser: user('$login') has been disabled", "all");
67
					$result = LD_ERR_ACCESS;
68
				}
69
			} else {
70
				debug("authUser: user('$login') could not authenticate", "all");
71
				$result = LD_ERR_ACCESS;
72
			}
73
			return $result;
74
		}
75
76
		function getUser($login, $ARUserDir="/system/users/") {
77
			global $store, $AR;
78
			if (!$ARUserDir) {
79
				$ARUserDir = "/system/users/";
80
			}
81
82
			$criteria = array();
83
			$criteria["object"]["implements"]["="]="puser";
84
			$criteria["login"]["value"]["="]=$login;
85
86
			$user = current(
87
				$store->call(
88
					"system.get.phtml",
89
					Array(),
90
					$store->find($ARUserDir, $criteria)
91
				)
92
			);
93
94
			if ($user) {
95
				if ((!$user->data->config || !$user->data->config->disabled)) {
96
					$AR->user = $user;
97
					$result = true;
98
				} else {
99
					debug("getUser: user('$login') has been disabled", "all");
100
					$result = LD_ERR_ACCESS;
101
				}
102
103
			} else {
104
				debug("getUser: user('$login') not found", "all");
105
				$result = LD_ERR_ACCESS;
106
			}
107
			return $result;
108
		}
109
110
		function checkLogin($login, $password, $requestedPath="/") {
111
		global $ARCurrent, $AR;
112
			debug("checkLogin($login, [password])", "all");
113
			$result = null;
114
			if ($login) {
115
				debug("checkLogin: initiating new login ($login)", "all");
116
				if ($ARCurrent->session) {
117
					$ARUserDir = $ARCurrent->session->get("ARUserDir", true);
118
					if (!$ARUserDir) {
119
						$ARUserDir = "/system/users/";
120
					}
121
122
					if (!$ARCurrent->session->get("ARLogin") ||
123
							$ARCurrent->session->get("ARLogin") == "public") {
124
						debug("checkLogin: logging into a public session (".$ARCurrent->session->id.")", "all");
125
						$result = $this->authUser($login, $password, $requestedPath);
126
						if ($result !== true) {
127
							$this->getUser('public');
128
						}
129
					} else {
130
						if (ldCheckCredentials($login)) {
0 ignored issues
show
Bug introduced by
The call to ldCheckCredentials() misses a required argument $password.

This check looks for function calls that miss required arguments.

Loading history...
131
							debug("checkLogin: succesfully logged into private session (".$ARCurrent->session->id.")", "all");
132
							$result = $this->getUser($login, $ARUserDir);
133
						} else {
134
							if ($ARCurrent->session->get("ARLogin") == $login) {
135
								debug("checkLogin: user ($login) tries to login to his session without a cookie set @ $ARUserDir", "all");
136
								$result = $this->authUser($login, $password, $ARUserDir);
137
								if ($result !== true) {
138
									$this->getUser('public');
139
								}
140
							} else
141
							if (ldCheckCredentials($ARCurrent->session->get("ARLogin")))  {
0 ignored issues
show
Bug introduced by
The call to ldCheckCredentials() misses a required argument $password.

This check looks for function calls that miss required arguments.

Loading history...
142
								debug("checkLogin: user tries to login as another user", "all");
143
								$result = $this->authUser($login, $password, $requestedPath);
144
								if ($result !== true) {
145
									$this->getUser('public');
146
								}
147
							} else {
148
								debug("checkLogin: could not login to private session (".$ARCurrent->session->id."): creating a new one", "all");
149
								ldStartSession();
150
								$result = $this->authUser($login, $password, $ARUserDir);
151
								if ($result !== true) {
152
									$this->getUser('public');
153
								}
154
							}
155
						}
156
					}
157
				} else {
158
					debug("checkLogin: trying to log on", "all");
159
					$result = $this->authUser($login, $password, $requestedPath);
160
					if ($result !== true) {
161
						$this->getUser('public');
162
					}
163
164
				}
165
			} else {
166
				if ($ARCurrent->session) {
167
					$ARUserDir = $ARCurrent->session->get("ARUserDir", true);
168
					if (!$ARUserDir) {
169
						$ARUserDir = "/system/users/";
170
					}
171
172
					if (!$ARCurrent->session->get("ARLogin")) {
173
						if ($ARCurrent->session->get("ARSessionTimedout", 1)) {
174
							$ARCurrent->session->put("ARSessionTimedout", 0, 1);
175
						}
176
						debug("checkLogin: logging in with public session (".$ARCurrent->session->id.")", "all");
177
						$result = $this->checkLogin("public", "none");
178
					} else
179
					if ($ARCurrent->session->get("ARSessionTimedout", 1)) {
180
						debug("checkLogin: session has been timedout, forcing login", "all");
181
						// become public
182
						$this->getUser('public');
183
						$result = LD_ERR_SESSION;
184
					} else {
185
						$login = $ARCurrent->session->get("ARLogin");
186
						if (ldCheckCredentials($login)) {
0 ignored issues
show
Bug introduced by
The call to ldCheckCredentials() misses a required argument $password.

This check looks for function calls that miss required arguments.

Loading history...
187
							debug("checkLogin: logging ($login) into a private session (".$ARCurrent->session->id.") with credentials from cookie", "all");
188
							$result = $this->getUser($login, $ARUserDir);
189
						} else {
190
							debug("checkLogin: could not login ($login) on private session (".$ARCurrent->session->id.") with credentials from cookie: removing cookie", "all");
191
							// FIXME: only the loader should know about cookies for sessions
192
							setcookie("ARSessionCookie[".$ARCurrent->session->id."]", false);
193
							$this->getUser('public');
194
							$result = LD_ERR_ACCESS;
195
						}
196
					}
197
				} else {
198
					if ($AR->arSessionRespawn) {
199
						debug("checkLogin: trying to respawn a session", "all");
200
						$cookies = ldGetCredentials();
201
						if (is_array($cookies)) {
202
							reset($cookies);
203
							while (!$result && (list($sid, $sval)=each($cookies))) {
204
								ldStartSession($sid);
205
								$login = $ARCurrent->session->get("ARLogin");
206
								debug("checkLogin: trying to respawn session ($sid) for user ($login)", "all");
207
								if (ldCheckCredentials($login)) {
0 ignored issues
show
Bug introduced by
The call to ldCheckCredentials() misses a required argument $password.

This check looks for function calls that miss required arguments.

Loading history...
208
									$ARUserDir = $ARCurrent->session->get("ARUserDir", true);
209
									if (!$ARUserDir) {
210
										$ARUserDir = "/system/users/";
211
									}
212
213
									debug("checkLogin: credentials matched, loading user", "all");
214
									$result = $this->getUser($login, $ARUserDir);
215
								} else {
216
									debug("checkLogin: credentials didn't match", "all");
217
								}
218
							}
219
						}
220
					}
221
					if (!$result) {
222
						debug("checkLogin: normal public login", "all");
223
						$result = $this->authUser("public", "none");
224
					}
225
				}
226
			}
227
			return $result;
228
		}
229
	}
230