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

slash_commands_controller.rb « integrations « projects « controllers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 891a7c1a7497bd53760e04ee27cb7fa12e5541b8 (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
# frozen_string_literal: true

module Projects
  module Integrations
    class SlashCommandsController < Projects::ApplicationController
      before_action :authenticate_user!

      feature_category :integrations

      def show
        @redirect_url = integration_redirect_url

        unless valid_request?
          @error = s_("Integrations|The slash command verification request has expired. Please run the command again.")
          return
        end

        return if valid_user? || @redirect_url.blank?

        @error = s_("Integrations|The slash command request is invalid.")
      end

      def confirm
        if valid_request? && valid_user?
          Gitlab::SlashCommands::VerifyRequest.new(integration, chat_user, request_params[:response_url]).approve!
          redirect_to request_params[:redirect_url]
        else
          @error = s_("Integrations|The slash command request is invalid.")
          render :show
        end
      end

      private

      def request_params
        params.permit(:integration, :team, :channel, :response_url, :command_id, :redirect_url)
      end

      def cached_params
        @cached_params ||= Rails.cache.fetch(cache_key)
      end

      def cache_key
        @cache_key ||= Kernel.format(::Integrations::BaseSlashCommands::CACHE_KEY, secret: request_params[:command_id])
      end

      def integration
        integration = request_params[:integration]

        case integration
        when 'slack_slash_commands'
          project.slack_slash_commands_integration
        when 'mattermost_slash_commands'
          project.mattermost_slash_commands_integration
        end
      end

      def integration_redirect_url
        return unless integration

        team, channel, url = request_params.values_at(:team, :channel, :response_url)

        integration.redirect_url(team, channel, url)
      end

      def valid_request?
        cached_params.present?
      end

      def valid_user?
        return false unless chat_user

        current_user == chat_user.user
      end

      def chat_user
        @chat_user ||= ChatNames::FindUserService.new(cached_params[:team_id], cached_params[:user_id]).execute
      end
    end
  end
end