20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

class UserIdentity extends CUserIdentity<br />

{<br />

private $_id;<br />

public function authenticate()<br />

{<br />

$record=User::model()->findByAttributes(array(<br />

'username'=>$this->username<br />

));<br />

if($record===null)<br />

$this->errorCode=self::ERROR_USERNAME_INVALID;<br />

else if($record->password!==md5($this->password))<br />

$this->errorCode=self::ERROR_PASSWORD_INVALID;<br />

else<br />

{<br />

$this->_id=$record->id;<br />

$this->setState('title', $record->title);<br />

$this->errorCode=self::ERROR_NONE;<br />

}<br />

return !$this->errorCode;<br />

}<br />

}<br />

public function getId()<br />

{<br />

return $this->_id;<br />

}<br />

When we cover login and logout in the next section, we'll see that we pass this identity<br />

class into the login method for a user. Any information that we store in a state (by calling<br />

CBaseUserIdentity::setState) will be passed to CWebUser, which in turn will store them in<br />

a persistent storage, such as session. This information can then be accessed like<br />

properties of CWebUser. In our example, we stored the user title information via$this-<br />

>setState('title', $record->title);. Once we complete our login process, we can obtain the<br />

title information of the current user by simply using <strong>Yii</strong>::app()->user->title (This has been<br />

available since version 1.0.3. In prior versions, one must instead use <strong>Yii</strong>::app()->user-<br />

>getState('title').)<br />

Info: By default, CWebUser uses session as persistent storage for user identity<br />

information. If cookie-based login is enabled (by setting CWebUser::allowAutoLogin to<br />

be true), the user identity information may also be saved in cookie. Make sure you <strong>do</strong><br />

not declare sensitive information (e.g. password) to be persistent.<br />

Login and Logout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!