debian-mirror-gitlab/app/models/integrations/base_slash_commands.rb

74 lines
2 KiB
Ruby
Raw Normal View History

2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
# Base class for ChatOps integrations
# This class is not meant to be used directly, but only to inherrit from.
module Integrations
class BaseSlashCommands < Integration
2023-01-13 00:05:48 +05:30
attribute :category, default: 'chat'
2021-09-04 01:27:46 +05:30
prop_accessor :token
2022-08-27 11:52:29 +05:30
has_many :chat_names, foreign_key: :integration_id, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
2021-09-04 01:27:46 +05:30
def valid_token?(token)
self.respond_to?(:token) &&
self.token.present? &&
ActiveSupport::SecurityUtils.secure_compare(token, self.token)
end
def self.supported_events
%w()
end
2021-09-30 23:02:18 +05:30
def testable?
2021-09-04 01:27:46 +05:30
false
end
def fields
[
2022-05-03 16:02:30 +05:30
{
type: 'password',
name: 'token',
non_empty_password_title: s_('ProjectService|Enter new token'),
non_empty_password_help: s_('ProjectService|Leave blank to use your current token.'),
placeholder: 'XXxxXXxxXXxxXXxxXXxxXXxx'
}
2021-09-04 01:27:46 +05:30
]
end
def trigger(params)
return unless valid_token?(params[:token])
chat_user = find_chat_user(params)
user = chat_user&.user
if user
unless user.can?(:use_slash_commands)
return Gitlab::SlashCommands::Presenters::Access.new.deactivated if user.deactivated?
return Gitlab::SlashCommands::Presenters::Access.new.access_denied(project)
end
Gitlab::SlashCommands::Command.new(project, chat_user, params).execute
else
url = authorize_chat_name_url(params)
Gitlab::SlashCommands::Presenters::Access.new(url).authorize
end
end
private
# rubocop: disable CodeReuse/ServiceClass
def find_chat_user(params)
2023-03-04 22:38:38 +05:30
ChatNames::FindUserService.new(params[:team_id], params[:user_id]).execute
2021-09-04 01:27:46 +05:30
end
# rubocop: enable CodeReuse/ServiceClass
# rubocop: disable CodeReuse/ServiceClass
def authorize_chat_name_url(params)
ChatNames::AuthorizeUserService.new(self, params).execute
end
# rubocop: enable CodeReuse/ServiceClass
end
end