How and Where to Setup Login Session?

Hi,

We are having an issue with the Login action. It’s being called from UserController to login the authenticated user. In the User model, we have the following:


public function login()

	{

            

            if($this->_identity===null)

            {

                    $this->_identity=new UserIdentity($this->username,md5($this->password));

                    $this->_identity->authenticate();

            }

            if($this->_identity->errorCode===UserIdentity::ERROR_NONE)

            {

                    $duration=$this->rememberMe ? 3600*24*30 : 60*20; // 30 days

                    Yii::app()->user->login($this->_identity,$duration);

                    return true;

            }

            else{

                    $this->addError('password','Incorrect username or password.');

                    return false;

            }

	}

I understand this calls the Idetity class. However, I don’t see that a session has been instantiated. How and where should we do this?

Just read this article:

danaluther.blogspot.co.il/2010/03/yii-authentication-via-database.html

It’s really good one for me.

in Yii demo’s application we can get login session in UserIdentity.php, at directory protected/components

this is code in UserIdentity:




public function authenticate()

	{

		$user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));

		if($user===null)

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		else if(!$user->validatePassword($this->password))

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else

		{

			$this->_id=$user->id;

			$this->username=$user->username;

			$this->errorCode=self::ERROR_NONE;

			Yii::app()->session->add('ses_nama',$this->username);

		}

		return $this->errorCode==self::ERROR_NONE;

	}



you can get session at this line:

Yii::app()->session->add(‘ses_nama’,$this->username);

Thanks for your reply. I made changes to UserIdenity.php as follows:


public function authenticate()

        {

//                $username = $this->username;

//                $password = $this->password;

               

                $user = Users::model()->findbyAttributes(array($username=>$this->username));

                if($user === NULL){

                        $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;

                        

                        

                }else if ($user->password !== md5($this->password)){

//                        $this->username = $user->username;

//                       sess('SESS_USER_INFO', $user->attributes);

//                        $this->errorCode=self::ERROR_NONE;

						//invalid password

						$this->errorCode=self::ERROR_PASSWORD_INVALID;

                }

                else {

                	$this->errorCode=self::ERROR_NONE;

                }

                return !$this->errorCode;

        

I commented out the session syntax for now. Is this on track?