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

moderate_user_service.rb « abuse_reports « admin « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e14806c694998a19b7abd7212cc4b826b1f96e2 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true

module Admin
  module AbuseReports
    class ModerateUserService < 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
        when :trust_user then trust_user
        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?

        close_similar_open_reports
        abuse_report.closed!
        success
      end

      def trust_user
        Users::TrustService.new(current_user).execute(abuse_report.user)
      end

      def close_similar_open_reports
        # admins see the abuse report and other open reports for the same user in one page
        # hence, if the request is to close the report, close other open reports for the same user too
        abuse_report.similar_open_reports_for_user.update_all(status: 'closed')
      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
end