我在哪里覆盖yii中的身份验证方法?

时间:2013-05-26 09:53:48

标签: yii yii-extensions

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

** ovveride的原因是还检查一个Flag,如果该标志是FLASE不认证用户,或者即使用户也在页面上进行了身份验证更改(header reload)注销用户如果标志改为FLASE **

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

此致

1 个答案:

答案 0 :(得分: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 
    {
        ....
    }