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

标签 php yii

我们集成了 yii 验证访问规则。在登录页面,提交后 表单,它显示以下错误消息显示

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

这是 Controller 代码

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

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

最佳答案

你可以不使用

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

}

关于php - yii 调用身份验证访问规则部分中未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25092618/

相关文章:

php - 为什么PHP的sem_acquire会阻塞程序执行?

php - 当日期改变时,将每日总数更改为 +1

php - 在 Yii 的标准中如何获得计数 (*)

php - Yii:在 mysql 服务器上使用自动提交关闭的事件记录

php - MySQL MULTIPLE INSERT 获取失败的行?

php - 如何包含超过 2 个目录的文件?

PHPUnit:在一次测试中对模拟方法使用多个断言是一种不好的做法吗?

php - 我可以从 js 刷新 Yii CGridView 吗?

database - 如何利用缓存来提高分类网站的性能?

unit-testing - 在哪里测试验证 - 在单元或功能测试中?