Skip to content

Commit 31959ec

Browse files
committed
Restrict visible pastes to given :project.
1 parent 00764fb commit 31959ec

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

app/controllers/pastes_controller.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,8 @@ def destroy
9494
private
9595

9696
def find_paste_and_project
97-
if params[:project_id].present?
98-
@project = Project.find(params[:project_id])
99-
@pastes = @project.pastes
100-
else
101-
@pastes = Paste
102-
end
103-
@pastes = @pastes.visible(User.current)
97+
@project = Project.find(params[:project_id]) if params[:project_id].present?
98+
@pastes = Paste.visible(User.current, :project => @project)
10499

105100
if params[:id].present?
106101
if Paste.secure_id?(params[:id])

app/models/paste.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,21 @@ class Paste < ActiveRecord::Base
3535
scope :expired, where("expires_at <= current_timestamp")
3636
scope :unexpired, where("expires_at IS NULL OR expires_at > current_timestamp")
3737

38+
#
39+
# * Restrict to project, if given.
3840
#
3941
# * Admin users should be able to see all pastes (even secure ones.)
4042
#
4143
# * An ordinary user can see a secure paste only if he has authored it.
4244
#
4345
# * Never show expired pastes even to an admin.
4446
#
45-
scope :visible, lambda { |user, options={}|
46-
where(user.admin? ? nil : ["access_token IS NULL OR author_id = ?", user.id]).unexpired
47+
scope :visible, lambda{ |user, *args|
48+
options = args.first || {}
49+
s = self
50+
s = s.where(:project_id => options[:project]) if options[:project]
51+
s = s.where(["access_token IS NULL OR author_id = ?", user.id]) unless user.admin?
52+
s.unexpired
4753
}
4854

4955
acts_as_searchable :columns => ["#{table_name}.title", "#{table_name}.text"],

0 commit comments

Comments
 (0)