diff --git a/app/Http/Livewire/SlackSettingsForm.php b/app/Http/Livewire/SlackSettingsForm.php index bb3a7bd7f6..7fc53c7818 100644 --- a/app/Http/Livewire/SlackSettingsForm.php +++ b/app/Http/Livewire/SlackSettingsForm.php @@ -12,7 +12,7 @@ class SlackSettingsForm extends Component public $webhook_endpoint; public $webhook_channel; public $webhook_botname; - public $isDisabled ='' ; + public $isDisabled ='disabled' ; public $webhook_name; public $webhook_link; public $webhook_placeholder; @@ -22,11 +22,17 @@ class SlackSettingsForm extends Component public Setting $setting; + public $webhook_endpoint_rules; + + protected $rules = [ - 'webhook_endpoint' => 'url|required_with:webhook_channel|starts_with:https://hooks.slack.com/services|nullable', + 'webhook_endpoint' => 'required_with:webhook_channel|starts_with:http://,https://,ftp://,irc://,https://hooks.slack.com/services/|url|nullable', 'webhook_channel' => 'required_with:webhook_endpoint|starts_with:#|nullable', 'webhook_botname' => 'string|nullable', ]; + public $messages = [ + 'webhook_endpoint.starts_with' => 'your webhook endpoint should begin with http://, https:// or other protocol.', + ]; public function mount() { $this->webhook_text= [ @@ -55,9 +61,7 @@ class SlackSettingsForm extends Component $this->webhook_botname = $this->setting->webhook_botname; $this->webhook_options = $this->setting->webhook_selected; - if($this->setting->webhook_selected == 'general'){ - $this->isDisabled=''; - } + if($this->setting->webhook_endpoint != null && $this->setting->webhook_channel != null){ $this->isDisabled= ''; } @@ -65,9 +69,8 @@ class SlackSettingsForm extends Component } public function updated($field) { - if($this->webhook_selected != 'general') { $this->validateOnly($field, $this->rules); - } + } public function updatedWebhookSelected() { @@ -82,7 +85,6 @@ class SlackSettingsForm extends Component } private function isButtonDisabled() { - if($this->webhook_selected == 'slack') { if (empty($this->webhook_endpoint)) { $this->isDisabled = 'disabled'; $this->save_button = trans('admin/settings/general.webhook_presave'); @@ -91,8 +93,6 @@ class SlackSettingsForm extends Component $this->isDisabled = 'disabled'; $this->save_button = trans('admin/settings/general.webhook_presave'); } - } - } public function render() @@ -108,6 +108,7 @@ class SlackSettingsForm extends Component 'defaults' => [ 'exceptions' => false, ], + 'allow_redirects' => false, ]); $payload = json_encode( @@ -116,18 +117,23 @@ class SlackSettingsForm extends Component 'text' => trans('general.webhook_test_msg', ['app' => $this->webhook_name]), 'username' => e($this->webhook_botname), 'icon_emoji' => ':heart:', + ]); try { + $test = $webhook->post($this->webhook_endpoint, ['body' => $payload]); - $webhook->post($this->webhook_endpoint, ['body' => $payload]); + if(($test->getStatusCode() == 302)||($test->getStatusCode() == 301)){ + return session()->flash('error' , trans('admin/settings/message.webhook.error_redirect', ['endpoint' => $this->webhook_endpoint])); + } $this->isDisabled=''; $this->save_button = trans('general.save'); - return session()->flash('success' , 'Your '.$this->webhook_name.' Integration works!'); + return session()->flash('success' , trans('admin/settings/message.webhook.success', ['webhook_name' => $this->webhook_name])); } catch (\Exception $e) { - $this->isDisabled= 'disabled'; + $this->isDisabled='disabled'; + $this->save_button = trans('admin/settings/general.webhook_presave'); return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name])); } @@ -158,9 +164,7 @@ class SlackSettingsForm extends Component if (Helper::isDemoMode()) { session()->flash('error',trans('general.feature_disabled')); } else { - if ($this->webhook_selected != 'general') { - $this->validate($this->rules); - } + $this->validate($this->rules); $this->setting->webhook_selected = $this->webhook_selected; $this->setting->webhook_endpoint = $this->webhook_endpoint; diff --git a/resources/lang/en/admin/settings/message.php b/resources/lang/en/admin/settings/message.php index 48b42d1202..c9b0f34217 100644 --- a/resources/lang/en/admin/settings/message.php +++ b/resources/lang/en/admin/settings/message.php @@ -35,10 +35,12 @@ return [ ], 'webhook' => [ 'sending' => 'Sending :app test message...', + 'success' => 'Your :webhook_name Integration works!', 'success_pt1' => 'Success! Check the ', 'success_pt2' => ' channel for your test message, and be sure to click SAVE below to store your settings.', '500' => '500 Server Error.', 'error' => 'Something went wrong. :app responded with: :error_message', + 'error_redirect' => 'ERROR: 301/302 :endpoint returns a redirect. For security reasons, we don’t follow redirects. Please use the actual endpoint.', 'error_misc' => 'Something went wrong. :( ', ] ]; diff --git a/resources/views/livewire/slack-settings-form.blade.php b/resources/views/livewire/slack-settings-form.blade.php index a3c6ddc0fa..0c67ca067e 100644 --- a/resources/views/livewire/slack-settings-form.blade.php +++ b/resources/views/livewire/slack-settings-form.blade.php @@ -79,7 +79,7 @@ {{ Form::label('webhook_endpoint', trans('admin/settings/general.webhook_endpoint',['app' => $webhook_name ])) }}
- + {!! $errors->first('webhook_endpoint', '') !!}
@@ -95,7 +95,7 @@ {{ Form::label('webhook_channel', trans('admin/settings/general.webhook_channel',['app' => $webhook_name ])) }}
- + {!! $errors->first('webhook_channel', '') !!}
@@ -111,7 +111,7 @@ {{ Form::label('webhook_botname', trans('admin/settings/general.webhook_botname',['app' => $webhook_name ])) }}
- + {!! $errors->first('webhook_botname', '') !!}
@@ -121,7 +121,6 @@ @endif - @if($webhook_selected == 'slack') @if($webhook_endpoint != null && $webhook_channel != null)
@@ -138,7 +137,6 @@
@endif - @endif