0

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

** ovveride 的原因是还要检查一个标志,如果标志是 FLASE,则不验证用户,或者即使用户在页面更改(标题重新加载)时也通过了身份验证,如果标志更改为则注销用户闪光**

如果您还帮助我找到足够的参考资料,在 yii/wiki 和 google 旁边的类似情况下可以帮助我,我将不胜感激:)

问候,

4

1 回答 1

2
  1. 对于自定义身份验证扩展 CUserIdentity 类:

    应用程序/组件/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;
        }
    }
    

    然后创建自定义表单(app/models/MainLoginForm.php):

    <?php
    
    /**
     * MainLoginForm class.
     * MainLoginForm is the data structure for keeping
     * user login form data.
     */
    class MainLoginForm extends CFormModel
    {
        public $email;
        public $password;
        public $rememberMe;
    
        /**
         * Declares the validation rules.
         * The rules state that email and password are required,
         * and password needs to be authenticated.
         */
        public function rules()
        {
            return array(
                array('email', 'filter', 'filter' => 'trim'),
                array('email', 'required',
                    'message' => Yii::t('auth', 'Email address is required.')),
                array('email', 'email',
                    'message' => Yii::t('auth', 'Enter a valid Email address.')),
    
                array('password', 'required',
                    'message' => Yii::t('auth', 'Password is required.')),
    
                // password needs to be authenticated
                array('password', 'authenticate'),
    
                array('rememberMe', 'safe'),
            );
        }
    
        /**
         * Declares attribute labels.
         */
        public function attributeLabels()
        {
           return array(
              'email'       => Yii::t('auth', 'Email Address'),
              'password'    => Yii::t('auth', 'Password'),
              'rememberMe'  => Yii::t('auth', 'Remember me.'),
           );
        }
    
        /**
         * Authenticates the password.
         * This is the 'authenticate' validator as declared in rules().
         */
        public function authenticate($attribute, $params)
        {
            // we only want to authenticate when no input errors
            if (! $this->hasErrors()) {
                $identity = new UserIdentity($this->email, $this->password);
                $identity->authenticate();
                switch ($identity->errorCode) {
                    case UserIdentity::ERROR_NONE:
                        $duration = ($this->rememberMe)
                            ? 3600*24*14 // 14 days
                            : 0; // login till the user closes the browser
                        Yii::app()->user->login($identity, $duration);
                        break;
    
                    default:
                        // UserIdentity::ERROR_USERNAME_INVALID
                        // UserIdentity::ERROR_PASSWORD_INVALID
                        // UserIdentity::ERROR_MEMBER_NOT_APPOVED
                        $this->addError('', Yii::t('auth',
                            'Incorrect username/password combination.'));
                        break;
                }
            }
        }
    }
    

    最后更新您的登录方法(actionLogin):

    $form = new MainLoginForm;
    if (isset($_POST['MainLoginForm'])) {
        $form->attributes = $_POST['MainLoginForm'];
        $valid = $form->validate();
        if ($valid) {
            // redirect
        }
    }
    
  2. 对于自动注销,您可以扩展 CController:

    应用程序/组件/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();
            }
        }
    }
    

    然后使用自定义基本控件:

    class YourController extends MainBaseController 
    {
        ....
    }
    
于 2013-05-26T10:42:06.983 回答