3
votes

Well, i am implementing CAS login in my application.. problem is, in CasLogin() function.. login gets successfull, all session variables are set.. and Yii::app()->user->isGuest, returns false. i.e. logged in properly. and Yii::app()->user->id, returns logged in ID.

yet... when i redirect from here to my index action, Yii::app()->user->isGuest, returns true. and Yii::app()->user->id, returns NULL.

ACTUALLY i conducted an experiment $_session contains all values in CasLogin(), but after redirection, it has NULL so session is being destroyed... during redirection

casUserIdentity extends CUserIdentity , whose constructor i am calling in Login Function of CasLoginForm();

By the way, LocalLoginIdentity works just fine... these values are not reset there.. though i send, username and password to LocalUserIdentity constructor..

Here are Codes Sequentially.. public function actionIndex() { echo"hello at index";

    if (! Yii::app()->user->isGuest) {
        //#CASE 1: User is already logged in

        $this->redirect(array("site/upload"));
    }
    else{
        //#CASE 2: User is not Logged in
        echo '<br>Always show up, and id NULL <br>';
        var_dump(Yii::app()->user->id);
        exit;
        $this->redirect(array("site/login"));
    }

    }

here is the code of CasLoginFunction():

    public function actionCasLogin($CID=NULL)
{
     //code to be imported from GMS here
    PhpCasControl::setPhpCasContext($CID);
    phpCAS::setPostAuthenticateCallback(array($this,'_saveServiceTkt'));
    $loginForm = new CasLoginForm;

    // validate user input and redirect to the previous page if valid

    if ($loginForm->login($CID)) {

      if (Yii::app()->user->isGuest){
            echo 'this always show up, if at this controller only';
      var_dump(Yii::app()->user->id);}
    else{
            echo 'it never showed up';
    var_dump(Yii::app()->user->id);}


        $this->redirect(array('index'));

    }
    else {
        throw new Exception("You don't have sufficient permissions to access this service. Please contact Administrator !");
    }
}

following is the code where i call CasUserIdentity() class CasLoginForm extends CFormModel {

private $_identity;   //User identity

/**
 * Logs in the user using the cas forceAuthentication method
 * @return boolean whether login is successful
 */
public function login($cname=NULL) {

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

        $this->_identity = new CasUserIdentity('', NULL);

        $this->_identity->authenticate($cname);

        var_dump($this->_identity);

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

        $duration = 0; // 0 days
        Yii::app()->user->login($this->_identity, $duration);
        //get service tkt from session

        $serviceTkt  = Yii::app()->user->getState('service_tkt');
        $this->rename_session($serviceTkt);
        //echo "<br> current_session <br><br>".$serviceTkt."<br>";
        //var_dump($_SESSION);
        return true;
    }
    else
        //echo "hies";
        return false;
}

this function returns True.. so no need to worry, about other details of CAS

1
What is the value of allowAutoLogin property of your user class? - Andrey Mischenko
@AndreyMischenko : it is perfectly set to TRUE - Himanshu97

1 Answers

1
votes

why you didn't use CUserIdentity class ? , however In your current authentication code just add below code to fix this problem .

Yii::app()->user->setState('id',$userId);// assign user id  

I recommended you to read this article which will help you so much

http://www.yiiframework.com/wiki/779/how-to-pass-the-third-parameter-to-useridentity-on-login-authentication/