1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-09 19:52:28 +01:00

Validate forms and APIs using \Validator to reject invalid data

This commit is contained in:
Chaoyi Zha 2016-12-03 15:41:24 -05:00
parent f8565c6dc2
commit 8e3befd96c
6 changed files with 41 additions and 24 deletions

View File

@ -7,18 +7,21 @@ use App\Factories\LinkFactory;
use App\Helpers\LinkHelper; use App\Helpers\LinkHelper;
class ApiLinkController extends ApiController { class ApiLinkController extends ApiController {
public static function shortenLink(Request $request) { public function shortenLink(Request $request) {
$response_type = $request->input('response_type'); $response_type = $request->input('response_type');
$user = self::getApiUserInfo($request); $user = self::getApiUserInfo($request);
/* */ // Validate parameters
$validator = \Validator::make($request->all(), [
'url' => 'required|url'
]);
if ($validator->fails()) {
return abort(400, 'Parameters invalid or missing.');
}
$long_url = $request->input('url'); // * required $long_url = $request->input('url'); // * required
$is_secret = ($request->input('is_secret') == 'true' ? true : false); $is_secret = ($request->input('is_secret') == 'true' ? true : false);
if (!self::checkRequiredArgs([$long_url])) {
abort(400, "Missing required arguments.");
}
$link_ip = $request->ip(); $link_ip = $request->ip();
$custom_ending = $request->input('custom_ending'); $custom_ending = $request->input('custom_ending');
@ -32,18 +35,21 @@ class ApiLinkController extends ApiController {
return self::encodeResponse($formatted_link, 'shorten', $response_type); return self::encodeResponse($formatted_link, 'shorten', $response_type);
} }
public static function lookupLink(Request $request) { public function lookupLink(Request $request) {
$response_type = $request->input('response_type'); $response_type = $request->input('response_type');
$user = self::getApiUserInfo($request); $user = self::getApiUserInfo($request);
/* */ // Validate URL form data
$validator = Validator::make($request, [
'url_ending' => 'required|alpha_dash'
]);
$url_ending = $request->input('url_ending'); // * required if ($validator->fails()) {
return abort(400, 'Parameters invalid or missing.');
if (!self::checkRequiredArgs([$url_ending])) {
abort(400, "Missing required arguments.");
} }
$url_ending = $request->input('url_ending');
// "secret" key required for lookups on secret URLs // "secret" key required for lookups on secret URLs
$url_key = $request->input('url_key'); $url_key = $request->input('url_key');
@ -55,7 +61,6 @@ class ApiLinkController extends ApiController {
} }
} }
if ($link) { if ($link) {
return self::encodeResponse([ return self::encodeResponse([
'long_url' => $link['long_url'], 'long_url' => $link['long_url'],

View File

@ -24,14 +24,15 @@ class LinkController extends Controller {
return redirect(route('index'))->with('error', 'You must be logged in to shorten links.'); return redirect(route('index'))->with('error', 'You must be logged in to shorten links.');
} }
$this->request = $request; // Validate URL form data
$this->validate($request, [
'link-url' => 'required|url'
]);
$long_url = $request->input('link-url'); $long_url = $request->input('link-url');
$custom_ending = $request->input('custom-ending'); $custom_ending = $request->input('custom-ending');
$is_secret = ($request->input('options') == "s" ? true : false); $is_secret = ($request->input('options') == "s" ? true : false);
$creator = session('username'); $creator = session('username');
$link_ip = $request->ip(); $link_ip = $request->ip();
try { try {

View File

@ -57,15 +57,17 @@ class UserController extends Controller {
return redirect(route('index'))->with('error', 'Sorry, but registration is disabled.'); return redirect(route('index'))->with('error', 'Sorry, but registration is disabled.');
} }
// Validate signup form data
$this->validate($request, [
'username' => 'required|alpha_dash',
'password' => 'required',
'email' => 'required|email'
]);
$username = $request->input('username'); $username = $request->input('username');
$password = $request->input('password'); $password = $request->input('password');
$email = $request->input('email'); $email = $request->input('email');
if (!self::checkRequiredArgs([$username, $password, $email])) {
// missing a required argument
return redirect(route('signup'))->with('error', 'Please fill in all required fields.');
}
$ip = $request->ip(); $ip = $request->ip();
$user_exists = UserHelper::userExists($username); $user_exists = UserHelper::userExists($username);

View File

@ -59,8 +59,8 @@ $app->middleware([
Illuminate\Cookie\Middleware\EncryptCookies::class, Illuminate\Cookie\Middleware\EncryptCookies::class,
// Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, // Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
Illuminate\Session\Middleware\StartSession::class, Illuminate\Session\Middleware\StartSession::class,
// Illuminate\View\Middleware\ShareErrorsFromSession::class, Illuminate\View\Middleware\ShareErrorsFromSession::class,
Laravel\Lumen\Http\Middleware\VerifyCsrfToken::class, App\Http\Middleware\VerifyCsrfToken::class
]); ]);
// $app->routeMiddleware([ // $app->routeMiddleware([

View File

@ -102,6 +102,8 @@ return [
| |
*/ */
'attributes' => [], 'attributes' => [
'link-url' => 'link URL'
],
]; ];

View File

@ -70,6 +70,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@if (Session::has('success')) @if (Session::has('success'))
toastr["success"](`{{session('success')}}`, "Success") toastr["success"](`{{session('success')}}`, "Success")
@endif @endif
@if (count($errors) > 0)
// Handle Lumen validation errors
@foreach ($errors->all() as $error)
toastr["error"](`{{$error}}`, "Error")
@endforeach
@endif
</script> </script>
@yield('js') @yield('js')