Authentication with user db (3 fields)

Hi,

I’m successfully logged in using the user database in my project…

But I like to include the branch field while authentication, So based upon the Head office/normal branch the appropriate page gets displayed for the user…

Code in my login form model…




public $branch;


public function rules()

	{

		return array(

			// username and password are required

			array('username, password, branch', 'required'),

			// rememberMe needs to be a boolean

			array('rememberMe', 'boolean'),

			// password needs to be authenticated

			array('password', 'authenticate'),

		);

	}


public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

		{

			$this->_identity=new UserIdentity($this->username,$this->password,$this->branch);

			if(!$this->_identity->authenticate())

				$this->addError('branch','Incorrect username or password.');

		}

	}


public function login()

	{

		if($this->_identity===null)

		{

			$this->_identity=new UserIdentity($this->username,$this->password,$this->branch);

			$this->_identity->authenticate();

		}

		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)

		{

			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

			Yii::app()->user->login($this->_identity,$duration);

			return true;

		}

		else

			return false;

	}



Code in User Identity…




private $hint;


public function authenticate()

	{

		$record=User::model()->findByAttributes(array('username'=>$this->username));

		if($record===null)

		{

			$this->_id='user Null';

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		}

		else if($record->password!==$this->password)

		{

			$this->_id=$this->username;

			$this->errorCode=self::ERROR_PASSWORD_INVALID;	

		}

		else if($record->branch!==$this->branch)

		{

			$this->_id=$this->username;

			$err="You Branch Name is Incorrect.";

			$this->errorCode=$err;	

		}

		else

		{

			$this->_id=$record['userid'];

			$this->setState('title',$record['userid']);

			$this->errorCode=self::ERROR_NONE;

			

		}

		return !$this->errorCode;

		}



I’m not able to login with the branch field, it always show “Incorrect username or password.” error, please help to solve this problem…

post your login controller code

code present in controller/sitecontroller/actionLogin…




public function actionLogin()

	{

		$model=new LoginForm;


		// if it is ajax validation request

		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		// collect user input data

		if(isset($_POST['LoginForm']))

		{

			$model->attributes=$_POST['LoginForm'];

		        // validate user input and redirect to the previous page if valid

			if($model->validate() && $model->login())

				$this->redirect(Yii::app()->user->returnUrl);

		}

		// display the login form

		$this->render('login',array('model'=>$model));

	}



Note: In my UserIdentity function I fail to get the $this->branch, but while calling I include the branch as 3rd parameter…

It’s because UserIdentity constructor accepts only 2 parameters, you need to overwrite it, something like this:




public function __construct($user,$pass,$branch)

{

   $this->branch = $branch;

   parent::__construct($user,$pass);

}



I include this function in my User Identity class also, bust still it’s show error like branch not defined like that, if i define the branch as public in the class it show no error and while authentication not allow to enter into the page, please some help for this problem…

I tried branch field instead of password, in that case also it shows the same error…

In your UserIdentity you have




private $hint;

//may be 

private $branch;



Just checked it and it works




private $branch;

	

public function __construct($username, $password, $branch)

{

	$this->branch = $branch;

	parent::__construct($username, $password);

}


public function authenticate()

{

		echo $this->branch;die;

}



And it print branch!

Thank so much Newb, I spend full day to find this problem and finally got d output with your kind help…

Now I moved to the next stage of validation "error intimation"…




$this->errorCode=self::ERROR_USERNAME_INVALID;

$this->errorCode=self::ERROR_PASSWORD_INVALID;



I got this error messages in my login form using the switch case… But i want to throw the error for status and branch also during validation…

For status validation I have a code like




else if($record->branch!==$this->branch)

		{

			//$this->errorCode=self::ERROR_BRANCH_INVALID;

			$err="You Branch Incorrect";

			$this->errorCode=$err;	

		}

else if($record['status']!=='0')

		{

			$err="You have been Inactive by Admin.";

			$this->errorCode=$err;

		}



But i don know how to get this status error message in my switch case and i also need to get the branch error message also… I hope u surly know about this problem…

switch case in my login form…




$this->_identity->authenticate();

			switch($this->_identity->errorCode)

			{

			    case UserIdentity::ERROR_NONE:

				$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days

				Yii::app()->user->login($this->_identity,$duration);

				break;

			    case UserIdentity::ERROR_USERNAME_INVALID:

				$this->addError('username','Username is incorrect.');

				break;

			    case UserIdentity::ERROR_PASSWORD_INVALID:

				$this->addError('password','Password is incorrect.');

				break; 

			    case UserIdentity::ERROR_BRANCH_INVALID:

				$this->addError('branch','Branch is incorrect.');

				break; 

                            /*case UserIdentity::$err:

				$this->addError('branch','You have been Inactive by Admin.');

				break;*/ 

			    default: // UserIdentity::ERROR_PASSWORD_INVALID

				$this->addError('branch','Incorrect Username/Password or Hint.');

				break;

			}



You need to create Constants in your UserIdentity to check it. And set them for return value. For now, you don’t even have such constants as UserIdentity::ERROR_BRANCH_INVALID, don’t know will you see error, or just you ll never come to this part of code…




//it is only as example

const SOME_ERROR = SOME_VALUE


if(!SOMETHING)

   $this->errorCode = self::SOME_ERROR;

return $this->errorCode;



And switch will check it after.

Yes your correct friend, I declared it as a constant and now successfully log-in with all the fields and its respective errors…

Note: the constant declaration code is given below and it always start with 3 because 1&2 goes for username and password…

It will surly help in future for some others…




const ERROR_BRANCH_INVALID = 3;

const ERROR_STATUS_INVALID = 4;



Thank you… :P