Two Login Function in a app

Can someone pls help me on this prob…

Error message says that "$model->_id" is invalid, because $model is an instance of PsmsUserAccInfo model and it has no property named "_id".

Where does this "_id" belong to?

You should note that no one can tell the code that you are writing in your source files (PsmsUserAccInfo.php, SiteController.php, … etc).

I think you have given us too little information to get a help.

Why don’t you pull the relevant code fragments together and post them in an easy to read style?

my view file


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'psms-user-acc-info-login-form',

	'enableAjaxValidation'=>false,

)); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'username'); ?>

		<?php echo $form->textField($model,'username'); ?>

		<?php echo $form->error($model,'username'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton('Submit'); ?>

	</div>


<?php $this->endWidget(); ?>

my site controller


public function actionLogin1()

{

    $model=new PsmsUserAccInfo('login1');


if(isset($_POST['ajax']) && $_POST['ajax']==='psms-user-acc-info-login-form')

		{

			echo CActiveForm::validate($model);

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

		}





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

    {

		

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

        if($model->validate())

        {

          $user = PsmsUserAccInfo::model()->findByAttributes(array('username'=>$model->username));

		  print_r($user);

		      if ($user->username!== $model->username) 

			  			{ 

						

		                   $model->_id=$model->username;

							$this->errorCode=self::ERROR_USERNAME_INVALID;

								}

            return;

        }

    }

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

}

my model


public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('keyvalue', 'numerical', 'integerOnly'=>true),

			array('username, createdby, modifiedby', 'length', 'max'=>64),

			array('password', 'length', 'max'=>256),

			array('statusflag', 'length', 'max'=>1),

			array('creationdate, modifieddate', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, username, password, keyvalue, statusflag, createdby, modifiedby, creationdate, modifieddate', 'safe', 'on'=>'search'),

		);

	}



This is how user authentication works in Yii -

  1. User clicks ‘Login’

  2. Some action method in some controller is called, based on the url (loginForm in default case)

  3. Within the actionLoginForm method, you create an instance of UserIdentity class and call authenticate method of UserIdentity to authenticate the user.

  4. So the real authentication happens in authenticate method of UserIdentity class (present in protected/components folder)

  5. In your case you can create a new action method, say actionNewLoginForm and a new authenticate method in UserIdentity class, say newAuthenticate().

public function actionNewLoginForm()

{

$username = $_POST[‘username’];

$userIdentity = new UserIdentity($username);

if($identity->newAuthenticate())

Yii::app()->user->login($userIdentity);

else

echo $identity->errorMessage;

}

The newAuthenticate() method will be almost similar to authenticate() method, except you won’t check if the password is also correct. This is the crudest way. You can very easily refactor your code to reduce the number of methods.

You have copy-and-pasted some code snippet from other source, probably UserIdentity.php. "_id" and "ERROR_USERNAME_INVALID" belong to it and they are not of the current model or the current controller.

It’s quite natural that this code produces errors. Don’t you think so?

I think you would be better to start it all over again by reading the fundamental documents about the authentication framework of Yii. "Authenticating User" section of the blog tutorial might be the best starting point.

http://www.yiiframework.com/doc/blog/1.1/en/prototype.auth

[EDIT]

Ah, Mukesh has already summarized the points.

I have redone the authentication as you guys told. Here is the code

Inside User identity new authenticate




public function newauthenticate()

	{

	$user =PtUserAccInfo::model()->findByAttributes(array('UAI_UNAME'=>$this->username));

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

		                   $this->UAI_ID='user Null';


			$this->errorCode=self::ERROR_USERNAME_INVALID;

			}	

		else { // Okay!

		    $this->errorCode=self::ERROR_NONE;

		    // Store the role in a session:

		 //$this->setState('role', $user->role);

		$this->UAI_ID = $user->UAI_ID;

		}

		return !$this->errorCode;

	}		



My view File newlogin.php




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'pt-user-acc-info-newlogin-form',

	'enableAjaxValidation'=>true,

)); ?>


	


	<?php echo $form->errorSummary($model); ?>


	

	<div class="row">

		<?php echo $form->labelEx($model,'UAI_UNAME'); ?>

		<?php echo $form->textField($model,'UAI_UNAME'); ?>

		<?php echo $form->error($model,'UAI_UNAME'); ?>

	</div>


	

	<div class="row buttons">

		<?php echo CHtml::submitButton('Submit'); ?>

	</div>




my controller… site controller





	public function actionNewlogin()

{

    $model=new PtUserAccInfo('login');


    // uncomment the following code to enable ajax-based validation

    

    if(isset($_POST['ajax']) && $_POST['ajax']==='pt-user-acc-info-newlogin-form')

    {

        echo CActiveForm::validate($model);

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

    }

 


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

    {

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

        if($model->validate())

        {

		 $username = $_POST['UAI_UNAME'];


		$userIdentity = new UserIdentity($username);


		if($identity->newauthenticate())

		Yii::app()->user->newlogin($userIdentity);

		else

		echo $identity->errorMessage;

            return;

        }

    }

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

}

	






Anyway after this changes has be done… i am getting a error

Missing argument 2 for CUserIdentity::__construct(), called in C:\wamp\www\psmsb2c\protected\controllers\SiteController.php on line 54 and defined

in your controller


$userIdentity = new UserIdentity($username);

you must also add the password


$userIdentity = new UserIdentity($username,'myP4$$w0rd');

I just need to validate username… i dont want a password

My Requirment

My Coding

Hi Gomez

pass it blank , as you won’t use it

I don’t have time to check at all of your code, but this will solve the problem you are having

Finally guys i was able to log in using user name authentication alone.

I made a exact copy of the process Yii framework was using for authentication

and removed the password field from form and removed the password authentication from user identity.

Anyway this is not gona end my tasks… :-[

"属性 "SiteController.username" 未被定义."