Merge pull request #1168 from cachethq/validation

Improved api validation
This commit is contained in:
Graham Campbell
2015-11-21 21:46:10 +00:00
8 changed files with 71 additions and 16 deletions

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Exceptions\Displayers;
use AltThree\Validator\ValidationException;
use Exception;
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
use GrahamCampbell\Exceptions\Displayers\JsonDisplayer;
use Symfony\Component\HttpFoundation\JsonResponse;
class JsonValidationDisplayer extends JsonDisplayer implements DisplayerInterface
{
/**
* Get the error response associated with the given exception.
*
* @param \Exception $exception
* @param string $id
* @param int $code
* @param string[] $headers
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function display(Exception $exception, $id, $code, array $headers)
{
$info = $this->info->generate($exception, $id, 400);
$error = ['id' => $id, 'status' => $info['code'], 'title' => $info['name'], 'detail' => $info['detail'], 'meta' => ['details' => $exception->getMessageBag()->all()]];
return new JsonResponse(['errors' => [$error]], 400, array_merge($headers, ['Content-Type' => $this->contentType()]));
}
/**
* Can we display the exception?
*
* @param \Exception $original
* @param \Exception $transformed
* @param int $code
*
* @return bool
*/
public function canDisplay(Exception $original, Exception $transformed, $code)
{
return $transformed instanceof ValidationException;
}
}

View File

@@ -16,9 +16,9 @@ use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -75,7 +75,7 @@ class ComponentController extends AbstractApiController
Binput::get('group_id'),
(bool) Binput::get('enabled', true)
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
@@ -116,7 +116,7 @@ class ComponentController extends AbstractApiController
Binput::get('group_id'),
(bool) Binput::get('enabled', true)
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}

View File

@@ -15,8 +15,8 @@ use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\UpdateComponentGroupCommand;
use CachetHQ\Cachet\Models\ComponentGroup;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -63,7 +63,7 @@ class ComponentGroupController extends AbstractApiController
Binput::get('name'),
Binput::get('order', 0)
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
@@ -85,7 +85,7 @@ class ComponentGroupController extends AbstractApiController
Binput::get('name'),
Binput::get('order', 0)
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}

View File

@@ -15,9 +15,9 @@ use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand;
use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand;
use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand;
use CachetHQ\Cachet\Models\Incident;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -77,7 +77,7 @@ class IncidentController extends AbstractApiController
Binput::get('template'),
Binput::get('vars')
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
@@ -107,7 +107,7 @@ class IncidentController extends AbstractApiController
Binput::get('template'),
Binput::get('vars')
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}

View File

@@ -15,8 +15,8 @@ use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Commands\Metric\UpdateMetricCommand;
use CachetHQ\Cachet\Models\Metric;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -80,7 +80,7 @@ class MetricController extends AbstractApiController
Binput::get('display_chart'),
Binput::get('places', 2)
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
@@ -107,7 +107,7 @@ class MetricController extends AbstractApiController
Binput::get('display_chart'),
Binput::get('places', 2)
));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}

View File

@@ -16,8 +16,8 @@ use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand;
use CachetHQ\Cachet\Commands\Metric\UpdateMetricPointCommand;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -53,7 +53,7 @@ class MetricPointController extends AbstractApiController
Binput::get('value'),
Binput::get('timestamp'))
);
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}

View File

@@ -14,8 +14,8 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand;
use CachetHQ\Cachet\Models\Subscriber;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -47,7 +47,7 @@ class SubscriberController extends AbstractApiController
{
try {
$subscriber = $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'), Binput::get('verify', false)));
} catch (Exception $e) {
} catch (QueryException $e) {
throw new BadRequestHttpException();
}

View File

@@ -45,6 +45,7 @@ return [
*/
'displayers' => [
'CachetHQ\Cachet\Exceptions\Displayers\JsonValidationDisplayer',
'CachetHQ\Cachet\Exceptions\Displayers\RedirectDisplayer',
'GrahamCampbell\Exceptions\Displayers\DebugDisplayer',
'GrahamCampbell\Exceptions\Displayers\HtmlDisplayer',