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.

Restricting write access<br />

When you run the spec again with bin/rspec spec/controllers/tickets<br />

_controller_spec.rb, it now passes because this user can no longer see this project<br />

and is shown the error:<br />

.<br />

1 example, 0 failures<br />

You scoped the project find for the TicketsController in the same way you did for<br />

the ProjectsController, limiting it to only those projects to which the current user<br />

has access.<br />

That’s the end of that! Now ensure that all your specs and features are passing by<br />

running rake cucumber:ok spec. You should see this output:<br />

31 scenarios (31 passed)<br />

285 steps (285 passed)<br />

# and<br />

20 examples, 0 failures, 10 pending<br />

In this section, you altered the TicketsController so that only users with permission<br />

to access a project can see the tickets inside it. Let’s commit that:<br />

git add .<br />

git commit -m "Restrict reading tickets to correct project scope"<br />

git push<br />

Now let’s add a new permission that restricts who can create tickets in a project.<br />

8.5 Restricting write access<br />

Sometimes when working on a project, you’ll want to limit the creation of tickets to a<br />

certain person or a group of people, such as to only developers or only clients. For<br />

this, you want the New link to be hidden from people who don’t have this permission,<br />

and you need both the new and create actions to reject such users.<br />

8.5.1 Rewriting a feature<br />

You’re lucky to already have the feature for creating tickets, so you just need to add a<br />

step to the Background declaring that the user can create tickets in the project. Place<br />

this step directly under the one declaring that users can view the project. Open<br />

features/creating_tickets.feature, and modify the Background so it contains these two<br />

lines:<br />

And "user@ticketee.com" can view the "Internet Explorer" project<br />

And "user@ticketee.com" can create tickets in the "Internet Explorer" project<br />

When you run bin/cucumber features/creating_tickets.feature, you’re told this<br />

step is undefined:<br />

Given /^"([^"]*)" can create tickets in the "([^"]*)"<br />

➥ project$/ do |arg1, arg2|<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

185

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

Saved successfully!

Ooh no, something went wrong!