diff --git a/app/controllers/moderation_reports_controller.rb b/app/controllers/moderation_reports_controller.rb index 21a23b9d3..a36f3e13c 100644 --- a/app/controllers/moderation_reports_controller.rb +++ b/app/controllers/moderation_reports_controller.rb @@ -1,49 +1,28 @@ class ModerationReportsController < ApplicationController respond_to :html, :xml, :json, :js - before_action :member_only, only: [:new, :create] - before_action :moderator_only, only: [:index] def new - @moderation_report = ModerationReport.new(moderation_report_params) - check_privilege(@moderation_report) + @moderation_report = authorize ModerationReport.new(permitted_attributes(ModerationReport)) respond_with(@moderation_report) end def index - @moderation_reports = ModerationReport.visible(CurrentUser.user).paginated_search(params, count_pages: true) + @moderation_reports = authorize ModerationReport.visible(CurrentUser.user).paginated_search(params, count_pages: true) @moderation_reports = @moderation_reports.includes(:creator, :model) if request.format.html? respond_with(@moderation_reports) end def show + authorize ModerationReport redirect_to moderation_reports_path(search: { id: params[:id] }) end def create - @moderation_report = ModerationReport.new(moderation_report_params.merge(creator: CurrentUser.user)) - check_privilege(@moderation_report) + @moderation_report = authorize ModerationReport.new(creator: CurrentUser.user, **permitted_attributes(ModerationReport)) @moderation_report.save flash.now[:notice] = @moderation_report.valid? ? "Report submitted" : @moderation_report.errors.full_messages.join("; ") respond_with(@moderation_report) end - - private - - def model_type - params.fetch(:moderation_report, {}).fetch(:model_type) - end - - def model_id - params.fetch(:moderation_report, {}).fetch(:model_id) - end - - def check_privilege(moderation_report) - raise User::PrivilegeError unless moderation_report.model.reportable_by?(CurrentUser.user) - end - - def moderation_report_params - params.fetch(:moderation_report, {}).permit(%i[model_type model_id reason]) - end end diff --git a/app/models/comment.rb b/app/models/comment.rb index ea4b36e9e..ca4231caa 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -117,10 +117,6 @@ class Comment < ApplicationRecord end end - def reportable_by?(user) - creator_id != user.id && !creator.is_moderator? - end - def voted_by?(user) return false if user.is_anonymous? user.id.in?(votes.map(&:user_id)) diff --git a/app/models/dmail.rb b/app/models/dmail.rb index 86417d78f..a155582ff 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -169,10 +169,6 @@ class Dmail < ApplicationRecord end end - def reportable_by?(user) - owner == user && is_recipient? && !is_automated? && !from.is_moderator? - end - def dtext_shortlink(key: false, **options) key ? "dmail ##{id}/#{self.key}" : "dmail ##{id}" end diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 0bcb3397a..ee15d1904 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -81,10 +81,6 @@ class ForumPost < ApplicationRecord end end - def reportable_by?(user) - visible?(user) && creator_id != user.id && !creator.is_moderator? - end - def votable? bulk_update_request.present? && bulk_update_request.is_pending? end @@ -99,10 +95,6 @@ class ForumPost < ApplicationRecord end end - def visible?(user, show_deleted_posts = false) - user.is_moderator? || (user.level >= topic.min_level && (show_deleted_posts || !is_deleted?)) - end - def update_topic_updated_at_on_create if topic # need to do this to bypass the topic's original post from getting touched diff --git a/app/models/user.rb b/app/models/user.rb index 31203ddfd..ccc63785c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -697,10 +697,6 @@ class User < ApplicationRecord CurrentUser.as(self, &block) end - def reportable_by?(user) - ModerationReport.enabled? && user.is_builder? && id != user.id && !is_moderator? - end - def hide_favorites? !CurrentUser.is_admin? && enable_private_favorites? && CurrentUser.user.id != id end diff --git a/app/policies/comment_policy.rb b/app/policies/comment_policy.rb index cfa1f55ed..2d0583275 100644 --- a/app/policies/comment_policy.rb +++ b/app/policies/comment_policy.rb @@ -3,6 +3,10 @@ class CommentPolicy < ApplicationPolicy unbanned? && (user.is_moderator? || record.updater_id == user.id) end + def reportable? + unbanned? && record.creator_id != user.id && !record.creator.is_moderator? + end + def can_sticky_comment? user.is_moderator? end diff --git a/app/policies/dmail_policy.rb b/app/policies/dmail_policy.rb index effb614d1..5dc03a0b5 100644 --- a/app/policies/dmail_policy.rb +++ b/app/policies/dmail_policy.rb @@ -19,6 +19,10 @@ class DmailPolicy < ApplicationPolicy user.is_member? && (record.owner_id == user.id || record.valid_key?(request.params[:key])) end + def reportable? + unbanned? && record.owner_id == user.id && record.is_recipient? && !record.is_automated? && !record.from.is_moderator? + end + def permitted_attributes_for_create [:title, :body, :to_name, :to_id] end diff --git a/app/policies/forum_post_policy.rb b/app/policies/forum_post_policy.rb index 34787cef3..a444b16a0 100644 --- a/app/policies/forum_post_policy.rb +++ b/app/policies/forum_post_policy.rb @@ -19,6 +19,10 @@ class ForumPostPolicy < ApplicationPolicy unbanned? && show? && user.is_moderator? end + def reportable? + unbanned? && show? && record.creator_id != user.id && !record.creator.is_moderator? + end + def show_deleted? !record.is_deleted? || user.is_moderator? end diff --git a/app/policies/moderation_report_policy.rb b/app/policies/moderation_report_policy.rb new file mode 100644 index 000000000..12eef8914 --- /dev/null +++ b/app/policies/moderation_report_policy.rb @@ -0,0 +1,13 @@ +class ModerationReportPolicy < ApplicationPolicy + def index? + user.is_moderator? + end + + def create? + unbanned? && policy(record.model).reportable? + end + + def permitted_attributes + [:model_type, :model_id, :reason] + end +end diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 1ed65844f..7f600efb2 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -15,6 +15,10 @@ class UserPolicy < ApplicationPolicy user.is_member? end + def reportable? + false + end + def fix_counts? user.is_member? end diff --git a/app/views/comments/partials/show/_comment.html.erb b/app/views/comments/partials/show/_comment.html.erb index fcaec4e76..6a4365ba6 100644 --- a/app/views/comments/partials/show/_comment.html.erb +++ b/app/views/comments/partials/show/_comment.html.erb @@ -12,7 +12,7 @@ data-is-deleted="<%= comment.is_deleted? %>" data-is-sticky="<%= comment.is_sticky? %>" data-below-threshold="<%= comment.score < CurrentUser.user.comment_threshold %>" - <% if CurrentUser.is_moderator? %> + <% if policy(moderation_reports).show? %> data-is-reported="<%= moderation_reports.pluck(:model_id).include?(comment.id) %>" <% end %> data-is-voted="<%= comment.voted_by?(CurrentUser.user) %>"> @@ -56,7 +56,7 @@