Yii Framework Forum: Extend CWebUser - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Extend CWebUser trying to implement more methods extending it Rate Topic: -----

#1 User is offline   dracco 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-November 09

Posted 18 November 2009 - 05:16 AM

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...

Quote

CException
Description

Property "WebUser.isAdmin" is not defined.
Source File

/var/frameworks/yii-1.0.10/web/auth/CWebUser.php(97)


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.
0

#2 User is offline   dracco 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 18-November 09

Posted 18 November 2009 - 05:23 AM

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.
1

#3 User is offline   morabit 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 01-May 11

Posted 01 May 2011 - 07:56 AM

View Postdracco, on 18 November 2009 - 05:23 AM, said:

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

0

#4 User is offline   Liz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 10-February 12

Posted 10 February 2012 - 04:08 PM

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()

{
$user=User::model();
$user->logOutDateTime='TEST';
$user->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'=>array(
'user'=>array(
'class'=>'WebUser',


),
.....................

Where did I go wrong?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users