Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abuse_report_update_service.rb « admin « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12cf8bf14a8ca872fdb4e46e05cb3f41e5d2e770 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Admin
  class AbuseReportUpdateService < BaseService
    attr_reader :abuse_report, :params, :current_user, :action

    def initialize(abuse_report, current_user, params)
      @abuse_report = abuse_report
      @current_user = current_user
      @params = params
      @action = determine_action
    end

    def execute
      return ServiceResponse.error(message: 'Admin is required') unless current_user&.can_admin_all_resources?
      return ServiceResponse.error(message: 'Action is required') unless action.present?

      result = perform_action
      if result[:status] == :success
        event = close_report_and_record_event
        ServiceResponse.success(message: event.success_message)
      else
        ServiceResponse.error(message: result[:message])
      end
    end

    private

    def determine_action
      action = params[:user_action]
      if action.in?(ResourceEvents::AbuseReportEvent.actions.keys)
        action.to_sym
      elsif close_report?
        :close_report
      end
    end

    def perform_action
      case action
      when :ban_user then ban_user
      when :block_user then block_user
      when :delete_user then delete_user
      when :close_report then close_report
      end
    end

    def ban_user
      Users::BanService.new(current_user).execute(abuse_report.user)
    end

    def block_user
      Users::BlockService.new(current_user).execute(abuse_report.user)
    end

    def delete_user
      abuse_report.user.delete_async(deleted_by: current_user)
      success
    end

    def close_report
      return error('Report already closed') if abuse_report.closed?

      abuse_report.closed!
      success
    end

    def close_report_and_record_event
      event = action

      if close_report? && action != :close_report
        close_report
        event = "#{action}_and_close_report"
      end

      record_event(event)
    end

    def close_report?
      params[:close].to_s == 'true'
    end

    def record_event(action)
      reason = params[:reason]
      unless reason.in?(ResourceEvents::AbuseReportEvent.reasons.keys)
        reason = ResourceEvents::AbuseReportEvent.reasons[:other]
      end

      abuse_report.events.create(action: action, user: current_user, reason: reason, comment: params[:comment])
    end
  end
end