Added Slack test button

This commit is contained in:
snipe 2018-02-22 16:35:34 -08:00
parent cbd8409611
commit e3144c3093
5 changed files with 210 additions and 6 deletions

View file

@ -8,6 +8,8 @@ use App\Models\Ldap;
use Validator;
use App\Models\Setting;
use Mail;
use App\Notifications\SlackTest;
use Notification;
class SettingsController extends Controller
{
@ -96,6 +98,29 @@ class SettingsController extends Controller
}
public function slacktest()
{
if ($settings = Setting::getSettings()->slack_channel=='') {
\Log::debug('Slack is not enabled. Cannot test.');
return response()->json(['message' => 'Slack is not enabled, cannot test.'], 400);
}
\Log::debug('Preparing to test slack connection');
try {
Notification::send($settings = Setting::getSettings(), new SlackTest());
return response()->json(['message' => 'Success'], 200);
} catch (\Exception $e) {
\Log::debug('Slack connection failed');
return response()->json(['message' => $e->getMessage()], 400);
}
}
/**
* Test the email configuration
*

View file

@ -0,0 +1,77 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\SlackMessage;
use App\Models\Setting;
class SlackTest extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return [
//
];
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
$settings = Setting::getSettings();
return (new SlackMessage)
->from($settings->slack_botname, ':heart:')
->to($settings->slack_channel)
->image('https://snipeitapp.com/favicon.ico')
->content('Oh hai! Looks like your Slack integration with Snipe-IT is working!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}

View file

@ -105,7 +105,8 @@ return array(
'slack_channel' => 'Slack Channel',
'slack_endpoint' => 'Slack Endpoint',
'slack_integration' => 'Slack Settings',
'slack_integration_help' => 'Slack integration is optional, however the endpoint and channel are required if you wish to use it. To configure Slack integration, you must first <a href=":slack_link" target="_new">create an incoming webhook</a> on your Slack account.',
'slack_integration_help' => 'Slack integration is optional, however the endpoint and channel are required if you wish to use it. To configure Slack integration, you must first <a href=":slack_link" target="_new">create an incoming webhook</a> on your Slack account. Once you have saved your Slack information, a test button will appear.',
'slack_test_help' => 'Test whether your Slack integration is configured correctly. YOU MUST SAVE YOUR UPDATED SLACK SETTINGS FIRST.',
'snipe_version' => 'Snipe-IT version',
'support_footer' => 'Support Footer Links ',
'support_footer_help' => 'Specify who sees the links to the Snipe-IT Support info and Users Manual',

View file

@ -38,9 +38,14 @@
<div class="box-body">
<div class="col-md-11 col-md-offset-1">
<p style="border-bottom: 0px;">
{!! trans('admin/settings/general.slack_integration_help',array('slack_link' => 'https://my.slack.com/services/new/incoming-webhook')) !!}
</p>
<div class="col-md-11 col-md-offset-1" style="border-top: 0px;">
<p class="help-block">{!! trans('admin/settings/general.slack_integration_help',array('slack_link' => 'https://my.slack.com/services/new/incoming-webhook')) !!}</p>
<!-- slack endpoint -->
<div class="form-group {{ $errors->has('slack_endpoint') ? 'error' : '' }}">
@ -87,10 +92,26 @@
</div>
</div>
@if (($setting->slack_channel!='') && ($setting->slack_endpoint))
<div class="form-group">
<div class="col-md-3">
{{ Form::label('est_slack', 'Test Slack') }}
</div>
<div class="col-md-9" id="slacktestrow">
<a class="btn btn-default btn-sm pull-left" id="slacktest" style="margin-right: 10px;">Test Integration</a>
</div>
<div class="col-md-9 col-md-offset-3">
<span id="slacktesticon"></span>
<span id="slacktestresult"></span>
<span id="slackteststatus"></span>
</div>
<div class="col-md-9 col-md-offset-3">
<p class="help-block">{{ trans('admin/settings/general.slack_test_help') }}</p>
</div>
</div>
</div>
@endif
</div> <!--/-->
</div> <!--/.box-body-->
<div class="box-footer">
<div class="text-left col-md-6">
@ -108,3 +129,78 @@
{{Form::close()}}
@stop
@section('moar_scripts')
<script nonce="{{ csrf_token() }}">
$("#slacktest").click(function() {
$("#slacktestrow").removeClass('text-success');
$("#slacktestrow").removeClass('text-danger');
$("#slackteststatus").removeClass('text-danger');
$("#slackteststatus").html('');
$("#slacktesticon").html('<i class="fa fa-spinner spin"></i> Sending Slack test message...');
$.ajax({
url: '{{ route('api.settings.slacktest') }}',
type: 'POST',
headers: {
"X-Requested-With": 'XMLHttpRequest',
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
data: {
'ldaptest_user': $('#ldaptest_user').val(),
'ldaptest_password': $('#ldaptest_password').val()
},
dataType: 'json',
success: function (data) {
$("#slacktesticon").html('');
$("#slacktestrow").addClass('text-success');
$("#slackteststatus").addClass('text-success');
$("#slackteststatus").html('<i class="fa fa-check text-success"></i> Success! Check the {{ $setting->slack_channel}} channel for your test message');
},
error: function (data) {
if (data.responseJSON) {
var errors = data.responseJSON.message;
} else {
var errors;
}
var error_text = '';
$("#slacktesticon").html('');
$("#slackteststatus").addClass('text-danger');
$("#slacktesticon").html('<i class="fa fa-exclamation-triangle text-danger"></i>');
if (data.status == 500) {
$('#slackteststatus').html('500 Server Error');
} else if (data.status == 400) {
if (typeof errors != 'string') {
for (i = 0; i < errors.length; i++) {
if (errors[i]) {
error_text += '<li>Error: ' + errors[i];
}
}
} else {
error_text = errors;
}
$('#slackteststatus').html(error_text);
} else {
$('#slackteststatus').html(data.responseText.message);
}
}
});
});
</script>
@stop

View file

@ -527,6 +527,11 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () {
'uses' => 'SettingsController@ldaptestlogin'
]);
Route::post('settings/slacktest', [
'as' => 'api.settings.slacktest',
'uses' => 'SettingsController@slacktest'
]);
Route::post(
'settings/mailtest',
[