Extend CWebUser

Hello.

I’m new to Yii Framework. I’m working with 1.0.10 branch.

I’m trying to extend functionality to CWebUser, especyficaly, I’m trying to add a isAdmin and a isRegistered methods. I placed a new class WebUser in /protected/components, which is extending the original CWebUser. Inside, I placed a couple of new public methods, like this:




<?php


/**

 * Overload of CWebUser to set some more methods.

 */

class WebUser extends CWebUser

{


        public function isAdmin()

        {

                // When Users are implemented, change this to check roles.

                return ( $this->getName() == 'admin' );

        }


        public function isRegistered()

        {

                return ( !$this->isGuest() && !$this->isAdmin() );

        }

}



Then I added a line in config/main.php to tell Yii to use this new class:




        // application components

        'components'=>array(

                'user'=>array(

                        // use a customized class, placed in 'components'

                        'class'=>'WebUser',




Then, I tried to use these new methods in the layout/main.php:




<?php $this->widget('application.components.MainMenu',array(

        'items'=>array(

                array('label'=>'Backoffice', 'url'=>array('/site/backoffice_main'), 'visible'=>Yii::app()->user->isAdmin),

        ),



And finally, went to the browser and…

So?

I’ve tried to take off the class param in the config/main.php > components and then the exception says Property “CWebUser.isAdmin” is not defined, so may I understand the new class is being declared?

Did I left any step?

thank you.

Ok, I figured out by myself:

The line:




Yii::app()->user->isAdmin



goes for attributes of the class. So, I had to change it to:




Yii::app()->user->isAdmin()



Note the parenthesis.

Nothing like being a novice ;)

thank you.

You should use getIsAdmin() in your extended Webuser class as function name, then you can do the following:




Yii::app()->user->isAdmin



I am having similar issue. All I want is to save user’s lastLoginTime to be saved into DB

All I have for WebUser is:

<?php

// this file must be stored in:

// protected/components/WebUser.php

class WebUser extends CWebUser {

public function afterLogout()

{


	&#036;user=User::model();


	&#036;user-&gt;logOutDateTime='TEST';


	&#036;user-&gt;saveAttributes(array('logOutDateTime'));


}

}

?>

For now logOutDateTime in my database is just varChar so I assume every time a user logs out it should write ‘TEST’ in to that filed but it does nothing.

in my Config/main.php I have this line

'components'=&gt;array(


	'user'=&gt;array(


		'class'=&gt;'WebUser',


			


		


	),

Where did I go wrong?