2018-08-10 06:45:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-15 22:45:10 +00:00
|
|
|
# Base class for Chat services
|
|
|
|
# This class is not meant to be used directly, but only to inherrit from.
|
2017-05-31 05:50:53 +00:00
|
|
|
class SlashCommandsService < Service
|
2016-12-15 22:45:10 +00:00
|
|
|
default_value_for :category, 'chat'
|
|
|
|
|
|
|
|
prop_accessor :token
|
|
|
|
|
2017-06-08 15:16:27 +00:00
|
|
|
has_many :chat_names, foreign_key: :service_id, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2016-12-15 22:45:10 +00:00
|
|
|
|
|
|
|
def valid_token?(token)
|
|
|
|
self.respond_to?(:token) &&
|
|
|
|
self.token.present? &&
|
2019-06-25 17:54:42 +00:00
|
|
|
ActiveSupport::SecurityUtils.secure_compare(token, self.token)
|
2016-12-15 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2016-12-27 12:44:24 +00:00
|
|
|
def self.supported_events
|
|
|
|
%w()
|
2016-12-15 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_test?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2017-01-08 14:19:19 +00:00
|
|
|
{ type: 'text', name: 'token', placeholder: 'XXxxXXxxXXxxXXxxXXxxXXxx' }
|
2016-12-15 22:45:10 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def trigger(params)
|
2017-01-12 14:04:21 +00:00
|
|
|
return unless valid_token?(params[:token])
|
2016-12-15 22:45:10 +00:00
|
|
|
|
2018-02-22 17:34:04 +00:00
|
|
|
chat_user = find_chat_user(params)
|
2019-10-11 00:06:24 +00:00
|
|
|
user = chat_user&.user
|
|
|
|
|
|
|
|
if user
|
|
|
|
unless user.can?(:use_slash_commands)
|
|
|
|
return Gitlab::SlashCommands::Presenters::Access.new.deactivated if user.deactivated?
|
2017-01-10 18:43:58 +00:00
|
|
|
|
2019-08-26 12:23:36 +00:00
|
|
|
return Gitlab::SlashCommands::Presenters::Access.new.access_denied(project)
|
|
|
|
end
|
2019-07-04 10:31:44 +00:00
|
|
|
|
2018-02-22 17:34:04 +00:00
|
|
|
Gitlab::SlashCommands::Command.new(project, chat_user, params).execute
|
2017-01-10 18:43:58 +00:00
|
|
|
else
|
2016-12-15 22:45:10 +00:00
|
|
|
url = authorize_chat_name_url(params)
|
2017-05-31 05:50:53 +00:00
|
|
|
Gitlab::SlashCommands::Presenters::Access.new(url).authorize
|
2016-12-15 22:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2016-12-15 22:45:10 +00:00
|
|
|
def find_chat_user(params)
|
|
|
|
ChatNames::FindUserService.new(self, params).execute
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2016-12-15 22:45:10 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2016-12-15 22:45:10 +00:00
|
|
|
def authorize_chat_name_url(params)
|
|
|
|
ChatNames::AuthorizeUserService.new(self, params).execute
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2016-12-15 22:45:10 +00:00
|
|
|
end
|