Share Module Login With Other Modules

Dear,

I am building a sort of CMS system, which completely consists out of modules, so we can create a basic CMS for each customer and extend it with our own modules by copying and enabling it. Each module will have a "Backend" sub module if this is needed for admin/backend tasks.

The structure is as follows;

Application

  • Modules

– Admin

— Backend

– User

— Backend

– Page

— Backend

– Blog

— Backend

– Poll

— Backend

– Contact

— Backend

– Etc.

Now I like to create a login form within my admin module, I did the following;

modules/admin/AdminModule.php:


    public function init() {


        $this->setComponents(array(

            'user' => array(

                'class'             => 'CWebUser',

                'stateKeyPrefix'    => '_admin',

                'loginUrl'          => Yii::app()->createUrl('admin/default/login'),

            ),

        ));


        // set modules

        $this->setModules(array('backend'));


        // import objects

		$this->setImport(array(

			'admin.models.*',

			'admin.components.*',

		));

	}

modules/admin/controllers/DefaultController.php:


public function actionLogin() {


        $loginForm = new LoginForm;


        // collect default input data

        if(isset($_POST['LoginForm'])) {


            $loginForm->attributes = $_POST['LoginForm'];


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

            if($loginForm->validate()) {


                // find admin

                $admin = SysAdmin::model()->findByAttributes(array(

                    'username'  => $loginForm->username,

                    'active'    => true,

                ));


                // check admin status

                if(isset($admin)) {


                    if($loginForm->login()) {


                        $admin->last_login_date = new CDbExpression('NOW()');

                        $admin->update();


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

                    }

                } else {

                    $loginForm->addError('username', Yii::t('AdminModule.default', 'User "{username}" does not exist or is not allowed to login.', array('{username}' => $loginForm->username)));

                }

            }

        }


        // render view

        $this->render('login', array(

            'loginForm' => $loginForm,

        ));

    }

modules/admin/models/loginForm.php:


public function login()

    {

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

        {

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

            $this->_identity->authenticate();

        }

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

        {

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

            return true;

        }

        else

            return false;

    }

modules/admin/components/UserIdentity.php:


public function authenticate() {


        $user = SysAdmin::model()->findByAttributes(array(

            'username' => strtolower($this->username)

        ));

        if($user === null)

            $this->errorCode = self::ERROR_USERNAME_INVALID;

        else if(!CPasswordHelper::verifyPassword($this->password, $user->password))

            $this->errorCode = self::ERROR_PASSWORD_INVALID;

        else{

            // successful login

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

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

            $this->errorCode    = self::ERROR_NONE;


            $this->setState('username', $user->email);

            $this->setState('first_name', $user->first_name);

            $this->setState('insertions', $user->insertions);

            $this->setState('last_name', $user->last_name);

            $this->setState('full_name', $user->fullName);

            $this->setState('admin', true);

        }

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

    }

The login method is working well, but how can I check from another module, for example the page/backend module, if an admin user is logged on? I think the following should work;


if(Yii::app()->getModule('admin')->user->isGuest()) {

    echo 'not logged in!';

} else {

    echo 'logged in as '.Yii::app()->getModule('admin')->name.'!';

}

But this does’nt work! It keeps saying that i am “Guest”.

When i dump the Yii::app()->getModule(‘admin’)->user object i get te following:


CWebUser Object

(

    [allowAutoLogin] => 

    [guestName] => Guest

    [loginUrl] => /admin/default/login

    [identityCookie] => 

    [authTimeout] => 

    [absoluteAuthTimeout] => 

    [autoRenewCookie] => 

    [autoUpdateFlash] => 1

    [loginRequiredAjaxResponse] => 

    [_keyPrefix:CWebUser:private] => _admin

    [_access:CWebUser:private] => Array

        (

        )


    [behaviors] => Array

        (

        )


    [_initialized:CApplicationComponent:private] => 1

    [_e:CComponent:private] => 

    [_m:CComponent:private] => 

)



When i dump the Yii::app()->user object, i get te following:


EWebUser Object

(

    [allowAutoLogin] => 1

    [guestName] => Guest

    [loginUrl] => Array

        (

            [0] => /site/login

        )


    [identityCookie] => 

    [authTimeout] => 

    [absoluteAuthTimeout] => 

    [autoRenewCookie] => 

    [autoUpdateFlash] => 1

    [loginRequiredAjaxResponse] => 

    [_keyPrefix:CWebUser:private] => 7ba5c56c556a1f4fd81b1c02deb63c9c

    [_access:CWebUser:private] => Array

        (

        )


    [behaviors] => Array

        (

        )


    [_initialized:CApplicationComponent:private] => 1

    [_e:CComponent:private] => 

    [_m:CComponent:private] => 

)

When i dump the $_SESSION var, i get the following:


Array

(

    [7ba5c56c556a1f4fd81b1c02deb63c9c__id] => 1

    [7ba5c56c556a1f4fd81b1c02deb63c9c__name] => kevin@domain.com

    [7ba5c56c556a1f4fd81b1c02deb63c9cusername] => kevin@domain.com

    [7ba5c56c556a1f4fd81b1c02deb63c9cfirst_name] => Kevin

    [7ba5c56c556a1f4fd81b1c02deb63c9cinsertions] => 

    [7ba5c56c556a1f4fd81b1c02deb63c9clast_name] => Doe

    [7ba5c56c556a1f4fd81b1c02deb63c9cfull_name] => Kevin Doe

    [7ba5c56c556a1f4fd81b1c02deb63c9cadmin] => 1

    [7ba5c56c556a1f4fd81b1c02deb63c9c__states] => Array

        (

            [username] => 1

            [first_name] => 1

            [insertions] => 1

            [last_name] => 1

            [full_name] => 1

            [admin] => 1

        )


)



What am I doing wrong here?

Thanks in advance!

My solution for this (not perfect):

$user = new CWebUser;

[size=2]$user->setStateKeyPrefix("[/size][color=#008800][size=2]_admin[/size][/color][size=2]");[/size]

echo $user->getState("id") // you can get the states, not the entire object