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) 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) public function getView(Request $request, $uuid, $id)

View File

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

View File

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

View File

@ -214,6 +214,49 @@
</div> </div>
</div> </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) @can('edit-subuser', $server)
<div class="well"> <div class="well">
<div class="row"> <div class="row">

View File

@ -203,6 +203,49 @@
</div> </div>
</div> </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) @can('edit-subuser', $server)
<div class="well"> <div class="well">
<div class="row"> <div class="row">