1 class IssuesController < ApplicationController
4 before_action :authorize_web
5 before_action :require_user
6 before_action :set_issues
7 before_action :check_permission, :only => [:index, :show, :resolve, :open, :ignore, :comment]
8 before_action :find_issue, :only => [:show, :resolve, :reopen, :ignore]
9 before_action :setup_user_role, :only => [:show, :index]
11 helper_method :sort_column, :sort_direction
15 @issue_types = @moderator_issues
16 @users = User.joins(:roles).where(:user_roles => { :role => "moderator" })
18 @issue_types = @admin_issues
19 @users = User.joins(:roles).where(:user_roles => { :role => "administrator" })
22 @issues = Issue.where(:issue_type => @user_role).order(sort_column + " " + sort_direction)
25 if params[:search_by_user] && params[:search_by_user].present?
26 @find_user = User.find_by(:display_name => params[:search_by_user])
28 @issues = @issues.where(:reported_user_id => @find_user.id)
30 notice = t("issues.index.search.user_not_found")
34 if params[:status] && params[:status][0].present?
35 @issues = @issues.where(:status => params[:status][0].to_i)
38 if params[:issue_type] && params[:issue_type][0].present?
39 @issues = @issues.where(:reportable_type => params[:issue_type][0])
43 if params[:last_updated_by] && params[:last_updated_by][0].present?
44 last_updated_by = params[:last_updated_by][0].to_s == "nil" ? nil : params[:last_updated_by][0].to_i
45 @issues = @issues.where(:updated_by => last_updated_by)
48 notice = t("issues.index.search.issues_not_found") if @issues.first.nil?
50 if params[:last_reported_by] && params[:last_reported_by][0].present?
51 last_reported_by = params[:last_reported_by][0].to_s == "nil" ? nil : params[:last_reported_by][0].to_i
52 @issues = @issues.where(:updated_by => last_reported_by)
55 redirect_to issues_path, :notice => notice if notice
59 @read_reports = @issue.read_reports
60 @unread_reports = @issue.unread_reports
61 @comments = @issue.comments
62 @related_issues = @issue.reported_user.issues.where(:issue_type => @user_role)
66 if create_new_issue_params.present?
67 @issue = Issue.find_or_initialize_by(create_new_issue_params)
68 path = "issues.report_strings." + @issue.reportable.class.name.to_s
69 @report_strings_yaml = t(path)
74 @issue = Issue.find_by(:reportable_id => params[:reportable_id], :reportable_type => params[:reportable_type])
75 # Check if Issue already exists
77 @issue = Issue.find_or_initialize_by(issue_params)
78 @issue.updated_by = nil
80 # Reassign to moderators if it is a moderator issue
81 @issue.issue_type = "administrator"
82 reassign_issue if @moderator_issues.include? @issue.reportable.class.name
85 # Check if details provided are sufficient
86 if check_report_params
87 @report = @issue.reports.build(report_params)
88 details = report_details
89 @report.reporter_user_id = @user.id
90 @report.details = details
91 # Checking if instance has been updated since last report
92 @last_report = @issue.reports.order(:updated_at => :desc).last
94 @issue.save! if @issue.reopen
98 @issue.report_count = @issue.reports.count
101 @admins_or_mods = UserRole.where(:role => @issue.issue_type)
102 @admins_or_mods.each do |user|
103 Notifier.new_issue_notification(@issue.id, User.find(user.user_id)).deliver_now
106 redirect_back "/", :notice => t("issues.create.successful_report")
109 redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id), :notice => t("issues.create.provide_details")
114 @issue = Issue.find_by(issue_params)
115 # Check if details provided are sufficient
116 if check_report_params
117 @report = @issue.reports.where(:reporter_user_id => @user.id).first
120 @report = @issue.reports.build(report_params)
121 @report.reporter_user_id = @user.id
122 notice = t("issues.update.new_report")
125 details = report_details
126 @report.details = details
128 # Checking if instance has been updated since last report
129 @last_report = @issue.reports.order(:updated_at => :desc).last
135 notice = t("issues.update.successful_update") if notice.nil?
138 @issue.report_count = @issue.reports.count
140 redirect_back "/", :notice => notice
143 redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id), :notice => t("issues.update.provide_details")
148 @issue = Issue.find(params[:id])
149 if issue_comment_params.blank?
150 notice = t("issues.comment.provide_details")
152 @issue_comment = @issue.comments.build(issue_comment_params)
153 @issue_comment.commenter_user_id = @user.id
156 @issue_comment.reassign = true
159 @issue.updated_by = @user.id
161 notice = t("issues.comment.comment_created")
163 redirect_to @issue, :notice => notice
166 # Status Transistions
170 redirect_to @issue, :notice => t("issues.resolved")
178 @issue.updated_by = @user.id
180 redirect_to @issue, :notice => t("issues.ignored")
188 @issue.updated_by = @user.id
190 redirect_to @issue, :notice => t("issues.reopened")
196 # Reassign Issues between Administrators and Moderators
198 @issue.issue_type = upgrade_issue(@issue.issue_type)
204 def upgrade_issue(type)
205 if type == "moderator"
213 @admin_issues = %w[DiaryEntry DiaryComment User]
214 @moderator_issues = %w[Changeset Note]
219 @user_role = @user.administrator? ? "administrator" : "moderator"
223 if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
231 params[:report][:details] + "--||--" + params[:report_type].to_s + "--||--"
234 def check_report_params
235 params[:report] && params[:report][:details] && params[:report_type]
239 @issue = Issue.find(params[:id])
243 unless @user.administrator? || @user.moderator?
244 flash[:error] = t("application.require_admin.not_an_admin")
245 redirect_to root_path
249 def create_new_issue_params
250 params.permit(:reportable_id, :reportable_type)
254 params[:issue].permit(:reportable_id, :reportable_type)
258 params[:report].permit(:details)
261 def issue_comment_params
262 params.require(:issue_comment).permit(:body)
266 Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
270 %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
273 # back-port of ActionController#redirect_back from rails 5
274 def redirect_back(fallback_location, **args)
275 if referer = request.headers["Referer"]
276 redirect_to referer, **args
278 redirect_to fallback_location, **args