Skip to content

Commit 5274457

Browse files
author
Ryan Bigg
committed
Section 8.3.5: Don’t show projects that a user doesn't have permission to see
1 parent d34c458 commit 5274457

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

ticketee/app/controllers/application_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ class ApplicationController < ActionController::Base
55

66
private
77

8+
def after_sign_out_path_for(resource_or_scope)
9+
new_user_session_path
10+
end
11+
812
def authorize_admin!
913
authenticate_user!
1014

ticketee/app/controllers/projects_controller.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class ProjectsController < ApplicationController
22
before_action :authorize_admin!, except: [:index, :show]
3-
before_action :authenticate_user!, only: [:show]
3+
before_action :authenticate_user!, only: [:index, :show]
44
before_action :set_project, only: [:show,
55
:edit,
66
:update,
77
:destroy]
88
def index
9-
@projects = Project.all
9+
@projects = Project.for(current_user)
1010
end
1111

1212
def new
@@ -46,11 +46,7 @@ def destroy
4646
private
4747

4848
def set_project
49-
@project = if current_user.admin?
50-
Project.find(params[:id])
51-
else
52-
Project.readable_by(current_user).find(params[:id])
53-
end
49+
@project = Project.for(current_user).find(params[:id])
5450
rescue ActiveRecord::RecordNotFound
5551
flash[:alert] = "The project you were looking" +
5652
" for could not be found."

ticketee/app/models/project.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ class Project < ActiveRecord::Base
99
joins(:permissions).where(permissions: { action: "read",
1010
user_id: user.id })
1111
end
12+
13+
scope :for, ->(user) do
14+
user.admin? ? all : readable_by(user)
15+
end
1216
end

ticketee/spec/features/signing_up_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
feature "Sign up" do
44
it "a user can sign up" do
55
visit "/"
6-
click_link "Sign up"
6+
within("nav") do
7+
click_link "Sign up"
8+
end
79
fill_in "Email", with: "[email protected]"
810
fill_in "user_password", with: "password"
911
fill_in "Password confirmation", with: "password"

ticketee/spec/features/viewing_projects_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
end
1111

1212
scenario "Listing all projects" do
13+
FactoryGirl.create(:project, name: "Hidden")
1314
visit "/"
15+
expect(page).to_not have_content("Hidden")
1416
click_link project.name
1517

1618
expect(page.current_url).to eql(project_url(project))

0 commit comments

Comments
 (0)