Skip to content

Commit e57003b

Browse files
committed
require context read access for discussion topics
test plan: * create a course with a discussion topic * keep it unpublished but add a student * as a student, should not be able to view the discussion topic through a direct link closes #CNVS-6384 Change-Id: I0c0bc66e377d870de6c97555e25e82a8226264f4 Reviewed-on: https://gerrit.instructure.com/48278 Tested-by: Jenkins Reviewed-by: Simon Williams <simon@instructure.com> QA-Review: Adam Stone <astone@instructure.com> Product-Review: James Williams <jamesw@instructure.com>
1 parent 9ef2e79 commit e57003b

9 files changed

Lines changed: 41 additions & 6 deletions

app/controllers/application_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ def require_context
420420
return @context != nil
421421
end
422422

423+
def require_context_and_read_access
424+
return require_context && authorized_action(@context, @current_user, :read)
425+
end
426+
423427
helper_method :clean_return_to
424428

425429
MAX_ACCOUNT_LINEAGE_TO_SHOW_IN_CRUMBS = 3

app/controllers/discussion_entries_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
# @API Discussion Topics
2020
class DiscussionEntriesController < ApplicationController
21-
before_filter :require_context, :except => :public_feed
21+
before_filter :require_context_and_read_access, :except => :public_feed
2222

2323
def show
2424
@entry = @context.discussion_entries.find(params[:id]).tap{|e| e.current_user = @current_user}
@@ -152,7 +152,7 @@ def public_feed
152152
render :template => "shared/unauthorized_feed", :layout => "layouts/application", :status => :bad_request, :formats => [:html] # :template => "shared/unauthorized_feed", :status => :bad_request
153153
return
154154
end
155-
if authorized_action(@topic, @current_user, :read)
155+
if authorized_action(@context, @current_user, :read) && authorized_action(@topic, @current_user, :read)
156156
@all_discussion_entries = @topic.discussion_entries.active
157157
@discussion_entries = @all_discussion_entries
158158
if request.format == :rss && !@topic.podcast_has_student_posts

app/controllers/discussion_topics_api_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DiscussionTopicsApiController < ApplicationController
2323
include Api::V1::DiscussionTopics
2424
include Api::V1::User
2525

26-
before_filter :require_context
26+
before_filter :require_context_and_read_access
2727
before_filter :require_topic
2828
before_filter :require_initial_post, except: [:add_entry, :mark_topic_read,
2929
:mark_topic_unread, :show,

app/controllers/discussion_topics_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
# }
218218
#
219219
class DiscussionTopicsController < ApplicationController
220-
before_filter :require_context, :except => :public_feed
220+
before_filter :require_context_and_read_access, :except => :public_feed
221221

222222
include Api::V1::DiscussionTopics
223223
include Api::V1::Assignment
@@ -694,6 +694,7 @@ def destroy
694694

695695
def public_feed
696696
return unless get_feed_context
697+
697698
feed = Atom::Feed.new do |f|
698699
f.title = t :discussion_feed_title, "%{title} Discussion Feed", :title => @context.name
699700
f.links << Atom::Link.new(:href => polymorphic_url([@context, :discussion_topics]), :rel => 'self')

spec/apis/v1/discussion_topics_api_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ def blank_fallback
431431
expect(json).to eq @response_json.merge("subscribed" => @topic.subscribed?(@user))
432432
end
433433

434+
it "should require course to be published for students" do
435+
@course.claim
436+
json = api_call(:get, "/api/v1/courses/#{@course.id}/discussion_topics/#{@topic.id}",
437+
{:controller => 'discussion_topics_api', :action => 'show', :format => 'json', :course_id => @course.id.to_s, :topic_id => @topic.id.to_s},
438+
{}, :expected_status => 401)
439+
end
440+
434441
it "should properly translate a video media comment in the discussion topic's message" do
435442
@topic.update_attributes(
436443
message: '<p><a id="media_comment_m-spHRwKY5ATHvPQAMKdZV_g" class="instructure_inline_media_comment video_comment" href="/media_objects/m-spHRwKY5ATHvPQAMKdZV_g">this is a media comment</a></p>'
@@ -1701,6 +1708,7 @@ def create_graded_discussion_for_da(assignment_opts={})
17011708
course_with_teacher
17021709
create_topic(@course, :title => "topic", :message => "topic")
17031710
course_with_observer_logged_in(:course => @course)
1711+
@course.offer
17041712
json = api_call(:get, "/api/v1/courses/#{@course.id}/discussion_topics.json",
17051713
{ :controller => 'discussion_topics', :action => 'index', :format => 'json',
17061714
:course_id => @course.id.to_s })

spec/controllers/discussion_entries_controller_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def topic_with_media_reply
4242
get 'show', :course_id => @course.id, :id => @entry.id
4343
assert_unauthorized
4444
end
45+
46+
it "should require course to be published for students" do
47+
user_session(@student)
48+
@course.claim
49+
get 'show', :course_id => @course.id, :id => @entry.id
50+
assert_unauthorized
51+
end
4552

4653
it "should assign variables" do
4754
user_session(@student)

spec/controllers/discussion_topics_controller_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def topic_entry
5151
get 'index', :course_id => @course.id
5252
assert_unauthorized
5353
end
54+
55+
it "should require the course to be published for students" do
56+
@course.claim
57+
user_session(@student)
58+
get 'index', :course_id => @course.id
59+
assert_unauthorized
60+
end
5461
end
5562

5663
describe "GET 'show'" do
@@ -60,6 +67,14 @@ def topic_entry
6067
assert_unauthorized
6168
end
6269

70+
it "should require the course to be published for students" do
71+
course_topic
72+
@course.claim
73+
user_session(@student)
74+
get 'show', :course_id => @course.id, :id => @topic.id
75+
assert_unauthorized
76+
end
77+
6378
it "should work for announcements in a public course" do
6479
@course.update_attribute(:is_public, true)
6580
@announcement = @course.announcements.create!(

spec/integration/context_module_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
describe ContextModule do
2222
def course_module
23-
course_with_student_logged_in
23+
course_with_student_logged_in(:active_all => true)
2424
@module = @course.context_modules.create!(:name => "some module")
2525
end
2626

spec/selenium/announcements_teacher_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
end
202202

203203
it "should remove delayed_post_at when unchecking delay_posting" do
204-
topic = announcement_model(:title => @topic_title, :user => @user, :delayed_post_at => 10.days.ago)
204+
topic = @course.announcements.create!(:title => @topic_title, :user => @user, :delayed_post_at => 10.days.ago, :message => "message")
205205
get "/courses/#{@course.id}/announcements/#{topic.id}"
206206
expect_new_page_load { f(".edit-btn").click }
207207

0 commit comments

Comments
 (0)