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.

144 CHAPTER 7 Basic access control<br />

That should be the last of it. When you run rake cucumber:ok, everything once again<br />

passes:<br />

16 scenarios (16 passed)<br />

158 steps (158 passed)<br />

Great! Now that accessing the actions is restricted, let’s make a commit here:<br />

git add .<br />

git commit -m "Restrict access to project<br />

actions to admins only"<br />

git push<br />

You should also hide the links from the users who are not admins, because it’s useless<br />

to show actions to people who can’t perform them.<br />

7.3.2 Hiding the New Project link<br />

Next you’ll learn how to hide certain links, such as the New Project link, from users<br />

who have no authorization to perform that action in your application. To begin,<br />

write a new feature called features/hidden_links.feature, which looks like the following<br />

listing.<br />

Listing 7.11 features/hidden_links.feature<br />

Feature: Hidden Links<br />

In order to clean up the user experience<br />

As the system<br />

I want to hide links from users who can't act on them<br />

Background:<br />

Given there are the following users:<br />

| email | password | admin |<br />

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

| admin@ticketee.com | password | true |<br />

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

Scenario: New project link is hidden for non-signed-in users<br />

Given I am on the homepage<br />

Then I should not see the "New Project" link<br />

Scenario: New project link is hidden for signed-in users<br />

Given I am signed in as "user@ticketee.com"<br />

Then I should not see the "New Project" link<br />

Scenario: New project link is shown to admins<br />

Given I am signed in as "admin@ticketee.com"<br />

Then I should see the "New Project" link<br />

When you run this feature using bin/cucumber features/hidden_links.feature,<br />

you’re given three new steps to define:<br />

Then /^I should not see the "([^\"]*)" link$/ do |arg1|<br />

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

end

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

Saved successfully!

Ooh no, something went wrong!