Problem with Authenticate function function authenticate()

I have the number of users with agencyid, username and email id. In function authenticate currently it checks only username and email id, but we want to check $_SESSION[agencyid] also.

class UserIdentity extends CUserIdentity {

private $_id;





/**


 * Authenticates a user.


 * @return boolean whether authentication succeeds.


 */


public function authenticate() 


{


	$user = User::model()->find('LOWER(username)=?', array(strtolower($this->username)));


	if ($user === null)


	{


		$user = User::model()->find('LOWER(email)=?', array(strtolower($this->username)));


		if ($user === null){


			$this->errorCode = self::ERROR_USERNAME_INVALID;


			return 0;


		}


	}

we are trying to check like:

$user = User::model()->find(

           array(


              'select'=>'agency_site_id,username',  // add ONLY the columns you need


              'condition'=>'username=:uname AND agency_site_id = :agencyid',


              'params'=>array(':uname'=>$this->username, ':agencyid'=>$_SESSION['AgencySiteId'])


           )


         );

if ($user === null)

{

$user = User::model()->find(

           array(


              'select'=>'agency_site_id,email',  // add ONLY the columns you need


              'condition'=>'email=:uname AND agency_site_id = :agencyid',


              'params'=>array(':uname'=>$this->username, ':agencyid'=>$_SESSION['AgencySiteId'])


           )


         );





if ($user === null){


$this->errorCode = self::ERROR_USERNAME_INVALID;


return 0;

}

}

Here it checks only with username not agencyid

it shows first row of same username, but we want same username and SESSION [agencyid]= db agency id

Please help …