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)
72 flash[:referer] = params[:referer]
77 @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id], params[:reportable_type])
78 # Check if Issue already exists
80 @issue = Issue.find_or_initialize_by(issue_params)
81 @issue.updated_by = nil
83 # Reassign to moderators if it is a moderator issue
84 @issue.issue_type = "administrator"
85 reassign_issue if @moderator_issues.include? @issue.reportable.class.name
88 # Check if details provided are sufficient
89 if check_report_params
90 @report = @issue.reports.build(report_params)
91 details = report_details
92 @report.reporter_user_id = @user.id
93 @report.details = details
94 # Checking if instance has been updated since last report
95 @last_report = @issue.reports.order(:updated_at => :desc).last
97 @issue.save! if @issue.reopen
101 @issue.report_count = @issue.reports.count
104 @admins_or_mods = UserRole.where(:role => @issue.issue_type)
105 @admins_or_mods.each do |user|
106 Notifier.new_issue_notification(@issue.id, User.find(user.user_id)).deliver_now
109 redirect_to flash[:referer], :notice => t("issues.create.successful_report")
112 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")
117 @issue = Issue.find_by(issue_params)
118 # Check if details provided are sufficient
119 if check_report_params
120 @report = @issue.reports.where(:reporter_user_id => @user.id).first
123 @report = @issue.reports.build(report_params)
124 @report.reporter_user_id = @user.id
125 notice = t("issues.update.new_report")
128 details = report_details
129 @report.details = details
131 # Checking if instance has been updated since last report
132 @last_report = @issue.reports.order(:updated_at => :desc).last
138 notice = t("issues.update.successful_update") if notice.nil?
141 @issue.report_count = @issue.reports.count
143 redirect_to flash[:referer], :notice => notice
146 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")
151 @issue = Issue.find(params[:id])
152 if issue_comment_params.blank?
153 notice = t("issues.comment.provide_details")
155 @issue_comment = @issue.comments.build(issue_comment_params)
156 @issue_comment.commenter_user_id = @user.id
159 @issue_comment.reassign = true
162 @issue.updated_by = @user.id
164 notice = t("issues.comment.comment_created")
166 redirect_to @issue, :notice => notice
169 # Status Transistions
173 redirect_to @issue, :notice => t("issues.resolved")
181 @issue.updated_by = @user.id
183 redirect_to @issue, :notice => t("issues.ignored")
191 @issue.updated_by = @user.id
193 redirect_to @issue, :notice => t("issues.reopened")
199 # Reassign Issues between Administrators and Moderators
201 @issue.issue_type = upgrade_issue(@issue.issue_type)
207 def upgrade_issue(type)
208 if type == "moderator"
216 @admin_issues = %w(DiaryEntry DiaryComment User)
217 @moderator_issues = %w(Changeset Note)
222 @user_role = @user.administrator? ? "administrator" : "moderator"
226 if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
234 params[:report][:details] + "--||--" + params[:report_type].to_s + "--||--"
237 def check_report_params
238 params[:report] && params[:report][:details] && params[:report_type]
242 @issue = Issue.find(params[:id])
246 unless @user.administrator? || @user.moderator?
247 flash[:error] = t("application.require_admin.not_an_admin")
248 redirect_to root_path
252 def create_new_issue_params
253 params.permit(:reportable_id, :reportable_type, :reported_user_id)
257 params[:issue].permit(:reportable_id, :reportable_type, :reported_user_id)
261 params[:report].permit(:details)
264 def issue_comment_params
265 params.require(:issue_comment).permit(:body)
269 Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
273 %w(asc desc).include?(params[:direction]) ? params[:direction] : "asc"