diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php index 62380b221..d0f7fea60 100644 --- a/app/Http/Controllers/Api/SettingsController.php +++ b/app/Http/Controllers/Api/SettingsController.php @@ -143,47 +143,6 @@ class SettingsController extends Controller } - public function slacktest(SlackSettingsRequest $request) - { - - $validator = Validator::make($request->all(), [ - 'slack_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable', - 'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable', - ]); - - if ($validator->fails()) { - return response()->json(['message' => 'Validation failed', 'errors' => $validator->errors()], 422); - } - - // If validation passes, continue to the curl request - $slack = new Client([ - 'base_url' => e($request->input('slack_endpoint')), - 'defaults' => [ - 'exceptions' => false, - ], - ]); - - $payload = json_encode( - [ - 'channel' => e($request->input('slack_channel')), - 'text' => trans('general.slack_test_msg'), - 'username' => e($request->input('slack_botname')), - 'icon_emoji' => ':heart:', - ]); - - try { - $slack->post($request->input('slack_endpoint'), ['body' => $payload]); - return response()->json(['message' => 'Success'], 200); - - } catch (\Exception $e) { - return response()->json(['message' => 'Please check the channel name and webhook endpoint URL ('.e($request->input('slack_endpoint')).'). Slack responded with: '.$e->getMessage()], 400); - } - - //} - return response()->json(['message' => 'Something went wrong :( '], 400); - } - - /** * Test the email configuration * diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index d8f7ee3b6..b04c692ac 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -679,33 +679,6 @@ class SettingsController extends Controller return view('settings.slack', compact('setting')); } - /** - * Return a form to allow a super admin to update settings. - * - * @author [A. Gianotto] [] - * - * @since [v1.0] - * - * @return View - */ - public function postSlack(SlackSettingsRequest $request) - { - if (is_null($setting = Setting::getSettings())) { - return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); - } - - $setting->slack_endpoint = $request->input('slack_endpoint'); - $setting->slack_channel = $request->input('slack_channel'); - $setting->slack_botname = $request->input('slack_botname'); - - if ($setting->save()) { - return redirect()->route('settings.index') - ->with('success', trans('admin/settings/message.update.success')); - } - - return redirect()->back()->withInput()->withErrors($setting->getErrors()); - } - /** * Return a form to allow a super admin to update settings. * diff --git a/app/Http/Livewire/SlackSettingsForm.php b/app/Http/Livewire/SlackSettingsForm.php new file mode 100644 index 000000000..8e3e148f3 --- /dev/null +++ b/app/Http/Livewire/SlackSettingsForm.php @@ -0,0 +1,86 @@ + 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable', + 'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable', + 'slack_botname' => 'string|nullable', + ]; + + public function mount(){ + + $this->setting = Setting::getSettings(); + $this->slack_endpoint = $this->setting->slack_endpoint; + $this->slack_channel = $this->setting->slack_channel; + $this->slack_botname = $this->setting->slack_botname; + + } + public function updated($field){ + + $this->validateOnly($field ,$this->rules); + } + + public function render() + { + return view('livewire.slack-settings-form'); + } + + public function testSlack(){ + + $slack = new Client([ + 'base_url' => e($this->slack_endpoint), + 'defaults' => [ + 'exceptions' => false, + ], + ]); + + $payload = json_encode( + [ + 'channel' => e($this->slack_channel), + 'text' => trans('general.slack_test_msg'), + 'username' => e($this->slack_botname), + 'icon_emoji' => ':heart:', + ]); + + try { + $slack->post($this->slack_endpoint, ['body' => $payload]); + return session()->flash('success' , 'Your Slack Integration works!'); + + } catch (\Exception $e) { + return session()->flash('error' , 'Please check the channel name and webhook endpoint URL ('.e($this->slack_endpoint).'). Slack responded with: '.$e->getMessage()); + } + + //} + return session()->flash('message' , 'Something went wrong :( '); + + + + } + public function submit() + { + $this->validate($this->rules); + + $this->setting->slack_endpoint = $this->slack_endpoint; + $this->setting->slack_channel = $this->slack_channel; + $this->setting->slack_botname = $this->slack_botname; + + $this->setting->save(); + + session()->flash('save',trans('admin/settings/message.update.success')); + + + } +} diff --git a/app/Http/Requests/SlackSettingsRequest.php b/app/Http/Requests/SlackSettingsRequest.php deleted file mode 100644 index 1f4421519..000000000 --- a/app/Http/Requests/SlackSettingsRequest.php +++ /dev/null @@ -1,33 +0,0 @@ - 'url|required_with:slack_channel|starts_with:"https://hooks.slack.com"|nullable', - 'slack_channel' => 'required_with:slack_endpoint|starts_with:#|nullable', - 'slack_botname' => 'string|nullable', - - ]; - } - - -} diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 8f1f65201..fd02992f7 100755 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -83,6 +83,9 @@ class Setting extends Model 'email_domain', 'email_format', 'username_format', + 'slack_endpoint', + 'slack_channel', + 'slack_botname', ]; /** diff --git a/resources/views/livewire/slack-settings-form.blade.php b/resources/views/livewire/slack-settings-form.blade.php new file mode 100644 index 000000000..c165118cb --- /dev/null +++ b/resources/views/livewire/slack-settings-form.blade.php @@ -0,0 +1,101 @@ + + +
+ @if (session()->has('save')) +
+ {{session('save')}} +
+ @endif + @if(session()->has('success')) +
+ {{session('success')}} +
+ @endif + @if(session()->has('error')) +
+ {{session('error')}} +
+ @endif + @if(session()->has('message')) +
+ {{session('message')}} +
+ @endif +
+ {{csrf_field()}} + + +
+
+ {{ Form::label('slack_endpoint', trans('admin/settings/general.slack_endpoint')) }} +
+
+ @if (config('app.lock_passwords')===true) +

{{ trans('general.feature_disabled') }}

+
+ @else +
+ @endif + {!! $errors->first('slack_endpoint', '') !!} +
+
+ + +
+
+ {{ Form::label('slack_channel', trans('admin/settings/general.slack_channel')) }} +
+
+ @if (config('app.lock_passwords')===true) +
+

{{ trans('general.feature_disabled') }}

+ + @else +
+ @endif + {!! $errors->first('slack_channel', '') !!} +
+
+ + +
+
+ {{ Form::label('slack_botname', trans('admin/settings/general.slack_botname')) }} +
+
+ @if (config('app.lock_passwords')===true) +
+

{{ trans('general.feature_disabled') }}

+ + @else +
+ @endif + {!! $errors->first('slack_botname', '') !!} +
+
+ + + @if($slack_endpoint != null && $slack_channel != null && $slack_botname != null) +
+
+ {{ Form::label('test_slack', 'Test Slack') }} +
+ +
+ @endif + + +
+ +
+ + diff --git a/resources/views/settings/slack.blade.php b/resources/views/settings/slack.blade.php index 705510ab8..fffe4383c 100644 --- a/resources/views/settings/slack.blade.php +++ b/resources/views/settings/slack.blade.php @@ -20,15 +20,9 @@ } - - {{ Form::open(['method' => 'POST', 'files' => false, 'autocomplete' => 'off', 'class' => 'form-horizontal', 'role' => 'form' ]) }} - - {{csrf_field()}} -
-

@@ -37,227 +31,20 @@

-

{!! trans('admin/settings/general.slack_integration_help',array('slack_link' => 'https://my.slack.com/services/new/incoming-webhook')) !!} - - @if (($setting->slack_channel=='') && ($setting->slack_endpoint=='')) - {{ trans('admin/settings/general.slack_integration_help_button') }} - @endif

+ @livewire('slack-settings-form') -
- - - -
-
- {{ Form::label('slack_endpoint', trans('admin/settings/general.slack_endpoint')) }} -
-
- @if (config('app.lock_passwords')===true) - {{ Form::text('slack_endpoint', old('slack_endpoint', $setting->slack_endpoint), array('class' => 'form-control','disabled'=>'disabled','placeholder' => 'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX', 'id' => 'slack_endpoint')) }} -

{{ trans('general.feature_disabled') }}

- - @else - {{ Form::text('slack_endpoint', old('slack_endpoint', $setting->slack_endpoint), array('class' => 'form-control','placeholder' => 'https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX', 'id' => 'slack_endpoint')) }} - @endif - {!! $errors->first('slack_endpoint', '') !!} -
-
- - -
-
- {{ Form::label('slack_channel', trans('admin/settings/general.slack_channel')) }} -
-
- @if (config('app.lock_passwords')===true) - {{ Form::text('slack_channel', old('slack_channel', $setting->slack_channel), array('class' => 'form-control','disabled'=>'disabled','placeholder' => '#IT-Ops')) }} -

{{ trans('general.feature_disabled') }}

- - @else - {{ Form::text('slack_channel', old('slack_channel', $setting->slack_channel), array('class' => 'form-control','placeholder' => '#IT-Ops')) }} - @endif - {!! $errors->first('slack_channel', '') !!} -
-
- - -
-
- {{ Form::label('slack_botname', trans('admin/settings/general.slack_botname')) }} -
-
- @if (config('app.lock_passwords')===true) - {{ Form::text('slack_botname', old('slack_botname', $setting->slack_botname), array('class' => 'form-control','disabled'=>'disabled','placeholder' => 'Snipe-Bot')) }} -

{{ trans('general.feature_disabled') }}

- - @else - {{ Form::text('slack_botname', old('slack_botname', $setting->slack_botname), array('class' => 'form-control','placeholder' => 'Snipe-Bot')) }} - @endif - {!! $errors->first('slack_botname', '') !!} -
-
- - -
-
- -
- {{Form::close()}} @stop -@push('js') - - -@endpush