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].blank?
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].blank?
35 @issues = @issues.where(:status => params[:status][0].to_i)
38 if params[:issue_type] && !params[:issue_type][0].blank?
39 @issues = @issues.where(:reportable_type => params[:issue_type][0])
43 if params[:last_updated_by] && !params[:last_updated_by][0].blank?
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].blank?
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.user.issues.where(:issue_type => @user_role)
64 @updated_by_admin = User.find(@issue.updated_by) if @issue.updated_by
68 unless create_new_issue_params.blank?
69 @issue = Issue.find_or_initialize_by(create_new_issue_params)
70 path = "issues.report_strings." + @issue.reportable.class.name.to_s
71 @report_strings_yaml = t(path)
76 @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id], params[:reportable_type])
77 # Check if Issue already exists
79 @issue = Issue.find_or_initialize_by(issue_params)
80 @issue.updated_by = nil
82 # Reassign to moderators if it is a moderator issue
83 @issue.issue_type = "administrator"
84 reassign_issue if @moderator_issues.include? @issue.reportable.class.name
87 # Check if details provided are sufficient
88 if check_report_params
89 @report = @issue.reports.build(report_params)
90 details = report_details
91 @report.reporter_user_id = @user.id
92 @report.details = details
93 # Checking if instance has been updated since last report
94 @last_report = @issue.reports.order(:updated_at => :desc).last
96 @issue.save! if @issue.reopen
100 @issue.report_count = @issue.reports.count
103 @admins_or_mods = UserRole.where(:role => @issue.issue_type)
104 @admins_or_mods.each do |user|
105 Notifier.new_issue_notification(@issue.id, User.find(user.user_id)).deliver_now
108 redirect_back "/", :notice => t("issues.create.successful_report")
111 redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id, :reported_user_id => @issue.reported_user_id), :notice => t("issues.create.provide_details")
116 @issue = Issue.find_by(issue_params)
117 # Check if details provided are sufficient
118 if check_report_params
119 @report = @issue.reports.where(:reporter_user_id => @user.id).first
122 @report = @issue.reports.build(report_params)
123 @report.reporter_user_id = @user.id
124 notice = t("issues.update.new_report")
127 details = report_details
128 @report.details = details
130 # Checking if instance has been updated since last report
131 @last_report = @issue.reports.order(:updated_at => :desc).last
137 notice = t("issues.update.successful_update") if notice.nil?
140 @issue.report_count = @issue.reports.count
142 redirect_back "/", :notice => notice
145 redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id, :reported_user_id => @issue.reported_user_id), :notice => t("issues.update.provide_details")
150 @issue = Issue.find(params[:id])
151 if issue_comment_params.blank?
152 notice = t("issues.comment.provide_details")
154 @issue_comment = @issue.comments.build(issue_comment_params)
155 @issue_comment.commenter_user_id = @user.id
158 @issue_comment.reassign = true
161 @issue.updated_by = @user.id
163 notice = t("issues.comment.comment_created")
165 redirect_to @issue, :notice => notice
168 # Status Transistions
172 redirect_to @issue, :notice => t("issues.resolved")
180 @issue.updated_by = @user.id
182 redirect_to @issue, :notice => t("issues.ignored")
190 @issue.updated_by = @user.id
192 redirect_to @issue, :notice => t("issues.reopened")
198 # Reassign Issues between Administrators and Moderators
200 @issue.issue_type = upgrade_issue(@issue.issue_type)
206 def upgrade_issue(type)
207 if type == "moderator"
215 @admin_issues = %w(DiaryEntry DiaryComment User)
216 @moderator_issues = %w(Changeset Note)
221 @user_role = @user.administrator? ? "administrator" : "moderator"
225 if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
233 params[:report][:details] + "--||--" + params[:report_type].to_s + "--||--"
236 def check_report_params
237 params[:report] && params[:report][:details] && params[:report_type]
241 @issue = Issue.find(params[:id])
245 unless @user.administrator? || @user.moderator?
246 flash[:error] = t("application.require_admin.not_an_admin")
247 redirect_to root_path
251 def create_new_issue_params
252 params.permit(:reportable_id, :reportable_type, :reported_user_id)
256 params[:issue].permit(:reportable_id, :reportable_type, :reported_user_id)
260 params[:report].permit(:details)
263 def issue_comment_params
264 params.require(:issue_comment).permit(:body)
268 Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
272 %w(asc desc).include?(params[:direction]) ? params[:direction] : "asc"
275 # back-port of ActionController#redirect_back from rails 5
276 def redirect_back(fallback_location, **args)
277 if referer = request.headers["Referer"]
278 redirect_to referer, **args
280 redirect_to fallback_location, **args