Add recaptcha implementation #212

This commit is contained in:
Nabeel Shahzad 2018-03-16 11:38:06 -05:00
parent b9380b2c3a
commit 92bb56e344
8 changed files with 80 additions and 21 deletions

View File

@ -69,20 +69,27 @@ class RegisterController extends Controller
*/ */
protected function validator(array $data) protected function validator(array $data)
{ {
return Validator::make($data, [ $rules = [
'name' => 'required|max:255', 'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users', 'email' => 'required|email|max:255|unique:users',
'airline_id' => 'required', 'airline_id' => 'required',
'home_airport_id' => 'required', 'home_airport_id' => 'required',
'password' => 'required|min:5|confirmed', 'password' => 'required|min:5|confirmed',
]); ];
if (config('captcha.enabled')) {
$rules['g-recaptcha-response'] = 'required|captcha';
}
return Validator::make($data, $rules);
} }
/** /**
* Get a validator for an incoming registration request. * Get a validator for an incoming registration request.
* @param array $data * @param array $data
* @return \Illuminate\Contracts\Validation\Validator * @return User
* @throws \RuntimeException * @throws \RuntimeException
* @throws \Exception
*/ */
protected function create(array $data) protected function create(array $data)
{ {
@ -106,20 +113,27 @@ class RegisterController extends Controller
/** /**
* Handle a registration request for the application. * Handle a registration request for the application.
* @throws \RuntimeException * @param Request $request
* @return mixed
* @throws \Exception
*/ */
public function register(Request $request) public function register(Request $request)
{ {
$this->validate(request(), [ $rules = [
'name' => 'required', 'name' => 'required',
'email' => 'required|email|unique:users,email', 'email' => 'required|email|unique:users,email',
'airline_id' => 'required', 'airline_id' => 'required',
'home_airport_id' => 'required', 'home_airport_id' => 'required',
'password' => 'required|confirmed' 'password' => 'required|confirmed'
]); ];
if(config('captcha.enabled')) {
$rules['g-recaptcha-response'] = 'required|captcha';
}
$this->validate(request(), $rules);
$user = $this->create($request->all()); $user = $this->create($request->all());
if($user->state === UserState::PENDING) { if($user->state === UserState::PENDING) {
return view('auth.pending'); return view('auth.pending');
} }

View File

@ -23,6 +23,7 @@ use Laratrust\Traits\LaratrustUserTrait;
* @property Rank rank * @property Rank rank
* @property Journal journal * @property Journal journal
* @property string pilot_id * @property string pilot_id
* @property int state
* @mixin \Illuminate\Notifications\Notifiable * @mixin \Illuminate\Notifications\Notifiable
* @mixin \Laratrust\Traits\LaratrustUserTrait * @mixin \Laratrust\Traits\LaratrustUserTrait
*/ */

View File

@ -1,8 +1,22 @@
<?php <?php
/*
* The keys can be created here
* https://www.google.com/recaptcha/admin
*
* Don't edit this file directly, add the section to your config.php
*/
return [ return [
'secret' => env('NOCAPTCHA_SECRET'), 'enabled' => false,
'sitekey' => env('NOCAPTCHA_SITEKEY'), 'sitekey' => '',
'secret' => '',
# Attributes can be found here:
# https://developers.google.com/recaptcha/docs/display#render_param
'attributes' => [
'data-theme' => 'light',
],
'options' => [ 'options' => [
'timeout' => 2.0, 'timeout' => 2.0,
], ],

View File

@ -87,6 +87,7 @@
}); });
}); });
</script> </script>
@yield('scripts') @yield('scripts')
</body> </body>

View File

@ -105,6 +105,10 @@ return [
'source_name' => [ 'source_name' => [
'required' => 'PIREP Source is required', 'required' => 'PIREP Source is required',
], ],
'g-recaptcha-response' => [
'required' => 'Please verify that you are not a robot.',
'captcha' => 'Captcha error! try again later or contact site admin.',
],
], ],
/** /**

View File

@ -47,10 +47,21 @@ return [
'prefix' => 'phpvms_', 'prefix' => 'phpvms_',
], ],
/*
* You can get a captcha key from here:
* https://www.google.com/recaptcha/admin
*/
'captcha' => [ 'captcha' => [
'enabled' => true, 'enabled' => false,
'secret' => '',
'sitekey' => '', 'sitekey' => '',
'secret' => '',
# Attributes can be found here:
# https://developers.google.com/recaptcha/docs/display#render_param
'attributes' => [
'data-theme' => 'light',
],
], ],
# overrides database.php # overrides database.php

View File

@ -23,6 +23,7 @@
<link href="{{ public_asset('/assets/system/css/vendor.css') }}" rel="stylesheet"/> <link href="{{ public_asset('/assets/system/css/vendor.css') }}" rel="stylesheet"/>
@yield('css') @yield('css')
@yield('scripts_head')
</head> </head>
<body> <body>
<!-- Navbar --> <!-- Navbar -->
@ -90,6 +91,5 @@ $(document).ready(function () {
}); });
</script> </script>
</body> </body>
</html> </html>

View File

@ -12,7 +12,7 @@
<div class="panel-body"> <div class="panel-body">
<h2>Register</h2> <h2>Register</h2>
<label for="name" class="control-label">Full Name</label> <label for="name" class="control-label">Full Name</label>
<div class="input-group form-group-no-border{{ $errors->has('name') ? ' has-danger' : '' }}"> <div class="input-group form-group-no-border {{ $errors->has('name') ? 'has-danger' : '' }}">
{{ Form::text('name', null, ['class' => 'form-control']) }} {{ Form::text('name', null, ['class' => 'form-control']) }}
</div> </div>
@if ($errors->has('name')) @if ($errors->has('name'))
@ -20,7 +20,7 @@
@endif @endif
<label for="email" class="control-label">Email Address</label> <label for="email" class="control-label">Email Address</label>
<div class="input-group form-group-no-border{{ $errors->has('email') ? ' has-danger' : '' }}"> <div class="input-group form-group-no-border {{ $errors->has('email') ? 'has-danger' : '' }}">
{{ Form::text('email', null, ['class' => 'form-control']) }} {{ Form::text('email', null, ['class' => 'form-control']) }}
</div> </div>
@if ($errors->has('email')) @if ($errors->has('email'))
@ -28,7 +28,7 @@
@endif @endif
<label for="airline" class="control-label">Airline</label> <label for="airline" class="control-label">Airline</label>
<div class="input-group form-group-no-border{{ $errors->has('airline') ? ' has-danger' : '' }}"> <div class="input-group form-group-no-border {{ $errors->has('airline') ? 'has-danger' : '' }}">
{{ Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) }} {{ Form::select('airline_id', $airlines, null , ['class' => 'form-control select2']) }}
</div> </div>
@if ($errors->has('airline_id')) @if ($errors->has('airline_id'))
@ -36,7 +36,7 @@
@endif @endif
<label for="home_airport" class="control-label">Home Airport</label> <label for="home_airport" class="control-label">Home Airport</label>
<div class="input-group form-group-no-border{{ $errors->has('home_airport') ? ' has-danger' : '' }}"> <div class="input-group form-group-no-border {{ $errors->has('home_airport') ? 'has-danger' : '' }}">
{{ Form::select('home_airport_id', $airports, null , ['class' => 'form-control select2']) }} {{ Form::select('home_airport_id', $airports, null , ['class' => 'form-control select2']) }}
</div> </div>
@if ($errors->has('home_airport_id')) @if ($errors->has('home_airport_id'))
@ -44,7 +44,7 @@
@endif @endif
<label for="timezone" class="control-label">Timezone</label> <label for="timezone" class="control-label">Timezone</label>
<div class="input-group form-group-no-border{{ $errors->has('timezone') ? ' has-danger' : '' }}"> <div class="input-group form-group-no-border {{ $errors->has('timezone') ? 'has-danger' : '' }}">
{{ Form::select('timezone', $timezones, null, ['id'=>'timezone', 'class' => 'form-control select2' ]) }} {{ Form::select('timezone', $timezones, null, ['id'=>'timezone', 'class' => 'form-control select2' ]) }}
</div> </div>
@if ($errors->has('timezone')) @if ($errors->has('timezone'))
@ -52,7 +52,7 @@
@endif @endif
<label for="password" class="control-label">Password</label> <label for="password" class="control-label">Password</label>
<div class="input-group form-group-no-border{{ $errors->has('password') ? ' has-danger' : '' }}"> <div class="input-group form-group-no-border {{ $errors->has('password') ? 'has-danger' : '' }}">
{{ Form::password('password', ['class' => 'form-control']) }} {{ Form::password('password', ['class' => 'form-control']) }}
</div> </div>
@if ($errors->has('password')) @if ($errors->has('password'))
@ -60,14 +60,24 @@
@endif @endif
<label for="password_confirmation" class="control-label">Confirm Password</label> <label for="password_confirmation" class="control-label">Confirm Password</label>
<div class="input-group form-group-no-border{{ $errors->has('password_confirmation') ? ' has-danger' : '' }}"> <div class="input-group form-group-no-border {{ $errors->has('password_confirmation') ? 'has-danger' : '' }}">
{{ Form::password('password_confirmation', ['class' => 'form-control']) }} {{ Form::password('password_confirmation', ['class' => 'form-control']) }}
</div> </div>
@if ($errors->has('password_confirmation')) @if ($errors->has('password_confirmation'))
<p class="text-danger">{{ $errors->first('password_confirmation') }}</p> <p class="text-danger">{{ $errors->first('password_confirmation') }}</p>
@endif @endif
@include("auth.toc") @if(config('captcha.enabled'))
<label for="g-recaptcha-response" class="control-label">Fill out the captcha</label>
<div class="input-group form-group-no-border {{ $errors->has('g-recaptcha-response') ? 'has-danger' : '' }}">
{!! NoCaptcha::display(config('captcha.attributes')) !!}
</div>
@if ($errors->has('g-recaptcha-response'))
<p class="text-danger">{{ $errors->first('g-recaptcha-response') }}</p>
@endif
@endif
@include('auth.toc')
<div style="width: 100%; text-align: right; padding-top: 20px;"> <div style="width: 100%; text-align: right; padding-top: 20px;">
By registering, you agree to the Term and Conditions<br /><br /> By registering, you agree to the Term and Conditions<br /><br />
@ -82,3 +92,7 @@
<div class="col-sm-4"></div> <div class="col-sm-4"></div>
</div> </div>
@endsection @endsection
@section('scripts')
{!! NoCaptcha::renderJs(config('app.locale')) !!}
@endsection