问题描述:

We have integrated the yii authenticate acceess rules. In the login page, after submit the

form, it displays the following error message shows

Fatal error: Call to undefined method LoginForm::model() in D:\wamp\www\onlinetest\protected\components\UserIdentity.php on line 13

Here is the controller code

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));

}

Here is the login form model

class LoginForm extends CFormModel

{

public $username;

public $password;

public $rememberMe;

private $_identity;

public function tableName()

{

return 'tbl_login';

}

public function authenticate($attribute,$params)

{

if(!$this->hasErrors()) // we only want to authenticate when no input errors

{

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

$identity->authenticate();

switch($identity->errorCode)

{

case UserIdentity::ERROR_NONE:

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

break;

case UserIdentity::ERROR_USERNAME_INVALID:

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

break;

default: // UserIdentity::ERROR_PASSWORD_INVALID

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

break;

}

}

}

public function login()

{

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

{

$this->_identity=new UserIdentity($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()->user->login($this->_identity,$duration);

return true;

}

else

return false;

}

}

Here is the useridentity.php in components

class UserIdentity extends CUserIdentity

{

private $_id;

public function authenticate()

{

$record=LoginForm::model()->findByAttributes(array('VarUser_type'=>$this->username)); // here I use Email as user name which comes from database

if($record===null)

{

$this->_id='user Null';

$this->errorCode=self::ERROR_USERNAME_INVALID;

}

else if($record->E_PASSWORD!==$this->password) // here I compare db password with passwod field

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

$this->errorCode=self::ERROR_PASSWORD_INVALID;

}

else

{

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

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

$this->errorCode=self::ERROR_NONE;

}

return !$this->errorCode;

}

public function getId() // override Id

{

return $this->_id;

}

}

How can I fix this issue? If you know help me

网友答案:

you can njot use

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

because LoginForm extends CFormModel

for database retrival it should extends CActiveRecord

see this

see this

your model should be like this

class Users extends CActiveRecord
{
/**
 * Returns the static model of the specified AR class.
 * @return Users the static model class
 */

private $_identity;


public static function model($className=__CLASS__)
{
    return parent::model($className);
}

/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'users';
}

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(

        array(' password, user_name,' , 'required', 'on'=>'login' ),

        array('user_id, last_name, first_name, address1, address2, city, pincode, state_id, country_id, phone, fax, email, created_date, updated_date, last_login, company_name, tour_id, password, user_name, last_login_from, gender, is_session_on, status, memo, cell, role_type_id, group_contract_template_id, group_policy_id, billing_contact, billing_phone, billing_address, billing_email, after_hours_phone', 'safe', 'on'=>'search'),
    );
}

/**
 * @return array relational rules.
 */
public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(

    );
}


public function login( $id, $password = "" )
{

    $this->_identity = new UserIdentity($username = $id ,$password);
    $this->_identity->authenticate();
    //Yii::app()->user->login($this->_identity,3600*24*30);

    if(Yii::app()->user->login($this->_identity,0*0*0))
    {
        //echo $this->_identity->errorMessage;
                    return true;
    }
    else
    {
        Yii::app()->user->setState('error', $this->_identity->errorMessage);
        return false;
    }

}


/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'user_id' => 'User',
        'last_name' => 'Last Name',
        'first_name' => 'First Name',
        'address1' => 'Address1',

        'email' => 'Email',

    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
 */
public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('user_id',$this->user_id,true);
    $criteria->compare('last_name',$this->last_name,true);
    $criteria->compare('first_name',$this->first_name,true);

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



    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

}
网友答案:

you don't have included the code for model method

public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

in your model class

网友答案:

Your UserIdentity class method authenticate is incorrect.. Refer to the code below to see how.

   public function authenticate()
   {
       $record=YourUserModel::model()->find(array(
            'condition'=>'VarUser_type =:username',
            'params'=>array(':username'=>$this->username)
       )); 
       if($record===null)
               {
                       $this->_id='user Null';
                                   $this->errorCode=self::ERROR_USERNAME_INVALID;
               }
       else if($record->E_PASSWORD!==$this->password)            // here I compare db password with passwod field
               {        $this->_id=$this->username;
                       $this->errorCode=self::ERROR_PASSWORD_INVALID;
               }


       else
       {  
          $this->_id=$record['VarUser_type'];
           $this->setState('title', $record['VarUser_type']);
           $this->errorCode=self::ERROR_NONE;

       }
       return !$this->errorCode;
   }
相关阅读:
Top