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.

Now that we have seen an example of creating a user identity, we use this to help ease<br />

the implementation of our needed login and logout actions. The following code<br />

demonstrates how this is accomplished:<br />

// Login a user with the provided username and password.<br />

$identity=new UserIdentity($username,$password);<br />

if($identity->authenticate())<br />

<strong>Yii</strong>::app()->user->login($identity);<br />

else<br />

echo $identity->errorMessage;<br />

......<br />

// Logout the current user<br />

<strong>Yii</strong>::app()->user->logout();<br />

Here we are creating a new UserIdentity object and passing in the authentication<br />

credentials (i.e. the$username and $password values submitted by the user) to its<br />

constructor. We then simply call the authenticate() method. If successful, we pass the<br />

identity information into the CWebUser::login method, which will store the identity<br />

information into persistent storage (PHP session by default) for retrieval upon subsequent<br />

requests. If the authentication fails, we can interrogate the errorMessage property for more<br />

information as to why it failed.<br />

Whether or not a user has been authenticated can easily be checked throughout the<br />

application by using <strong>Yii</strong>::app()->user->isGuest. If using persistent storage like session (the<br />

default) and/or a cookie (discussed below) to store the identity information, the user can<br />

remain logged in upon subsequent requests. In this case, we <strong>do</strong>n't need to use the<br />

UserIdentity class and the entire login process upon each request. Rather CWebUser will<br />

automatically take care of loading the identity information from this persistent storage and<br />

will use it to determine whether <strong>Yii</strong>::app()->user->isGuest returns true or false.<br />

Cookie-based Login<br />

By default, a user will be logged out after a certain period of inactivity, depending on the<br />

session configuration. To change this behavior, we can set the allowAutoLogin property of<br />

the user component to be true and pass a duration parameter to the CWebUser::login<br />

method. The user will then remain logged in for the specified duration, even if he closes<br />

his browser win<strong>do</strong>w. Note that this feature requires the user's browser to accept cookies.<br />

// Keep the user logged in for 7 days.<br />

// Make sure allowAutoLogin is set true for the user component.<br />

<strong>Yii</strong>::app()->user->login($identity,3600*24*7);<br />

As we mentioned above, when cookie-based login is enabled, the states stored via<br />

CBaseUserIdentity::setState will be saved in the cookie as well. The next time when the<br />

user is logged in, these states will be read from the cookie and made accessible via<br />

<strong>Yii</strong>::app()->user.

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

Saved successfully!

Ooh no, something went wrong!