beforeLogin method - Authentication and Authorization

Dear All,

I am still struggling with adding additional info to CWebUser . Antonio Ramirez Helped me and gave a nice solution here and I am posting that code below .

Here are my questions , Could some one please spare some time and clarify ?

  1. beforeLogin method is getting called for every request to the server . Login to the website and refresh you webpage you see the beforeLogin method gets called or send any request to the server and you notice beforeLogin method gets called . Is it supposed to be like that ? Because after login the session is established why beforeLogin method is getting called for each request

  2. In the below code _dbUser is false for every call. My assumption was _dbUser gets stored in the session and only for the first request it is False and for the subsequent requests it gets the value from Session . Could some one please clarify ?

  3. What is $states parameter in beforeLogin ( The second passing parameter) . I always see empty , Can You know what needs to be populated in this and where ?

Thanks All for your help

Regards

Yii Fan


/**

 * Custom Webuser class

 */

class WebUser extends CWebUser

{


        private $_dbUser = false;


        /**

         * @return User the user record associated with the currently logged in user. 

         * Null if there is no such user record (user not logged).

         */

        public function getDbUser()

        {

                if($this->_dbUser === false)

                        $this->_dbUser = $this->isGuest ? null : User::model()->findByPk($this->id);

                return $this->_dbUser;

        }


        protected function beforeLogin($id,$states,$fromCookie)

        {

                if($fromCookie)

                {

                        $user = User::model()->findByPk($id);

                        if($user && isset($states['vkey']) && $user->validation_key === $states['vkey'])

                        {

                                $this->_dbUser = $user;

                                return true;

                        }

                        else

                                return false;

                }

                return true;

        }


        protected function afterLogin($fromCookie)

        {

                if(($user = $this->getDbUser()) !== null)

                {

                       /* should we set login attributes? -- remove them if you dont have these fields as this is an example */

                        $user->saveAttributes(array(

                                'login_time' => time(),

                                'login_ip' => Yii::app->request->userHostAddress

                        ));

                }

        }

}