Skip to content

Commit d5d7df6

Browse files
author
Ryan Bigg
committed
Section 8.4: Restrict reading tickets to correct project scope
1 parent 5274457 commit d5d7df6

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

ticketee/app/controllers/tickets_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class TicketsController < ApplicationController
2+
before_action :authenticate_user!
23
before_action :set_project
34
before_action :set_ticket, only: [:show, :edit, :update, :destroy]
45

@@ -42,7 +43,11 @@ def ticket_params
4243
end
4344

4445
def set_project
45-
@project = Project.find(params[:project_id])
46+
@project = Project.for(current_user).find(params[:project_id])
47+
rescue ActiveRecord::RecordNotFound
48+
flash[:alert] = "The project you were looking " +
49+
"for could not be found."
50+
redirect_to root_path
4651
end
4752

4853
def set_ticket
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
require 'rails_helper'
1+
require "rails_helper"
22

3-
RSpec.describe TicketsController, :type => :controller do
3+
RSpec.describe TicketsController, type: :controller do
4+
let(:user) { FactoryGirl.create(:user) }
5+
let(:project) { FactoryGirl.create(:project) }
6+
let(:ticket) { FactoryGirl.create(:ticket,
7+
project: project,
8+
author: user) }
49

10+
context "standard users" do
11+
before do
12+
allow(controller).to receive(:authenticate_user!)
13+
allow(controller).to receive(:current_user).and_return(user)
14+
end
15+
16+
it "cannot access a ticket for a project" do
17+
get :show, id: ticket.id, project_id: project.id
18+
19+
expect(response).to redirect_to(root_path)
20+
expect(flash[:alert]).to eql("The project you were looking " +
21+
"for could not be found.")
22+
end
23+
end
524
end

0 commit comments

Comments
 (0)