Authentication With One Of Two Fields

Hi,

Im pretty newbie to YII and I need to make a login that accepts email/phone + password.

In the UserIdentity/authenticate i have something like this:





public function authenticate()

	{

		//CVarDumper::dump($this->username, 10, true);

		//die('authenticate');


		if( strpos( $this->username, '@' ) !== false )

		{

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

				'select'    => 'user_id, salt, password',

				'condition' => 'email=:loginEmail AND blacklisted=:blacklisted AND active=:active AND verified=:verified',

				'params'    => array(

					':loginEmail'  => $this->username,

					':blacklisted' => 'false',

					':active'      => 'true',

					':verified'    => 1

				)

			) );


		}

		else // PHONE

		{


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

				'select'    => 'user_id, salt, password',

				'condition' => 'phone=:loginPhone AND blacklisted=:blacklisted AND active=:active AND verified=:verified',

				'params'    => array(

					':loginPhone'  => $this->username,

					':blacklisted' => 'false',

					':active'      => 'true',

					':verified'    => 1

				)

			) );

		}


		CVarDumper::dump( $this->username, 10, true );


		...

	}




when i login with a stupid name… without @… it should go to the phone part (ELSE), but the problem is that is matching with some other record, that it should NOT match…

what im doing wrong?

thanks

Hi pedro , I had somthing like your status because in my latest project I needed to use email or user id to login , but I solved it with out any problem .

I will share my code with you to help :


<?php


/** Ahmad Samilo

 * 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

{


	 // Need to store the user's ID:

	 private $_id;

 	public $password;

 	public $status;

 	public $session;

 

	/**

	 * Authenticates a user.

	 * The example implementation makes sure if the username and password

	 * are both 'demo'.

	 * In practical applications, this should be changed to authenticate

	 * against some persistent user identity storage (e.g. database).

	 * @return boolean whether authentication succeeds.

	 */




	public function authenticate()

	{

	

 

 

  	if(strpos($this->username, '@') == true){

	

   $user = Users::model()->findByAttributes(array('email'=>$this->username));

}


else{

   

   $user = Users::model()->findByAttributes(array('user_id'=>$this->username));

}

 

        	/// set number of faild login

         	$session=new CHttpSession;

         	$session->open();

      	

       	Yii::app()->session['number']= Yii::app()->session['number']+1;

       	$session=Yii::app()->session['number'];

          	/// end 


  	




		if ($user===null) { // No user found!

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		}

     	else if ($user->password !== SHA1($this->password) ) { // Invalid password!

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

        	

        	

    	

    	

    	

 	

     	

		} else { // Okay!

    	

     	

           	

		 	$this->errorCode=self::ERROR_NONE;

			// Store the role in a session:

			

         	$this->setState('id', $user->user_id);

         	$this->setState('name', $user->fname_ar.' '.$user->lname_ar);

         	$this->setState('per',$user->per);

         	$this->setState('active',$user->active);

			$this->_id = $user->user_id;

		}

		return !$this->errorCode;

	}

	

	public function getId()

	{

	 return $this->_id;

	}


	

}

Hope will help

Hi samilo,

thanks for the reply…

but my problem still remains the same. the query still returns a record wich should not be possible to return.

if i login with "foobar" and some password…





$user = User::model()->findByAttributes(array(

				'phone' => $this->username,

			));




that query is returning me the first record on the database, and that record has phone = 0… wich is completely stupid :(

foobar != 0

i think

why is the query making foobar == 0 ????

try to add intval and let me know

:


else // PHONE

                {


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

                                'select'    => 'user_id, salt, password',

                                'condition' => 'phone=:loginPhone AND blacklisted=:blacklisted AND active=:active AND verified=:verified',

                                'params'    => array(

                                        ':loginPhone'  => intval($this->username), ///// change here 

                                        ':blacklisted' => 'false',

                                        ':active'      => 'true',

                                        ':verified'    => 1

                                )

                        ) );

                }

yes, thanks

now it seams that is working :)

thanks

:lol: