]> git.openstreetmap.org Git - rails.git/blob - app/controllers/reports_controller.rb
Merge remote-tracking branch 'upstream/pull/5696'
[rails.git] / app / controllers / reports_controller.rb
1 class ReportsController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :check_database_readable
7
8   authorize_resource
9
10   before_action :check_database_writable, :only => [:new, :create]
11
12   def new
13     if required_new_report_params_present?
14       @report = Report.new
15       @report.issue = Issue.find_or_initialize_by(create_new_report_params)
16     else
17       redirect_to root_path, :notice => t(".missing_params")
18     end
19   end
20
21   def create
22     @report = current_user.reports.new(report_params)
23     @report.issue = Issue
24                     .create_with(:assigned_role => default_assigned_role)
25                     .find_or_initialize_by(issue_params)
26
27     if @report.save
28       @report.issue.assigned_role = "administrator" if default_assigned_role == "administrator"
29       @report.issue.reopen unless @report.issue.open?
30       @report.issue.save!
31
32       @report.issue.reported_user&.spam_check
33
34       redirect_to helpers.reportable_url(@report.issue.reportable), :notice => t(".successful_report")
35     else
36       flash.now[:notice] = t(".provide_details")
37       render :action => "new"
38     end
39   end
40
41   private
42
43   def required_new_report_params_present?
44     create_new_report_params["reportable_id"].present? && create_new_report_params["reportable_type"].present?
45   end
46
47   def create_new_report_params
48     params.permit(:reportable_id, :reportable_type)
49   end
50
51   def report_params
52     params.require(:report).permit(:details, :category)
53   end
54
55   def issue_params
56     params.require(:report).require(:issue).permit(:reportable_id, :reportable_type)
57   end
58
59   def default_assigned_role
60     case issue_params[:reportable_type]
61     when "Note"
62       "moderator"
63     when "User"
64       case report_params[:category]
65       when "vandal" then "moderator"
66       else "administrator"
67       end
68     else
69       "administrator"
70     end
71   end
72 end