Add subuser support to tasks

Also allow task creation…
This commit is contained in:
Dane Everitt 2016-09-05 17:39:58 -04:00
parent 9b4a0ed143
commit 9fb0cb420e
5 changed files with 187 additions and 80 deletions

View file

@ -71,7 +71,24 @@ class TaskController extends Controller
public function postNew(Request $request, $uuid)
{
dd($request->input());
$server = Models\Server::getByUUID($uuid);
$this->authorize('create-task', $server);
try {
$repo = new Repositories\TaskRepository;
$repo->create($server->id, $request->except([
'_token'
]));
} catch (DisplayValidationException $ex) {
return redirect()->route('server.tasks', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An unknown error occured while attempting to create this task.')->flash();
}
return redirect()->route('server.tasks', $uuid);
}
public function getView(Request $request, $uuid, $id)

View file

@ -106,12 +106,12 @@ class TaskRepository
$validator = Validator::make($data, [
'action' => 'string|required',
'data' => 'string|required',
'year' => 'string|sometimes|required',
'day_of_week' => 'string|sometimes|required',
'month' => 'string|sometimes|required',
'day_of_month' => 'string|sometimes|required',
'hour' => 'string|sometimes|required',
'minute' => 'string|sometimes|required'
'year' => 'string|sometimes',
'day_of_week' => 'string|sometimes',
'month' => 'string|sometimes',
'day_of_month' => 'string|sometimes',
'hour' => 'string|sometimes',
'minute' => 'string|sometimes'
]);
if ($validator->fails()) {
@ -124,7 +124,7 @@ class TaskRepository
$cron = $this->defaults;
foreach ($this->defaults as $setting => $value) {
if (array_key_exists($setting, $data)) {
if (array_key_exists($setting, $data) && !is_null($data[$setting]) && $data[$setting] !== '') {
$cron[$setting] = $data[$setting];
}
}
@ -140,7 +140,7 @@ class TaskRepository
$cron['year']
));
} catch (\Exception $ex) {
throw new DisplayException($ex);
throw $ex;
}
$task = new Models\Task;

View file

@ -40,7 +40,8 @@
<tbody>
@foreach($tasks as $task)
<tr @if($task->active === 0)class="text-disabled"@endif>
<td><a href="{{ route('server.tasks.view', [ $server->uuidShort, $task->id ]) }}">{{ $actions[$task->action] }}</a></td>
{{-- <td><a href="{{ route('server.tasks.view', [ $server->uuidShort, $task->id ]) }}">{{ $actions[$task->action] }}</a></td> --}}
<td>{{ $actions[$task->action] }}</td>
<td><code>{{ $task->data }}</code></td>
<td>{{ Carbon::parse($task->last_run)->toDayDateTimeString() }} <p class="text-muted"><small>({{ Carbon::parse($task->last_run)->diffForHumans() }})</small></p></td>
<td>
@ -70,82 +71,85 @@ $(document).ready(function () {
$('.server-tasks').addClass('active');
$('[data-toggle="tooltip"]').tooltip();
$('[data-action="delete-task"]').click(function (event) {
var self = $(this);
swal({
type: 'error',
title: 'Delete Task?',
text: 'Are you sure you want to delete this task? There is no undo.',
showCancelButton: true,
allowOutsideClick: true,
closeOnConfirm: false,
confirmButtonText: 'Delete Task',
confirmButtonColor: '#d9534f',
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'DELETE',
url: '{{ route('server.tasks', $server->uuidShort) }}/delete/' + self.data('id'),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Task has been deleted.'
});
self.parent().parent().slideUp();
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: 'An error occured while attempting to delete this task.'
@can('delete-task', $server)
$('[data-action="delete-task"]').click(function (event) {
var self = $(this);
swal({
type: 'error',
title: 'Delete Task?',
text: 'Are you sure you want to delete this task? There is no undo.',
showCancelButton: true,
allowOutsideClick: true,
closeOnConfirm: false,
confirmButtonText: 'Delete Task',
confirmButtonColor: '#d9534f',
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'DELETE',
url: '{{ route('server.tasks', $server->uuidShort) }}/delete/' + self.data('id'),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Task has been deleted.'
});
self.parent().parent().slideUp();
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: 'An error occured while attempting to delete this task.'
});
});
});
});
});
$('[data-action="toggle-task"]').click(function (event) {
var self = $(this);
swal({
type: 'info',
title: 'Toggle Task',
text: 'This will toggle the selected task.',
showCancelButton: true,
allowOutsideClick: true,
closeOnConfirm: false,
confirmButtonText: 'Continue',
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'POST',
url: '{{ route('server.tasks', $server->uuidShort) }}/toggle/' + self.data('id'),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Task has been toggled.'
});
if (data.status !== 1) {
self.parent().parent().addClass('text-disabled');
} else {
self.parent().parent().removeClass('text-disabled');
}
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: 'An error occured while attempting to toggle this task.'
@endcan
@can('toggle-task', $server)
$('[data-action="toggle-task"]').click(function (event) {
var self = $(this);
swal({
type: 'info',
title: 'Toggle Task',
text: 'This will toggle the selected task.',
showCancelButton: true,
allowOutsideClick: true,
closeOnConfirm: false,
confirmButtonText: 'Continue',
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'POST',
url: '{{ route('server.tasks', $server->uuidShort) }}/toggle/' + self.data('id'),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Task has been toggled.'
});
if (data.status !== 1) {
self.parent().parent().addClass('text-disabled');
} else {
self.parent().parent().removeClass('text-disabled');
}
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: 'An error occured while attempting to toggle this task.'
});
});
});
});
});
@endcan
});
</script>

View file

@ -214,6 +214,49 @@
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 fuelux">
<h4>Task Management</h4><hr />
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['list-tasks']))checked="checked"@endif value="list-tasks"> <strong>List Tasks</strong>
<p class="text-muted"><small>Allows a user to list all tasks (enabled and disabled) on a server.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['view-task']))checked="checked"@endif value="view-task"> <strong>View Task</strong>
<p class="text-muted"><small>Allows a user to view a specific task's details.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['toggle-task']))checked="checked"@endif value="toggle-task"> <strong>Toggle Task</strong>
<p class="text-muted"><small>Allows a user to toggle a task on or off.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['queue-task']))checked="checked"@endif value="queue-task"> <strong>Queue Task</strong>
<p class="text-muted"><small>Allows a user to queue a task to run on next cycle.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['create-task']))checked="checked"@endif value="create-task"> <strong>Create Task</strong>
<p class="text-muted"><small>Allows a user to create new tasks.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['delete-task']))checked="checked"@endif value="delete-task"> <strong>Delete Task</strong>
<p class="text-muted"><small><span class="label label-danger">Danger</span> Allows a user to delete a task.</small><p>
</label>
</div>
</div>
<div class="col-md-6 fuelux">
</div>
</div>
@can('edit-subuser', $server)
<div class="well">
<div class="row">

View file

@ -203,6 +203,49 @@
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 fuelux">
<h4>Task Management</h4><hr />
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['list-tasks']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="list-tasks"> <strong>List Tasks</strong>
<p class="text-muted"><small>Allows a user to list all tasks (enabled and disabled) on a server.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['view-task']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="view-task"> <strong>View Task</strong>
<p class="text-muted"><small>Allows a user to view a specific task's details.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['toggle-task']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="toggle-task"> <strong>Toggle Task</strong>
<p class="text-muted"><small>Allows a user to toggle a task on or off.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['queue-task']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="queue-task"> <strong>Queue Task</strong>
<p class="text-muted"><small>Allows a user to queue a task to run on next cycle.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['create-task']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="create-task"> <strong>Create Task</strong>
<p class="text-muted"><small>Allows a user to create new tasks.</small><p>
</label>
</div>
<div class="checkbox highlight">
<label class="checkbox-custom highlight" data-initialize="checkbox">
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($permissions['delete-task']))checked="checked"@endif @cannot('edit-subuser', $server)disabled="disabled"@endcannot value="delete-task"> <strong>Delete Task</strong>
<p class="text-muted"><small><span class="label label-danger">Danger</span> Allows a user to delete a task.</small><p>
</label>
</div>
</div>
<div class="col-md-6 fuelux">
</div>
</div>
@can('edit-subuser', $server)
<div class="well">
<div class="row">