View file vkclone-0.0.1/protected/components/DatabaseUserIdentity.php

File size: 1.23Kb
<?php
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class DatabaseUserIdentity extends CUserIdentity {
	const ERROR_NON_ADMIN = 3;

	private $_id;

	public $email;

	/**
	 * Constructor.
	 * @param string $email email
	 * @param string $password password
	 */
	public function __construct($email, $password) {
		$this->email = $email;
		$this->password = $password;
	}

	/**
	 * Authenticates a user.
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate($onlyAdmin = false) {
		$record = User::model()->findByAttributes(array('email' => $this->email));
		if ($record === null)
			$this->errorCode = self::ERROR_USERNAME_INVALID;
		else if ($record->password_hash !== crypt($this->password, $record->password_hash))
			$this->errorCode = self::ERROR_PASSWORD_INVALID;
		else if ($onlyAdmin && !Yii::app()->getAuthManager()->checkAccess('administrator', $record->id))
			$this->errorCode = self::ERROR_NON_ADMIN;
		else {
			$this->_id = $record->id;
			$this->errorCode = self::ERROR_NONE;
		}
		return $this->errorCode == self::ERROR_NONE;
	}

	public function getId() {
		return $this->_id;
	}
}