summaryrefslogtreecommitdiff
path: root/app/controllers/abuse_reports_controller.rb
blob: 55aef945702d7af19602c09787b42c1b7cedb628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

class AbuseReportsController < ApplicationController
  before_action :set_user, only: [:new, :add_category]

  feature_category :insider_threat

  def new
    @abuse_report = AbuseReport.new(
      user_id: @user.id,
      reported_from_url: params.fetch(:ref_url, '')
    )
  end

  def add_category
    @abuse_report = AbuseReport.new(
      user_id: @user.id,
      category: report_params[:category],
      reported_from_url: report_params[:reported_from_url]
    )

    Gitlab::Tracking.event(
      'ReportAbuse',
      'select_abuse_category',
      property: report_params[:category],
      user: @user
    )

    render :new
  end

  def create
    @abuse_report = AbuseReport.new(report_params)
    @abuse_report.reporter = current_user

    if @abuse_report.save
      @abuse_report.notify

      Gitlab::Tracking.event(
        'ReportAbuse',
        'submit_form',
        property: @abuse_report.category,
        user: @abuse_report.user
      )

      message = _("Thank you for your report. A GitLab administrator will look into it shortly.")
      redirect_to root_path, notice: message
    elsif report_params[:user_id].present?
      render :new
    else
      redirect_to root_path, alert: _("Cannot create the abuse report. The reported user was invalid. Please try again or contact support.")
    end
  end

  private

  def report_params
    params.require(:abuse_report).permit(:message, :user_id, :category, :reported_from_url, :screenshot, links_to_spam: [])
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def set_user
    @user = User.find_by(id: params[:user_id])

    if @user.nil?
      redirect_to root_path, alert: _("Cannot create the abuse report. The user has been deleted.")
    elsif @user.banned?
      redirect_to @user, alert: _("Cannot create the abuse report. This user has been banned.")
    end
  end
  # rubocop: enable CodeReuse/ActiveRecord
end