API Resources and first endpoints

This commit is contained in:
2021-03-31 03:55:09 +01:00
parent 2bc07d8ca0
commit ddd79d38fe
23 changed files with 497 additions and 209 deletions

View File

@@ -11,29 +11,13 @@ use Illuminate\Support\Facades\Hash;
class ApiKeyController extends Controller
{
/**
* Display a listing of the resource.
*
*/
public function index()
{
return view('dashboard.user.api.index')
->with('keys', Auth::user()->keys);
}
$this->authorize('viewAny', ApiKey::class);
public function adminKeys()
{
if (Auth::user()->hasRole('admin'))
{
return view('dashboard.administration.keys')
->with('keys', ApiKey::all());
}
else
{
return redirect()
->back()
->with('error', 'You do not have permission to access this page.');
}
return view('dashboard.administration.keys')
->with('keys', ApiKey::all());
}
/**
@@ -43,6 +27,8 @@ class ApiKeyController extends Controller
*/
public function store(CreateApiKeyRequest $request)
{
$this->authorize('create', ApiKey::class);
$discriminator = "#" . bin2hex(openssl_random_pseudo_bytes(7));
$secret = bin2hex(openssl_random_pseudo_bytes(32));
@@ -71,28 +57,24 @@ class ApiKeyController extends Controller
public function revokeKey(Request $request, ApiKey $key)
{
if (Auth::user()->is($key->user) || Auth::user()->hasRole('admin'))
{
if ($key->status == 'active')
{
$key->status = 'disabled';
$key->save();
}
else
{
return redirect()
->back()
->with('error', 'Key already revoked.');
}
$this->authorize('update', $key);
if ($key->status == 'active')
{
$key->status = 'disabled';
$key->save();
}
else
{
return redirect()
->back()
->with('success', 'Key revoked. Apps using this key will stop working.');
->with('error', 'Key already revoked.');
}
return redirect()
->back()
->with('error', 'You do not have permission to modify this key.');
->with('success', 'Key revoked. Apps using this key will stop working.');
}
/**
@@ -101,18 +83,13 @@ class ApiKeyController extends Controller
public function destroy($id)
{
$key = ApiKey::findOrFail($id);
$this->authorize('delete', $key);
if (Auth::user()->is($key->user) || Auth::user()->hasRole('admin'))
{
$key->delete();
return redirect()
->back()
->with('success', 'Key deleted successfully. Apps using this key will stop working.');
}
$key->delete();
return redirect()
->back()
->with('error', 'You do not have permission to modify this key.');
->with('success', 'Key deleted successfully. Apps using this key will stop working.');
}
}

View File

@@ -23,6 +23,7 @@ namespace App\Http\Controllers;
use App\Application;
use App\Events\ApplicationDeniedEvent;
use App\Http\Resources\ApplicationResource;
use App\Notifications\ApplicationMoved;
use App\Notifications\NewApplicant;
use App\Response;
@@ -56,39 +57,58 @@ class ApplicationController extends Controller
public function showUserApp(Request $request, Application $application)
{
$this->authorize('view', $application);
if (!$request->wantsJson())
{
$this->authorize('view', $application);
if (! is_null($application)) {
return view('dashboard.user.viewapp')
->with(
[
'application' => $application,
'comments' => $application->comments,
'structuredResponses' => json_decode($application->response->responseData, true),
'formStructure' => $application->response->form,
'vacancy' => $application->response->vacancy,
'canVote' => $this->canVote($application->votes),
]
);
} else {
$request->session()->flash('error', 'The application you requested could not be found.');
if (! is_null($application)) {
return view('dashboard.user.viewapp')
->with(
[
'application' => $application,
'comments' => $application->comments,
'structuredResponses' => json_decode($application->response->responseData, true),
'formStructure' => $application->response->form,
'vacancy' => $application->response->vacancy,
'canVote' => $this->canVote($application->votes),
]
);
} else {
$request->session()->flash('error', 'The application you requested could not be found.');
}
return redirect()->back();
}
return redirect()->back();
return (new ApplicationResource($application))->additional([
'meta' => [
'code' => 200,
'status' => 'success'
]
]);
}
public function showAllApps()
public function showAllApps(Request $request)
{
$this->authorize('viewAny', Application::class);
if (!$request->wantsJson())
{
$this->authorize('viewAny', Application::class);
return view('dashboard.appmanagement.all')
->with('applications', Application::paginate(6));
return view('dashboard.appmanagement.all')
->with('applications', Application::paginate(6));
}
// todo: eager load all relationships used
return ApplicationResource::collection(Application::paginate(6))->additional([
'code' => '200',
'status' => 'success',
]);
}
public function renderApplicationForm(Request $request, $vacancySlug)
{
// FIXME: Get rid of references to first(), this is a wonky query
$vacancyWithForm = Vacancy::with('forms')->where('vacancySlug', $vacancySlug)->get();
$firstVacancy = $vacancyWithForm->first();
@@ -96,10 +116,8 @@ class ApplicationController extends Controller
if (! $vacancyWithForm->isEmpty() && $firstVacancy->vacancyCount !== 0 && $firstVacancy->vacancyStatus == 'OPEN') {
return view('dashboard.application-rendering.apply')
->with([
'vacancy' => $vacancyWithForm->first(),
'preprocessedForm' => json_decode($vacancyWithForm->first()->forms->formStructure, true),
]);
} else {
abort(404, 'The application you\'re looking for could not be found or it is currently unavailable.');