27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

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.

Restricting read access<br />

you may have a large number of projects and want to assign a user to only one of<br />

them. Whitelist authorization involves fewer steps in restricting a user to one project.<br />

A good way to think about whitelist authorization is as the kind of list a security<br />

guard would have at an event. If you’re not on the list, you don’t get in. A blacklist<br />

comparison would be if the security guard had a list of people who weren’t allowed in.<br />

This chapter guides you through restricting access to the CRUD operations of<br />

TicketsController one by one, starting with reading and then moving into creating,<br />

updating, and deleting. Any time users want to perform one of these actions, they<br />

must be granted permission to do so, or added to “the list.”<br />

During this process, you’ll see another gem called CanCan, which provides some<br />

methods for your controllers and views that help you check the current user’s permission<br />

to perform a specific action.<br />

You first set up permissions through the Cucumber features, and once you’re done<br />

with restricting the actions in your controller, you’ll generate functionality in the<br />

backend to allow administrators of the application to assign permissions to users.<br />

8.1 Restricting read access<br />

A time comes in every ticket-tracking application’s life when it’s necessary to restrict<br />

which users can see which projects. For example, you could be operating in a consultancy<br />

where some people are working on one application, and others are working on<br />

another. You want the admins of the application to be able to customize which projects<br />

each user can see.<br />

First, you create a model called Permission that tracks which users have which permissions<br />

for which actions. Before you create that model, you must update one of<br />

your Viewing Projects features to make sure only users who have permission to view a<br />

project are able to do so.<br />

Add a background and change the scenario in this feature to set up a user with the<br />

correct permissions, and then make the user visit that project, changing the code in<br />

the scenario in this feature to what is shown in the following listing.<br />

Listing 8.1 features/viewing_projects.feature<br />

Background:<br />

Given there are the following users:<br />

| email | password |<br />

| user@ticketee.com | password |<br />

And I am signed in as them<br />

And there is a project called "TextMate 2"<br />

And "user@ticketee.com" can view the "TextMate 2" project<br />

Scenario: Listing all projects<br />

And I am on the homepage<br />

When I follow "TextMate 2"<br />

Then I should be on the project page for "TextMate 2"<br />

You’ve effectively rewritten a large portion of this feature, which is common practice<br />

when implementing such large changes.<br />

B<br />

165<br />

Let user<br />

view project

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

Saved successfully!

Ooh no, something went wrong!