<?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 UserIdentity extends CUserIdentity
{
    private $_id;
    /**
     * Authenticates a user.
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {
        $username 	= strtolower($this->username);
		$password	=	md5($this->password);
		// from database... change to suite your authentication criteria
		$user = User::model()->find('LOWER(company_email_address)=? AND isActive=?  AND isConfirmed=?', array($username,'Y','Y'));
		if($user===null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if($user->password!==$password)
			$this->errorCode = self::ERROR_PASSWORD_INVALID;
		else{
			
			/*if((UserTypes::model()->findByPk( $user->type_id )->type) == 'admin' || (UserTypes::model()->findByPk( $user->type_id )->type)=='superadmin') // This is admin login
			{
				$this->setStateKeyPrefix('admin_');
			}
			else
			{
				$this->setStateKeyPrefix('front_');
			}*/
			
			$this->_id = $user->id;
			$this->setState('fullname', $user->fullname);
			$this->setState('wallet', null);
			$this->setState('usertype', UserType::model()->findByPk($user->user_type_id)->type);
			$this->username = $user->company_email_address;
			$this->errorCode = self::ERROR_NONE;
		}
		
		
		
		return $this->errorCode == self::ERROR_NONE;
    }
	
    public function getId()
    {
        return $this->_id;
    }
}