27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

120 CHAPTER 6 Authentication and basic authorization<br />

Table 6.1 Devise default modules (continued)<br />

Module Provides<br />

:trackable Adds functionality to track users, such as how many times they<br />

sign in, when they last signed in, and the current and last IPs<br />

they signed in from.<br />

:validatable Validates the user has entered correct data, such as a valid<br />

email address and password.<br />

Table 6.2 Devise optional modules (off by default)<br />

Module Provides<br />

:token_authenticatable Lets the user authenticate via a token; can be used in conjunction<br />

with :database_authenticatable<br />

:encryptable Adds support for other methods of encrypting passwords; by<br />

default, Devise uses bcrypt<br />

:confirmable When users register, sends them an email with a link they click to<br />

confirm they’re a real person (you’ll switch on this module shortly<br />

because it’s one step to prevent automated signups)<br />

:lockable Locks the user out for a specific amount of time after a specific<br />

number of retries (configurable in the initializer); default is a lockout<br />

time of 1 hour after 20 retries<br />

:timeoutable If users have no activity in their session for a specified period of<br />

time, they are automatically signed out; useful for sites that may<br />

be used by multiple people on the same computer, such as email<br />

or banking sites<br />

:omniauthable Adds support for the OmniAuth gem, which allows for alternative<br />

authentication methods using services such as OAuth and<br />

OpenID<br />

The devise call is followed by a call to attr_accessible. This method defines fields<br />

that are accessible via attribute mass-assignment. Attribute mass-assignment happens<br />

when you pass a whole slew of attributes to a method such as create or<br />

update_attributes; because these methods take any and all parameters passed to<br />

them by default, users may attempt to hack the form and set an attribute they are not<br />

supposed to set, such as an admin boolean attribute. By using attr_accessible, you<br />

define a white list of fields you want the user to access. Any other fields passed<br />

through in an attribute mass-assignment are ignored.<br />

The final step here is to run rake db:migrate to create the users table from the<br />

Devise-provided migration in your development database and run rake<br />

db:test:prepare so it’s created in the test database too.

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

Saved successfully!

Ooh no, something went wrong!