You are viewing revision #6 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.
I found a better solution.
(Your login credentials may differ from my version so you will have to fit the script to your needs. I'm only telling what is the point)
Steps to follow:
- Add $user field to UserIdentity class
- Add getUser() method - getter for above property
- Add setUser($user) method - setter for above property which will assign user's properties info $user attribute.
My example UserIdentity class:
<?php
class UserIdentity extends CUserIdentity
{
/**
* User's attributes
* @var array
*/
public $user;
public function authenticate()
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
$user=User::model()->findByAttributes(array('email'=>CHtml::encode($this->username)));
if ($user)
{
if ($user->password === md5($user->salt.$this->password)) {
$this->errorCode=self::ERROR_NONE;
$this->setUser($user);
}
}
unset($user);
return !$this->errorCode;
}
public function getUser()
{
return $this->user;
}
public function setUser(CActiveRecord $user)
{
$this->user=$user->attributes;
}
}
?>
Now user's attributes has been set, create WebUser class and place it under /protected/components
class WebUser extends CWebUser
{
public function __get($name)
{
if ($this->hasState('__userInfo')) {
$user=$this->getState('__userInfo',array());
if (isset($user[$name])) {
return $user[$name];
}
}
return parent::__get($name);
}
public function login($identity, $duration) {
$this->setState('__userInfo', $identity->getUserinfo()->attributes);
parent::login($identity, $duration);
}
}
?>
remember to set that class as Yii::app()->user class:
<?php
'components'=>array(
'user'=>array(
'class'=>'WebUser',
)
)
?>
Should work now :) You can now access user's database fields as properties.
problem with using login in soap requests
public function login($identity, $duration)
must be
public function login($identity, $duration=0)
tested with yii-1.1.4.r2429
otherwise you will get the soap error
"looks like we got no XML document" when you using Yii::app()->user->login($identity); without duration.
Not right
To my way of thinking, it is important to remember that WebUser (which is user recognized by application) is not always related to Database user profile, and that your AR User is not your WebUser. Hence that, I prefer to call Yii::app()->user->model to get associated User model for current WebUser (if there is one).
Anyway, it is always nice to see a new cookbook article, thank you!
Hmm
$user does not need to be declared public if you have a getter and a setter.
Good Job
Good job man, this is awesome, You make my life easier :)
Yii Password Helper
Keep in mind that, starting from version 1.1.14 Yii has a password helper class that usees the strong blowfish algorithm. Take a look at
http://www.yiiframework.com/doc/api/1.1/CPasswordHelper
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.