yii在身份验证访问规则部分中调用未定义的方法

时间:2014-08-02 07:08:37

标签: php yii

我们已经整合了yii身份验证加密规则。在登录页面中,提交后 表单,它显示以下错误消息显示

致命错误:在第13行的D:\ wamp \ www \ onlinetest \ protected \ components \ UserIdentity.php中调用未定义的方法LoginForm :: model()

这是控制器代码

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

这是登录表单模型

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

以下是组件

中的useridentity.php
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;
   }
}

如何解决此问题?如果你知道帮助我

3 个答案:

答案 0 :(得分:1)

你可以不使用

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

因为LoginForm扩展了CFormModel

对于数据库检索,它应该扩展CActiveRecord

看到这个

see 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,
    ));
}

}

答案 1 :(得分:0)

您没有包含模型方法的代码

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

在您的模型类中

答案 2 :(得分:0)

您的UserIdentity类方法身份验证不正确。请参阅下面的代码以了解具体方法。

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