My Login Modul In Yii Can Not Work?

I am creating login action in yii framework, but I has the error and I can not fix,here is error:

Property "AdminIdentity.user_name" is not defined.


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

this is my login modul:

login.php




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

    'id'=>'login_form',

    'enableAjaxValidation'=>false,

    'enableClientValidation' => true));

     ?>     

        <div class="row">

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

        <?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>100)); ?>

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

        </div>


        <div class="row">

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

            <?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>100)); ?>

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

        </div>




        <div id="lower">        


        <?php echo $form->Checkbox($model,'rememberMe'); ?> 

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


        <input type="submit" value="Login"/>


        </div>


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



AdminController and actionLogin


class AdminController extends Controller

{

  public function actionLogin()

    {

        $this->layout = 'login';


        $model=new LoginForm;   




        if(isset($_POST['ajax']) && $_POST['ajax']==='login_form')

        {

             echo CActiveForm::validate($model);

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

        }




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

        {           

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

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

                $this->redirect('/ktbeauty/index.php/categories/index');

            }           

        }


        $this->render('login',array(

            'model'=>$model,

        ));

    }

}



LoginForm


class LoginForm extends CFormModel{

    public $username;

    public $password;

    public $rememberMe;

    private $_identity;


    public function rules()

    {

        return array(

            // username and password are required

            array('username, password', 'required','message'=>'input username requirement'),

            // rememberMe needs to be a boolean

            array('rememberMe', 'boolean'),

            // password needs to be authenticated

            array('password', 'authenticate','required','message'=>'input password requirement'),

        );

    }


    public function attributeLabels()

    {

        return array(

            'username'=>'User Name',

            'rememberMe'=>'Remember me next time',

            'password'=>'Password',

        );

    }


        public function authenticate($attribute,$params)

        {

                if(!$this->hasErrors())

                {

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

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

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

                }

        }


    public function login()

    {       

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

        {


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

            $this->_identity->authenticate();

        }

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

        {

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

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

            return true;

        }

        else

            return false;

    }

}



AdminIdentity class:




class AdminIdentity extends CUserIdentity

{

   private $_id;

   public function authenticate()

   {

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

       var_dump($record);exit;

       if($record===null)

       {

           $this->_id='user Null';

           $this->errorCode=self::ERROR_USERNAME_INVALID;

       }

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

       {        

            $this->_id=$this->user_name;

            $this->errorCode=self::ERROR_PASSWORD_INVALID;

       }

       else if($record['user_active']!=='1')

       {        

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

          $this->errorCode = $err;

       }


       else

       {  

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

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

          $this->errorCode=self::ERROR_NONE;


       }

       return !$this->errorCode;

   }


   public function getId()

   {

       return $this->_id;

   }

}

Admin Model class:


class Admin extends CActiveRecord

{   

    public function tableName()

    {

        return 'admin';

    }


    public function rules()

    {


        return array(

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

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

            array('user_name, password, email', 'length', 'max'=>100),

            array('phone, cellphone, name', 'length', 'max'=>45),           

            array('ad_id, user_name, password, email, phone, cellphone, name, user_status, user_active', 'safe', 'on'=>'search'),

        );

    }


    public function relations()

    {       

        return array(

        );

    }


    public function attributeLabels()

    {

        return array(

            'ad_id' => 'Ad',

            'user_name' => 'User Name',

            'password' => 'Password',

            'email' => 'Email',

            'phone' => 'Phone',

            'cellphone' => 'Cellphone',

            'name' => 'Name',

            'user_status' => 'User Status',

            'user_active' => 'User Active',

        );

    }


    public function search()

    {   


        $criteria=new CDbCriteria;


        $criteria->compare('ad_id',$this->ad_id);

        $criteria->compare('user_name',$this->user_name,true);

        $criteria->compare('password',$this->password,true);

        $criteria->compare('email',$this->email,true);

        $criteria->compare('phone',$this->phone,true);

        $criteria->compare('cellphone',$this->cellphone,true);

        $criteria->compare('name',$this->name,true);

        $criteria->compare('user_status',$this->user_status);

        $criteria->compare('user_active',$this->user_active);


        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

        ));

    }


    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }

}

hope you help me to finish the login modul. thankyou very much

Try changing the line below of your AdminIdentity Class




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



to




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



Note: All $this->user_name should be $this->username and $record->user_name should be the value of username in your database.

yes it works,in any ways, I have create cookie after login, how can I check that must login before access for Controller ?

here is my actionLogin


[code]public function actionLogin()

	{

	    $this->layout = 'login';

	  	   

		$model=new LoginForm;

		

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

		{			

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

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

				$cookie = new CHttpCookie('loginSuccess',$model->username);

				$cookie->expire = 604800;

				var_dump($cookie);exit;

				Yii::app()->request->cookies['loginSuccess'] = $cookie;			

				$this->redirect('/ktbeauty/index.php/categories/index');

			}			

		}

		

		$this->render('login',array(

			'model'=>$model,

		));

	}

[/code]

In your SiteController use Yii::app()->user->isGuest to check if the user is logged in and redirect to login page




 /**

     * This is the default 'index' action that is invoked

     * when an action is not explicitly requested by users.

     */

    public function actionIndex() {

        if (Yii::app()->user->isGuest)

            $this->redirect(Yii::app()->createUrl('site/login'));

        else

            $this->render('index');

    }



It not works for me,I store the user in cookies and how I check for all controller for the cookies is exist ?

I think this might help you.

http://www.yiiframework.com/forum/index.php/topic/15820-handling-cookies/

and

http://www.yiiframework.com/wiki/152/cookie-management-in-yii/

Once you know how to read the cookie value, you can now edit your SiteController.




public function actionIndex() {

        if (isset(ii::app()->request->cookies['user_name']->value))

            $this->redirect(Yii::app()->createUrl('site/login'));

        else

            $this->render('index');

    }



NOTE: Just put the checking in SiteController.php actionIndex. It will be applied to all the Controllers.

yes,it has worked,thankyou very much

That’s great!