Posts Tagged with yiicore

Displaying 1-3 of 3 results.
Wiki: Yii 1.1: More information to Yii::app() user - id, userrole etc.wiki
posted by admin on March 2, 2016
By default, the expression Yii::app()->user returns a CWebUser application component which represents the information that are closely related with the current user. Some information can be persistent throughout the current user session. For example, CWebUser already comes with a name property that stores the username of the current user.

In order to store more information, we need to modify the identity class used together with CWebUser. Each application may have one or several identity classes which are mainly responsible to provide ways of performing user authentication.

Here we use the UserIdentity class included in the testdrive application as an example, assuming our goal is to add the ID and the last login time of the user to CWebUser. We would modify UserIdentity as follows,


class UserIdentity extends CUserIdentity
{
private $_id;

public function authenticate()
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($user->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->setState('userrole', $user->userrole_id);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}

public function getId()
{
return $this->_id;
}
}


In the above, during authentication we retrieve the ID and the last login time information of the authenticated user. We save the ID in a private variable $_id and save lastLoginTime in a state by calling setState(). The reason that we use different approaches to save id and lastLoginTime is because id is a pre-defined property that is recognized by CWebUser. If we want to store more information, we should use setState(), like we do with lastLoginTime.

We also override the getId() method to return the private variable $_id. The parent implementation is to return the username.

That's all we need. Now if we want to retrieve the id or lastLoginTime information in our code, we can do the following:

$id=Yii::app()->user->id;
$userrole=Yii::app()->user->userrole;
Read more
Resolved: Yii - How to customize a CGridView CbuttonColumn
posted by admin on December 25, 2015
Customize default buttons? :(Read more
Resolved: Yii captcha does not work after moving site to the hosting
posted by admin on December 22, 2015
In localhost website captcha working well, but after moving to live hosting, captcha stop working. Not show image, break image.Read more