debian-mirror-gitlab/app/models/project_services/slash_commands_service.rb

66 lines
1.6 KiB
Ruby
Raw Normal View History

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