Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在yii中我在哪里重写身份验证方法?_Yii_Yii Extensions - Fatal编程技术网

在yii中我在哪里重写身份验证方法?

在yii中我在哪里重写身份验证方法?,yii,yii-extensions,Yii,Yii Extensions,我需要覆盖身份验证(当用户尝试登录时)和用于检查用户是否登录到应用程序头的函数(用于检查会话和cookie以检查用户是否登录的函数),但我不知道这些方法在哪里?而且我也不知道如何找到这些方法 **ovveride的原因是还检查一个标志,如果该标志为FLASE,则不验证用户,或者即使用户在页面更改(页眉重新加载)时也已验证,如果该标志更改为FLASE,则注销用户** 如果您能帮助我在yii/wiki和google以外的类似情况下找到足够的参考资料,我将不胜感激。我尝试过它们:) 问候, 对于自定义

我需要覆盖身份验证(当用户尝试登录时)和用于检查用户是否登录到应用程序头的函数(用于检查会话和cookie以检查用户是否登录的函数),但我不知道这些方法在哪里?而且我也不知道如何找到这些方法

**ovveride的原因是还检查一个标志,如果该标志为FLASE,则不验证用户,或者即使用户在页面更改(页眉重新加载)时也已验证,如果该标志更改为FLASE,则注销用户**

如果您能帮助我在yii/wiki和google以外的类似情况下找到足够的参考资料,我将不胜感激。我尝试过它们:)

问候,

  • 对于自定义身份验证扩展CUserIdentity类:

    app/components/UserIdentity.php

    <?php
    class UserIdentity extends CUserIdentity
    {
        const ERROR_USER_NOT_APPOVED=200;
    
        private $_id;
    
        /**
         * Authenticates a user.
         *
         * @return boolean whether authentication succeeds.
         */
        public function authenticate()
        {
            $criteria = new CDbCriteria;
            $criteria->condition = 'LOWER(email.email)=LOWER(:email)';
            $criteria->params = array(':email' => $this->username);
            $member = Member::model()
                        ->with('email')
                        ->together()
                        ->find($criteria);
    
            if ($member === null) {
                $this->errorCode = self::ERROR_USERNAME_INVALID;
            } elseif (!hash::check($this->password, $member->pass_hash)) {
                $this->errorCode = self::ERROR_PASSWORD_INVALID;
            } elseif (! $member->is_approved) {
                $this->errorCode = self::ERROR_USER_NOT_APPOVED;
            } else {
                $this->_id = $member->id;
                $this->username = $member->full_name;
    
                $this->setState('email', $member->email->email);
    
                $this->errorCode = self::ERROR_NONE;
            }
    
            return !$this->errorCode;
        }
    
        /**
         * @return integer the ID of the user record
         */
        public function getId()
        {
            return $this->_id;
        }
    }
    
  • 对于自动注销,您可以扩展CController:

    app/components/MainBaseController.php

    <?php
    
    class MainBaseController extends CController
    {
        public $settings = array();
    
        public function init()
        {
            parent::init();
    
            // set global settings
            // $this->settings = ...
    
            if (YOUR_FLAG_VALIDATION AND !Yii::app()->user->isGuest) {
                Yii::app()->user->logout();
            }
        }
    }
    

  • 我还需要实现这一点。我是否需要用新的UserIdentity.php替换protected/components/文件夹中现有的UserIdentity.php?
    $form = new MainLoginForm;
    if (isset($_POST['MainLoginForm'])) {
        $form->attributes = $_POST['MainLoginForm'];
        $valid = $form->validate();
        if ($valid) {
            // redirect
        }
    }
    
    <?php
    
    class MainBaseController extends CController
    {
        public $settings = array();
    
        public function init()
        {
            parent::init();
    
            // set global settings
            // $this->settings = ...
    
            if (YOUR_FLAG_VALIDATION AND !Yii::app()->user->isGuest) {
                Yii::app()->user->logout();
            }
        }
    }
    
    class YourController extends MainBaseController 
    {
        ....
    }