diff options
Diffstat (limited to 'app/Http')
-rw-r--r-- | app/Http/Controllers/UserController.php | 67 | ||||
-rw-r--r-- | app/Http/Middleware/Authenticate.php | 45 | ||||
-rw-r--r-- | app/Http/routes.php | 56 |
3 files changed, 148 insertions, 20 deletions
diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..7036439 --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,67 @@ +<?php +/** + * HebrewParseTrainer - practice Hebrew verbs + * Copyright (C) 2015 Camil Staps <info@camilstaps.nl> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +namespace App\Http\Controllers; + +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\ValidationException; +use Laravel\Lumen\Routing\Controller as BaseController; + +use HebrewParseTrainer\User; + +class UserController extends BaseController { + + public function createForm(Request $request) { + $messages = []; + + if ($request->isMethod('post')) { + $validator = Validator::make($request->input(), [ + 'email' => 'required|unique:users|email', + 'name' => 'required|unique:users', + 'password' => 'required|confirmed|min:8', + ]); + + if ($validator->fails()) { + foreach ($validator->errors()->all() as $error) { + $messages[] = ['danger', $error]; + } + } else { + $user = new User; + $user->name = $request->input('name'); + $user->email = $request->input('email'); + $user->password = $request->input('password'); + if ($user->save()) { + $messages[] = ['success', 'Your account has been created.']; + } else { + $messages[] = ['danger', 'Your account could not be created.']; + } + } + } + + return view('user.create', + [ + 'messages' => $messages, + 'form' => [ + 'email' => $request->input('email'), + 'name' => $request->input('name') + ] + ]); + } + +} diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php new file mode 100644 index 0000000..6db8bb0 --- /dev/null +++ b/app/Http/Middleware/Authenticate.php @@ -0,0 +1,45 @@ +<?php + +namespace App\Http\Middleware; + +use Closure; +use Illuminate\Contracts\Auth\Factory as Auth; + +class Authenticate +{ + /** + * The authentication guard factory instance. + * + * @var \Illuminate\Contracts\Auth\Factory + */ + protected $auth; + + /** + * Create a new middleware instance. + * + * @param \Illuminate\Contracts\Auth\Factory $auth + * @return void + */ + public function __construct(Auth $auth) + { + $this->auth = $auth; + } + + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * @param string|null $guard + * @return mixed + */ + public function handle($request, Closure $next, $guard = null) + { + if ($this->auth->guard($guard)->guest()) { + return response('Unauthorized.', 401) + ->header('WWW-Authenticate', 'Basic realm="Please enter your email and password"'); + } + + return $next($request); + } +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 67b661b..9cf12b4 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -28,24 +28,40 @@ | */ -$app->group(['prefix' => parse_url(env('APP_URL'), PHP_URL_PATH)], function ($app) { - - $app->get('/', function () use ($app) { - return view('trainer'); - }); - - $app->get('/stem', function () use ($app) { - return \HebrewParseTrainer\Stem::all(); - }); - - $app->get('/tense', function () use ($app) { - return \HebrewParseTrainer\Tense::all(); - }); - - $app->get('/verb/random', 'App\Http\Controllers\RandomVerbController@show'); - - $app->get('/stats', function () use ($app) { - return view('stats'); - }); - +$app->group( + ['prefix' => parse_url(env('APP_URL'), PHP_URL_PATH)], + function ($app) { + + $app->get('/', function () use ($app) { + return view('trainer'); + }); + + $app->get('/stem', function () use ($app) { + return \HebrewParseTrainer\Stem::all(); + }); + + $app->get('/tense', function () use ($app) { + return \HebrewParseTrainer\Tense::all(); + }); + + $app->get('/logout', function () use ($app) { + return response('Unauthorized.', 401) + ->header('WWW-Authenticate', 'Basic realm="Please click OK, then Cancel to logout."'); + }); + + $app->get('/verb/random', 'App\Http\Controllers\RandomVerbController@show'); + + $app->get('/user/create', 'App\Http\Controllers\UserController@createForm'); + $app->post('/user/create', 'App\Http\Controllers\UserController@createForm'); + + $app->group( + ['middleware' => 'auth:basic-http'], + function ($app) { + + $app->get('/stats', function () use ($app) { + return view('stats'); + }); + + }); + }); |