Skip to content

Commit 0a42901

Browse files
author
Ryan Bigg
committed
Section 8.8: Restrict actions in TicketsController based on permissions and hide links
1 parent 3512c40 commit 0a42901

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed

ticketee/app/helpers/application_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ def title(*parts)
1010
def admins_only(&block)
1111
block.call if current_user.try(:admin?)
1212
end
13+
14+
def authorized?(permission, thing, &block)
15+
block.call if policy(thing).send("#{permission}?")
16+
end
1317
end

ticketee/app/policies/project_policy.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def initialize(user, project)
77
end
88

99
def write?
10+
user.admin? ||
1011
user.permissions.exists?(thing: project, action: :write)
1112
end
1213
end

ticketee/app/views/projects/show.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
class: "delete" %>
1818
<% end %>
1919

20-
<%= link_to "New Ticket",
21-
new_project_ticket_path(@project),
22-
class: "new" %>
20+
<% authorized?(:write, @project) do %>
21+
<%= link_to "New Ticket", new_project_ticket_path(@project) %>
22+
<% end %>
2323

2424
<div class='row'>
2525
<ul id='tickets'>

ticketee/app/views/tickets/show.html.erb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<div id='ticket'>
22
<h2><%= @ticket.title %></h2>
3-
<%= link_to "Edit Ticket",
4-
[:edit, @project, @ticket],
5-
class: "edit" %>
6-
<%= link_to "Delete Ticket", [@project, @ticket], method: :delete,
7-
data: { confirm: "Are you sure you want to delete this ticket?"},
8-
class: "delete" %>
3+
<%= authorized?("write", @project) do %>
4+
<%= link_to "Edit Ticket",
5+
[:edit, @project, @ticket],
6+
class: "edit" %>
7+
<%= link_to "Delete Ticket", [@project, @ticket], method: :delete,
8+
data: { confirm: "Are you sure you want to delete this ticket?"},
9+
class: "delete" %>
10+
<% end %>
911
<div id='author'>
1012
Created by <%= @ticket.author.email %>
1113
</div>

ticketee/spec/features/hidden_links_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
let(:user) { FactoryGirl.create(:user) }
55
let(:admin) { FactoryGirl.create(:user, :admin) }
66
let(:project) { FactoryGirl.create(:project) }
7+
let(:ticket) do
8+
FactoryGirl.create(:ticket, project: project,
9+
author: user)
10+
end
711

812
context "anonymous users" do
913
scenario "cannot see the New Project link" do
@@ -38,6 +42,53 @@
3842
visit project_path(project)
3943
assert_no_link_for "Delete Project"
4044
end
45+
46+
scenario "New ticket link is shown to a user with permission" do
47+
define_permission!(user, "read", project)
48+
define_permission!(user, "write", project)
49+
visit project_path(project)
50+
assert_link_for "New Ticket"
51+
end
52+
53+
scenario "New ticket link is hidden from a user without permission" do
54+
define_permission!(user, "read", project)
55+
visit project_path(project)
56+
assert_no_link_for "New Ticket"
57+
end
58+
59+
scenario "Edit ticket link is shown to a user with permission" do
60+
ticket
61+
define_permission!(user, "read", project)
62+
define_permission!(user, "write", project)
63+
visit project_path(project)
64+
click_link ticket.title
65+
assert_link_for "Edit Ticket"
66+
end
67+
68+
scenario "Edit ticket link is hidden from a user without permission" do
69+
ticket
70+
define_permission!(user, "read", project)
71+
visit project_path(project)
72+
click_link ticket.title
73+
assert_no_link_for "Edit Ticket"
74+
end
75+
76+
scenario "Delete ticket link is shown to a user with permission" do
77+
ticket
78+
define_permission!(user, "read", project)
79+
define_permission!(user, "write", project)
80+
visit project_path(project)
81+
click_link ticket.title
82+
assert_link_for "Delete Ticket"
83+
end
84+
85+
scenario "Delete ticket link is hidden from users without permission" do
86+
ticket
87+
define_permission!(user, "read", project)
88+
visit project_path(project)
89+
click_link ticket.title
90+
assert_no_link_for "Delete Ticket"
91+
end
4192
end
4293

4394
context "admin users" do
@@ -56,5 +107,24 @@
56107
visit project_path(project)
57108
assert_link_for "Delete Project"
58109
end
110+
111+
scenario "New ticket link is shown to admins" do
112+
visit project_path(project)
113+
assert_link_for "New Ticket"
114+
end
115+
116+
scenario "Edit ticket link is shown to admins" do
117+
ticket
118+
visit project_path(project)
119+
click_link ticket.title
120+
assert_link_for "Edit Ticket"
121+
end
122+
123+
scenario "Delete ticket link is shown to admins" do
124+
ticket
125+
visit project_path(project)
126+
click_link ticket.title
127+
assert_link_for "Delete Ticket"
128+
end
59129
end
60130
end

0 commit comments

Comments
 (0)