UserIdentity::authenticate()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 12
c 1
b 1
f 0
nc 7
nop 0
dl 0
loc 29
rs 9.5555
1
<?php
2
3
/**
4
 * Represents the data needed to authenticate a user. It also provides the 
5
 * getId() method which is used to find the current user model through 
6
 * Yii::app()->user->id
7
 * 
8
 * @author Sam Stenvall <[email protected]>
9
 * @copyright Copyright &copy; Sam Stenvall 2013-
10
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
11
 */
12
class UserIdentity extends CUserIdentity
13
{
14
15
	/**
16
	 * @var int the user ID
17
	 */
18
	private $_userId;
19
20
	/**
21
	 * Authenticates a user
22
	 * @return int the error code (ERROR_NONE if authentication succeeded)
23
	 */
24
	public function authenticate()
25
	{
26
		// Try to match the user based on both the plain-text password and a 
27
		// hashed varient. The default "admin" user has its password stored in 
28
		// plaintext so we need to hash it on first login.
29
		
30
		// Match usernames case-insensitively
31
		$user = User::model()->find('LOWER(username) = :username', array(
32
			':username'=>strtolower($this->username)));
33
34
		$this->errorCode = self::ERROR_UNKNOWN_IDENTITY;
35
36
		if ($user !== null)
37
		{
38
			// Password is stored as plain-text
39
			if ($user->password === $this->password)
40
			{
41
				// Re-save the user, that way the password will get hashed
42
				$user->save();
43
				$this->errorCode = self::ERROR_NONE;
44
			}
45
			elseif (User::checkPassword($this->password, $user->password))
46
				$this->errorCode = self::ERROR_NONE;
47
48
			if ($this->errorCode === self::ERROR_NONE)
49
				$this->_userId = $user->id;
50
		}
51
52
		return $this->errorCode;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->errorCode returns the type integer which is incompatible with the return type mandated by IUserIdentity::authenticate() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
53
	}
54
55
	/**
56
	 * Returns the user ID
57
	 * @return int
58
	 */
59
	public function getId()
60
	{
61
		return $this->_userId;
62
	}
63
64
}