Skip to content

Commit 9fb0cb4

Browse files
committed
Add subuser support to tasks
Also allow task creation…
1 parent 9b4a0ed commit 9fb0cb4

File tree

5 files changed

+187
-80
lines changed

5 files changed

+187
-80
lines changed

app/Http/Controllers/Server/TaskController.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,24 @@ public function getNew(Request $request, $uuid)
7171

7272
public function postNew(Request $request, $uuid)
7373
{
74-
dd($request->input());
74+
$server = Models\Server::getByUUID($uuid);
75+
$this->authorize('create-task', $server);
76+
77+
try {
78+
$repo = new Repositories\TaskRepository;
79+
$repo->create($server->id, $request->except([
80+
'_token'
81+
]));
82+
} catch (DisplayValidationException $ex) {
83+
return redirect()->route('server.tasks', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput();
84+
} catch (DisplayException $ex) {
85+
Alert::danger($ex->getMessage())->flash();
86+
} catch (\Exception $ex) {
87+
Log::error($ex);
88+
Alert::danger('An unknown error occured while attempting to create this task.')->flash();
89+
}
90+
return redirect()->route('server.tasks', $uuid);
91+
7592
}
7693

7794
public function getView(Request $request, $uuid, $id)

app/Repositories/TaskRepository.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ public function create($id, $data)
106106
$validator = Validator::make($data, [
107107
'action' => 'string|required',
108108
'data' => 'string|required',
109-
'year' => 'string|sometimes|required',
110-
'day_of_week' => 'string|sometimes|required',
111-
'month' => 'string|sometimes|required',
112-
'day_of_month' => 'string|sometimes|required',
113-
'hour' => 'string|sometimes|required',
114-
'minute' => 'string|sometimes|required'
109+
'year' => 'string|sometimes',
110+
'day_of_week' => 'string|sometimes',
111+
'month' => 'string|sometimes',
112+
'day_of_month' => 'string|sometimes',
113+
'hour' => 'string|sometimes',
114+
'minute' => 'string|sometimes'
115115
]);
116116

117117
if ($validator->fails()) {
@@ -124,7 +124,7 @@ public function create($id, $data)
124124

125125
$cron = $this->defaults;
126126
foreach ($this->defaults as $setting => $value) {
127-
if (array_key_exists($setting, $data)) {
127+
if (array_key_exists($setting, $data) && !is_null($data[$setting]) && $data[$setting] !== '') {
128128
$cron[$setting] = $data[$setting];
129129
}
130130
}
@@ -140,7 +140,7 @@ public function create($id, $data)
140140
$cron['year']
141141
));
142142
} catch (\Exception $ex) {
143-
throw new DisplayException($ex);
143+
throw $ex;
144144
}
145145

146146
$task = new Models\Task;

resources/views/server/tasks/index.blade.php

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
<tbody>
4141
@foreach($tasks as $task)
4242
<tr @if($task->active === 0)class="text-disabled"@endif>
43-
<td><a href="{{ route('server.tasks.view', [ $server->uuidShort, $task->id ]) }}">{{ $actions[$task->action] }}</a></td>
43+
{{-- <td><a href="{{ route('server.tasks.view', [ $server->uuidShort, $task->id ]) }}">{{ $actions[$task->action] }}</a></td> --}}
44+
<td>{{ $actions[$task->action] }}</td>
4445
<td><code>{{ $task->data }}</code></td>
4546
<td>{{ Carbon::parse($task->last_run)->toDayDateTimeString() }} <p class="text-muted"><small>({{ Carbon::parse($task->last_run)->diffForHumans() }})</small></p></td>
4647
<td>
@@ -70,82 +71,85 @@
7071
$('.server-tasks').addClass('active');
7172
$('[data-toggle="tooltip"]').tooltip();
7273
73-
$('[data-action="delete-task"]').click(function (event) {
74-
var self = $(this);
75-
swal({
76-
type: 'error',
77-
title: 'Delete Task?',
78-
text: 'Are you sure you want to delete this task? There is no undo.',
79-
showCancelButton: true,
80-
allowOutsideClick: true,
81-
closeOnConfirm: false,
82-
confirmButtonText: 'Delete Task',
83-
confirmButtonColor: '#d9534f',
84-
showLoaderOnConfirm: true
85-
}, function () {
86-
$.ajax({
87-
method: 'DELETE',
88-
url: '{{ route('server.tasks', $server->uuidShort) }}/delete/' + self.data('id'),
89-
headers: {
90-
'X-CSRF-TOKEN': '{{ csrf_token() }}'
91-
}
92-
}).done(function (data) {
93-
swal({
94-
type: 'success',
95-
title: '',
96-
text: 'Task has been deleted.'
97-
});
98-
self.parent().parent().slideUp();
99-
}).fail(function (jqXHR) {
100-
console.error(jqXHR);
101-
swal({
102-
type: 'error',
103-
title: 'Whoops!',
104-
text: 'An error occured while attempting to delete this task.'
74+
@can('delete-task', $server)
75+
$('[data-action="delete-task"]').click(function (event) {
76+
var self = $(this);
77+
swal({
78+
type: 'error',
79+
title: 'Delete Task?',
80+
text: 'Are you sure you want to delete this task? There is no undo.',
81+
showCancelButton: true,
82+
allowOutsideClick: true,
83+
closeOnConfirm: false,
84+
confirmButtonText: 'Delete Task',
85+
confirmButtonColor: '#d9534f',
86+
showLoaderOnConfirm: true
87+
}, function () {
88+
$.ajax({
89+
method: 'DELETE',
90+
url: '{{ route('server.tasks', $server->uuidShort) }}/delete/' + self.data('id'),
91+
headers: {
92+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
93+
}
94+
}).done(function (data) {
95+
swal({
96+
type: 'success',
97+
title: '',
98+
text: 'Task has been deleted.'
99+
});
100+
self.parent().parent().slideUp();
101+
}).fail(function (jqXHR) {
102+
console.error(jqXHR);
103+
swal({
104+
type: 'error',
105+
title: 'Whoops!',
106+
text: 'An error occured while attempting to delete this task.'
107+
});
105108
});
106109
});
107110
});
108-
});
109-
110-
$('[data-action="toggle-task"]').click(function (event) {
111-
var self = $(this);
112-
swal({
113-
type: 'info',
114-
title: 'Toggle Task',
115-
text: 'This will toggle the selected task.',
116-
showCancelButton: true,
117-
allowOutsideClick: true,
118-
closeOnConfirm: false,
119-
confirmButtonText: 'Continue',
120-
showLoaderOnConfirm: true
121-
}, function () {
122-
$.ajax({
123-
method: 'POST',
124-
url: '{{ route('server.tasks', $server->uuidShort) }}/toggle/' + self.data('id'),
125-
headers: {
126-
'X-CSRF-TOKEN': '{{ csrf_token() }}'
127-
}
128-
}).done(function (data) {
129-
swal({
130-
type: 'success',
131-
title: '',
132-
text: 'Task has been toggled.'
133-
});
134-
if (data.status !== 1) {
135-
self.parent().parent().addClass('text-disabled');
136-
} else {
137-
self.parent().parent().removeClass('text-disabled');
138-
}
139-
}).fail(function (jqXHR) {
140-
console.error(jqXHR);
141-
swal({
142-
type: 'error',
143-
title: 'Whoops!',
144-
text: 'An error occured while attempting to toggle this task.'
111+
@endcan
112+
@can('toggle-task', $server)
113+
$('[data-action="toggle-task"]').click(function (event) {
114+
var self = $(this);
115+
swal({
116+
type: 'info',
117+
title: 'Toggle Task',
118+
text: 'This will toggle the selected task.',
119+
showCancelButton: true,
120+
allowOutsideClick: true,
121+
closeOnConfirm: false,
122+
confirmButtonText: 'Continue',
123+
showLoaderOnConfirm: true
124+
}, function () {
125+
$.ajax({
126+
method: 'POST',
127+
url: '{{ route('server.tasks', $server->uuidShort) }}/toggle/' + self.data('id'),
128+
headers: {
129+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
130+
}
131+
}).done(function (data) {
132+
swal({
133+
type: 'success',
134+
title: '',
135+
text: 'Task has been toggled.'
136+
});
137+
if (data.status !== 1) {
138+
self.parent().parent().addClass('text-disabled');
139+
} else {
140+
self.parent().parent().removeClass('text-disabled');
141+
}
142+
}).fail(function (jqXHR) {
143+
console.error(jqXHR);
144+
swal({
145+
type: 'error',
146+
title: 'Whoops!',
147+
text: 'An error occured while attempting to toggle this task.'
148+
});
145149
});
146150
});
147151
});
148-
});
152+
@endcan
149153
150154
});
151155
</script>

resources/views/server/users/new.blade.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,49 @@
214214
</div>
215215
</div>
216216
</div>
217+
<div class="row">
218+
<div class="col-md-6 fuelux">
219+
<h4>Task Management</h4><hr />
220+
<div class="checkbox highlight">
221+
<label class="checkbox-custom highlight" data-initialize="checkbox">
222+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['list-tasks']))checked="checked"@endif value="list-tasks"> <strong>List Tasks</strong>
223+
<p class="text-muted"><small>Allows a user to list all tasks (enabled and disabled) on a server.</small><p>
224+
</label>
225+
</div>
226+
<div class="checkbox highlight">
227+
<label class="checkbox-custom highlight" data-initialize="checkbox">
228+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['view-task']))checked="checked"@endif value="view-task"> <strong>View Task</strong>
229+
<p class="text-muted"><small>Allows a user to view a specific task's details.</small><p>
230+
</label>
231+
</div>
232+
<div class="checkbox highlight">
233+
<label class="checkbox-custom highlight" data-initialize="checkbox">
234+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['toggle-task']))checked="checked"@endif value="toggle-task"> <strong>Toggle Task</strong>
235+
<p class="text-muted"><small>Allows a user to toggle a task on or off.</small><p>
236+
</label>
237+
</div>
238+
<div class="checkbox highlight">
239+
<label class="checkbox-custom highlight" data-initialize="checkbox">
240+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['queue-task']))checked="checked"@endif value="queue-task"> <strong>Queue Task</strong>
241+
<p class="text-muted"><small>Allows a user to queue a task to run on next cycle.</small><p>
242+
</label>
243+
</div>
244+
<div class="checkbox highlight">
245+
<label class="checkbox-custom highlight" data-initialize="checkbox">
246+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['create-task']))checked="checked"@endif value="create-task"> <strong>Create Task</strong>
247+
<p class="text-muted"><small>Allows a user to create new tasks.</small><p>
248+
</label>
249+
</div>
250+
<div class="checkbox highlight">
251+
<label class="checkbox-custom highlight" data-initialize="checkbox">
252+
<input class="sr-only" name="permissions[]" type="checkbox" @if(isset($oldInput['delete-task']))checked="checked"@endif value="delete-task"> <strong>Delete Task</strong>
253+
<p class="text-muted"><small><span class="label label-danger">Danger</span> Allows a user to delete a task.</small><p>
254+
</label>
255+
</div>
256+
</div>
257+
<div class="col-md-6 fuelux">
258+
</div>
259+
</div>
217260
@can('edit-subuser', $server)
218261
<div class="well">
219262
<div class="row">

resources/views/server/users/view.blade.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,49 @@
203203
</div>
204204
</div>
205205
</div>
206+
<div class="row">
207+
<div class="col-md-6 fuelux">
208+
<h4>Task Management</h4><hr />
209+
<div class="checkbox highlight">
210+
<label class="checkbox-custom highlight" data-initialize="checkbox">
211+
<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>
212+
<p class="text-muted"><small>Allows a user to list all tasks (enabled and disabled) on a server.</small><p>
213+
</label>
214+
</div>
215+
<div class="checkbox highlight">
216+
<label class="checkbox-custom highlight" data-initialize="checkbox">
217+
<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>
218+
<p class="text-muted"><small>Allows a user to view a specific task's details.</small><p>
219+
</label>
220+
</div>
221+
<div class="checkbox highlight">
222+
<label class="checkbox-custom highlight" data-initialize="checkbox">
223+
<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>
224+
<p class="text-muted"><small>Allows a user to toggle a task on or off.</small><p>
225+
</label>
226+
</div>
227+
<div class="checkbox highlight">
228+
<label class="checkbox-custom highlight" data-initialize="checkbox">
229+
<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>
230+
<p class="text-muted"><small>Allows a user to queue a task to run on next cycle.</small><p>
231+
</label>
232+
</div>
233+
<div class="checkbox highlight">
234+
<label class="checkbox-custom highlight" data-initialize="checkbox">
235+
<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>
236+
<p class="text-muted"><small>Allows a user to create new tasks.</small><p>
237+
</label>
238+
</div>
239+
<div class="checkbox highlight">
240+
<label class="checkbox-custom highlight" data-initialize="checkbox">
241+
<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>
242+
<p class="text-muted"><small><span class="label label-danger">Danger</span> Allows a user to delete a task.</small><p>
243+
</label>
244+
</div>
245+
</div>
246+
<div class="col-md-6 fuelux">
247+
</div>
248+
</div>
206249
@can('edit-subuser', $server)
207250
<div class="well">
208251
<div class="row">

0 commit comments

Comments
 (0)