]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
ffce14774b65fe2151db1055849669e386d23441
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
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]
10
11   helper_method :sort_column, :sort_direction
12
13   def index
14     if @user.moderator?
15       @issue_types = @moderator_issues
16       @users = User.joins(:roles).where(:user_roles => { :role => "moderator" })
17     else
18       @issue_types = @admin_issues
19       @users = User.joins(:roles).where(:user_roles => { :role => "administrator" })
20     end
21
22     @issues = Issue.where(:issue_type => @user_role).order(sort_column + " " + sort_direction)
23
24     # If search
25     if params[:search_by_user] && params[:search_by_user].present?
26       @find_user = User.find_by(:display_name => params[:search_by_user])
27       if @find_user
28         @issues = @issues.where(:reported_user_id => @find_user.id)
29       else
30         notice = t("issues.index.search.user_not_found")
31       end
32     end
33
34     if params[:status] && params[:status][0].present?
35       @issues = @issues.where(:status => params[:status][0].to_i)
36     end
37
38     if params[:issue_type] && params[:issue_type][0].present?
39       @issues = @issues.where(:reportable_type => params[:issue_type][0])
40     end
41
42     # If last_updated_by
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)
46     end
47
48     notice = t("issues.index.search.issues_not_found") if @issues.first.nil?
49
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)
53     end
54
55     redirect_to issues_path, :notice => notice if notice
56   end
57
58   def show
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)
63
64     @updated_by_admin = User.find(@issue.updated_by) if @issue.updated_by
65   end
66
67   def new
68     if create_new_issue_params.present?
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     end
73   end
74
75   def create
76     @issue = Issue.find_by(:reportable_id => params[:reportable_id], :reportable_type => params[:reportable_type])
77     # Check if Issue already exists
78     unless @issue
79       @issue = Issue.find_or_initialize_by(issue_params)
80       @issue.updated_by = nil
81
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
85     end
86
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
95       if check_if_updated
96         @issue.save! if @issue.reopen
97       end
98
99       if @issue.save!
100         @issue.report_count = @issue.reports.count
101         @issue.save!
102
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
106         end
107
108         redirect_back "/", :notice => t("issues.create.successful_report")
109       end
110     else
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")
112     end
113   end
114
115   def update
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
120
121       if @report.nil?
122         @report = @issue.reports.build(report_params)
123         @report.reporter_user_id = @user.id
124         notice = t("issues.update.new_report")
125       end
126
127       details = report_details
128       @report.details = details
129
130       # Checking if instance has been updated since last report
131       @last_report = @issue.reports.order(:updated_at => :desc).last
132       if check_if_updated
133         @issue.reopen
134         @issue.save!
135       end
136
137       notice = t("issues.update.successful_update") if notice.nil?
138
139       if @report.save!
140         @issue.report_count = @issue.reports.count
141         @issue.save!
142         redirect_back "/", :notice => notice
143       end
144     else
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")
146     end
147   end
148
149   def comment
150     @issue = Issue.find(params[:id])
151     if issue_comment_params.blank?
152       notice = t("issues.comment.provide_details")
153     else
154       @issue_comment = @issue.comments.build(issue_comment_params)
155       @issue_comment.commenter_user_id = @user.id
156       if params[:reassign]
157         reassign_issue
158         @issue_comment.reassign = true
159       end
160       @issue_comment.save!
161       @issue.updated_by = @user.id
162       @issue.save!
163       notice = t("issues.comment.comment_created")
164     end
165     redirect_to @issue, :notice => notice
166   end
167
168   # Status Transistions
169   def resolve
170     if @issue.resolve
171       @issue.save!
172       redirect_to @issue, :notice => t("issues.resolved")
173     else
174       render :show
175     end
176   end
177
178   def ignore
179     if @issue.ignore
180       @issue.updated_by = @user.id
181       @issue.save!
182       redirect_to @issue, :notice => t("issues.ignored")
183     else
184       render :show
185     end
186   end
187
188   def reopen
189     if @issue.reopen
190       @issue.updated_by = @user.id
191       @issue.save!
192       redirect_to @issue, :notice => t("issues.reopened")
193     else
194       render :show
195     end
196   end
197
198   # Reassign Issues between Administrators and Moderators
199   def reassign_issue
200     @issue.issue_type = upgrade_issue(@issue.issue_type)
201     @issue.save!
202   end
203
204   private
205
206   def upgrade_issue(type)
207     if type == "moderator"
208       "administrator"
209     else
210       "moderator"
211     end
212   end
213
214   def set_issues
215     @admin_issues = %w[DiaryEntry DiaryComment User]
216     @moderator_issues = %w[Changeset Note]
217   end
218
219   def setup_user_role
220     # Get user role
221     @user_role = @user.administrator? ? "administrator" : "moderator"
222   end
223
224   def check_if_updated
225     if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
226       true
227     else
228       false
229     end
230   end
231
232   def report_details
233     params[:report][:details] + "--||--" + params[:report_type].to_s + "--||--"
234   end
235
236   def check_report_params
237     params[:report] && params[:report][:details] && params[:report_type]
238   end
239
240   def find_issue
241     @issue = Issue.find(params[:id])
242   end
243
244   def check_permission
245     unless @user.administrator? || @user.moderator?
246       flash[:error] = t("application.require_admin.not_an_admin")
247       redirect_to root_path
248     end
249   end
250
251   def create_new_issue_params
252     params.permit(:reportable_id, :reportable_type, :reported_user_id)
253   end
254
255   def issue_params
256     params[:issue].permit(:reportable_id, :reportable_type, :reported_user_id)
257   end
258
259   def report_params
260     params[:report].permit(:details)
261   end
262
263   def issue_comment_params
264     params.require(:issue_comment).permit(:body)
265   end
266
267   def sort_column
268     Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
269   end
270
271   def sort_direction
272     %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
273   end
274
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
279     else
280       redirect_to fallback_location, **args
281     end
282   end
283 end