diff --git a/app/controllers/post_flags_controller.rb b/app/controllers/post_flags_controller.rb index 9fe07d0d2..fcb3b9dfa 100644 --- a/app/controllers/post_flags_controller.rb +++ b/app/controllers/post_flags_controller.rb @@ -1,14 +1,13 @@ class PostFlagsController < ApplicationController - before_action :member_only, :except => [:index, :show] respond_to :html, :xml, :json, :js def new - @post_flag = PostFlag.new(post_flag_params) + @post_flag = authorize PostFlag.new(permitted_attributes(PostFlag)) respond_with(@post_flag) end def index - @post_flags = PostFlag.paginated_search(params) + @post_flags = authorize PostFlag.paginated_search(params) if request.format.html? @post_flags = @post_flags.includes(:creator, post: [:flags, :uploader, :approver]) @@ -20,21 +19,16 @@ class PostFlagsController < ApplicationController end def create - @post_flag = PostFlag.create(post_flag_params.merge(creator: CurrentUser.user)) + @post_flag = authorize PostFlag.new(creator: CurrentUser.user, **permitted_attributes(PostFlag)) + @post_flag.save flash[:notice] = @post_flag.errors.none? ? "Post flagged" : @post_flag.errors.full_messages.join("; ") respond_with(@post_flag) end def show - @post_flag = PostFlag.find(params[:id]) + @post_flag = authorize PostFlag.find(params[:id]) respond_with(@post_flag) do |fmt| fmt.html { redirect_to post_flags_path(search: { id: @post_flag.id }) } end end - - private - - def post_flag_params - params.fetch(:post_flag, {}).permit(%i[post_id reason]) - end end diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 14af3523f..cf309a276 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -335,27 +335,21 @@ class PostQueryBuilder end end - if q[:flagger_ids_neg] - q[:flagger_ids_neg].each do |flagger_id| - if CurrentUser.can_view_flagger?(flagger_id) - post_ids = PostFlag.unscoped.search(:creator_id => flagger_id, :category => "normal").reorder("").select {|flag| flag.not_uploaded_by?(CurrentUser.id)}.map {|flag| flag.post_id}.uniq - if post_ids.any? - relation = relation.where.not("posts.id": post_ids) - end - end - end + q[:flaggers_neg].to_a.each do |flagger| + flags = PostFlag.unscoped.creator_matches(flagger, CurrentUser.user).where("post_id = posts.id").select("1") + relation = relation.where("NOT EXISTS (#{flags.to_sql})") end - if q[:flagger_ids] - q[:flagger_ids].each do |flagger_id| - if flagger_id == "any" - relation = relation.where('EXISTS (' + PostFlag.unscoped.search(:category => "normal").where('post_id = posts.id').reorder('').select('1').to_sql + ')') - elsif flagger_id == "none" - relation = relation.where('NOT EXISTS (' + PostFlag.unscoped.search(:category => "normal").where('post_id = posts.id').reorder('').select('1').to_sql + ')') - elsif CurrentUser.can_view_flagger?(flagger_id) - post_ids = PostFlag.unscoped.search(creator_id: flagger_id, category: "normal").reorder("").select {|flag| flag.not_uploaded_by?(CurrentUser.id)}.map(&:post_id).uniq - relation = relation.where("posts.id": post_ids) - end + q[:flaggers].to_a.each do |flagger| + if flagger == "any" + flags = PostFlag.unscoped.category_matches("normal").where("post_id = posts.id").select("1") + relation = relation.where("EXISTS (#{flags.to_sql})") + elsif flagger == "none" + flags = PostFlag.unscoped.category_matches("normal").where("post_id = posts.id").select("1") + relation = relation.where("NOT EXISTS (#{flags.to_sql})") + else + flags = PostFlag.unscoped.creator_matches(flagger, CurrentUser.user).where("post_id = posts.id").select("1") + relation = relation.where("EXISTS (#{flags.to_sql})") end end @@ -738,28 +732,29 @@ class PostQueryBuilder end when "flagger" - q[:flagger_ids] ||= [] - if g2 == "none" - q[:flagger_ids] << "none" + q[:flaggers] ||= [] + q[:flaggers] << "none" elsif g2 == "any" - q[:flagger_ids] << "any" + q[:flaggers] ||= [] + q[:flaggers] << "any" else - user_id = User.name_to_id(g2) - q[:flagger_ids] << user_id unless user_id.blank? + user = User.find_by_name(g2) + q[:flaggers] ||= [] + q[:flaggers] << user unless user.blank? end when "-flagger" if g2 == "none" - q[:flagger_ids] ||= [] - q[:flagger_ids] << "any" + q[:flaggers] ||= [] + q[:flaggers] << "any" elsif g2 == "any" - q[:flagger_ids] ||= [] - q[:flagger_ids] << "none" + q[:flaggers] ||= [] + q[:flaggers] << "none" else - q[:flagger_ids_neg] ||= [] - user_id = User.name_to_id(g2) - q[:flagger_ids_neg] << user_id unless user_id.blank? + user = User.find_by_name(g2) + q[:flaggers_neg] ||= [] + q[:flaggers_neg] << user unless user.blank? end when "appealer" diff --git a/app/models/post_event.rb b/app/models/post_event.rb index a3dffc352..bbad886c6 100644 --- a/app/models/post_event.rb +++ b/app/models/post_event.rb @@ -48,7 +48,7 @@ class PostEvent true when PostFlag flag = event - user.can_view_flagger_on_post?(flag) + Pundit.policy!([user, nil], flag).can_view_flagger? end end diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index f7e792be2..65a61ca4b 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -26,42 +26,47 @@ class PostFlag < ApplicationRecord scope :old, -> { where("post_flags.created_at <= ?", 3.days.ago) } module SearchMethods + def creator_matches(creator, searcher) + policy = Pundit.policy!([searcher, nil], PostFlag.new(creator: creator)) + + if policy.can_view_flagger? + where(creator: creator).where.not(post: searcher.posts) + else + none + end + end + + def category_matches(category) + case category + when "normal" + where("reason NOT IN (?) AND reason NOT LIKE ?", [Reasons::UNAPPROVED], Reasons::REJECTED) + when "unapproved" + where(reason: Reasons::UNAPPROVED) + when "rejected" + where("reason LIKE ?", Reasons::REJECTED) + when "deleted" + where("reason = ? OR reason LIKE ?", Reasons::UNAPPROVED, Reasons::REJECTED) + else + none + end + end + def search(params) q = super q = q.search_attributes(params, :post, :is_resolved, :reason) q = q.text_attribute_matches(:reason, params[:reason_matches]) - # XXX if params[:creator_id].present? - if CurrentUser.can_view_flagger?(params[:creator_id].to_i) - q = q.where.not(post_id: CurrentUser.user.posts) - q = q.where("creator_id = ?", params[:creator_id].to_i) - else - q = q.none - end + flagger = User.find(params[:creator_id]) + q = q.creator_matches(flagger, CurrentUser.user) + elsif params[:creator_name].present? + flagger = User.find_by_name(params[:creator_name]) + q = q.creator_matches(flagger, CurrentUser.user) end - # XXX - if params[:creator_name].present? - flagger_id = User.name_to_id(params[:creator_name].strip) - if flagger_id && CurrentUser.can_view_flagger?(flagger_id) - q = q.where.not(post_id: CurrentUser.user.posts) - q = q.where("creator_id = ?", flagger_id) - else - q = q.none - end - end - - case params[:category] - when "normal" - q = q.where("reason NOT IN (?) AND reason NOT LIKE ?", [Reasons::UNAPPROVED], Reasons::REJECTED) - when "unapproved" - q = q.where(reason: Reasons::UNAPPROVED) - when "rejected" - q = q.where("reason LIKE ?", Reasons::REJECTED) - when "deleted" - q = q.where("reason = ? OR reason LIKE ?", Reasons::UNAPPROVED, Reasons::REJECTED) + if params[:category] + q = q.category_matches(params[:category]) end q.apply_default_order(params) @@ -71,7 +76,7 @@ class PostFlag < ApplicationRecord module ApiMethods def api_attributes attributes = super + [:category] - attributes -= [:creator_id] unless CurrentUser.can_view_flagger_on_post?(self) + attributes -= [:creator_id] unless Pundit.policy!([CurrentUser.user, nil], self).can_view_flagger? attributes end end @@ -131,10 +136,6 @@ class PostFlag < ApplicationRecord post.uploader_id end - def not_uploaded_by?(userid) - uploader_id != userid - end - def self.available_includes [:post] end diff --git a/app/models/user.rb b/app/models/user.rb index ccc63785c..6f03d467d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -430,14 +430,6 @@ class User < ApplicationRecord created_at <= 1.week.ago end - def can_view_flagger?(flagger_id) - is_moderator? || flagger_id == id - end - - def can_view_flagger_on_post?(flag) - (is_moderator? && flag.not_uploaded_by?(id)) || flag.creator_id == id - end - def upload_limit @upload_limit ||= UploadLimit.new(self) end diff --git a/app/policies/post_flag_policy.rb b/app/policies/post_flag_policy.rb new file mode 100644 index 000000000..09207cbe5 --- /dev/null +++ b/app/policies/post_flag_policy.rb @@ -0,0 +1,13 @@ +class PostFlagPolicy < ApplicationPolicy + def can_search_flagger? + user.is_moderator? + end + + def can_view_flagger? + (user.is_moderator? || record.creator_id == user.id) && (record.post&.uploader_id != user.id) + end + + def permitted_attributes + [:post_id, :reason] + end +end diff --git a/app/views/post_flags/_reasons.html.erb b/app/views/post_flags/_reasons.html.erb index 18063234e..7395e182a 100644 --- a/app/views/post_flags/_reasons.html.erb +++ b/app/views/post_flags/_reasons.html.erb @@ -3,7 +3,7 @@