From 33ad0c14d168d36a4e7ad42dc6aa6a37a7335849 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sun, 25 Sep 2016 23:56:46 +0200 Subject: Change from lumen 5.2 to laravel 5.3 (Resolves #1) --- app/Console/Commands/.gitkeep | 0 app/Console/Kernel.php | 15 +++- app/Events/Event.php | 10 --- app/Exceptions/Handler.php | 39 +++++++--- .../Controllers/Auth/ForgotPasswordController.php | 32 ++++++++ app/Http/Controllers/Auth/LoginController.php | 39 ++++++++++ app/Http/Controllers/Auth/RegisterController.php | 76 +++++++++++++++++++ .../Controllers/Auth/ResetPasswordController.php | 34 +++++++++ app/Http/Controllers/Controller.php | 7 +- app/Http/Controllers/RandomVerbController.php | 3 +- app/Http/Controllers/RootController.php | 3 +- app/Http/Controllers/UserController.php | 67 ---------------- app/Http/Controllers/VerbController.php | 5 +- app/Http/Kernel.php | 56 ++++++++++++++ app/Http/Middleware/Authenticate.php | 45 ----------- app/Http/Middleware/EncryptCookies.php | 17 +++++ app/Http/Middleware/ExampleMiddleware.php | 20 ----- app/Http/Middleware/Login.php | 34 --------- app/Http/Middleware/RedirectIfAuthenticated.php | 26 +++++++ app/Http/Middleware/VerifyCsrfToken.php | 17 +++++ app/Http/routes.php | 88 ---------------------- app/Jobs/Job.php | 25 ------ app/Listeners/Listener.php | 11 --- app/Providers/AppServiceProvider.php | 33 ++++---- app/Providers/AuthServiceProvider.php | 49 +++++------- app/Providers/BroadcastServiceProvider.php | 26 +++++++ app/Providers/EventServiceProvider.php | 13 ++++ app/Providers/RouteServiceProvider.php | 79 +++++++++++++++++++ app/User.php | 18 +++-- 29 files changed, 516 insertions(+), 371 deletions(-) delete mode 100644 app/Console/Commands/.gitkeep delete mode 100644 app/Events/Event.php create mode 100644 app/Http/Controllers/Auth/ForgotPasswordController.php create mode 100644 app/Http/Controllers/Auth/LoginController.php create mode 100644 app/Http/Controllers/Auth/RegisterController.php create mode 100644 app/Http/Controllers/Auth/ResetPasswordController.php delete mode 100644 app/Http/Controllers/UserController.php create mode 100644 app/Http/Kernel.php delete mode 100644 app/Http/Middleware/Authenticate.php create mode 100644 app/Http/Middleware/EncryptCookies.php delete mode 100644 app/Http/Middleware/ExampleMiddleware.php delete mode 100644 app/Http/Middleware/Login.php create mode 100644 app/Http/Middleware/RedirectIfAuthenticated.php create mode 100644 app/Http/Middleware/VerifyCsrfToken.php delete mode 100644 app/Http/routes.php delete mode 100644 app/Jobs/Job.php delete mode 100644 app/Listeners/Listener.php create mode 100644 app/Providers/BroadcastServiceProvider.php create mode 100644 app/Providers/RouteServiceProvider.php (limited to 'app') diff --git a/app/Console/Commands/.gitkeep b/app/Console/Commands/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ad6e311..622e774 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -3,7 +3,7 @@ namespace App\Console; use Illuminate\Console\Scheduling\Schedule; -use Laravel\Lumen\Console\Kernel as ConsoleKernel; +use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { @@ -24,6 +24,17 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // + // $schedule->command('inspire') + // ->hourly(); + } + + /** + * Register the Closure based commands for the application. + * + * @return void + */ + protected function commands() + { + require base_path('routes/console.php'); } } diff --git a/app/Events/Event.php b/app/Events/Event.php deleted file mode 100644 index b8230f0..0000000 --- a/app/Events/Event.php +++ /dev/null @@ -1,10 +0,0 @@ -expectsJson()) { + return response()->json(['error' => 'Unauthenticated.'], 401); + } + + return redirect()->guest('login'); } } diff --git a/app/Http/Controllers/Auth/ForgotPasswordController.php b/app/Http/Controllers/Auth/ForgotPasswordController.php new file mode 100644 index 0000000..a36a6f4 --- /dev/null +++ b/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -0,0 +1,32 @@ +middleware('guest'); + } +} diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php new file mode 100644 index 0000000..4c81bea --- /dev/null +++ b/app/Http/Controllers/Auth/LoginController.php @@ -0,0 +1,39 @@ +middleware('guest', ['except' => 'logout']); + } +} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php new file mode 100644 index 0000000..ed46cab --- /dev/null +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -0,0 +1,76 @@ +middleware('guest'); + } + + /** + * Get a validator for an incoming registration request. + * + * @param array $data + * @return \Illuminate\Contracts\Validation\Validator + */ + protected function validator(array $data) + { + return Validator::make($data, [ + 'name' => 'required|max:255', + 'email' => 'required|email|max:255|unique:users', + 'password' => 'required|min:6|confirmed', + ]); + } + + /** + * Create a new user instance after a valid registration. + * + * @param array $data + * @return User + */ + protected function create(array $data) + { + $user = new User([ + 'name' => $data['name'], + 'email' => $data['email'], + ]); + + $user->password = $data['password']; + + $user->save(); + + return $user; + } +} diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php new file mode 100644 index 0000000..98d1131 --- /dev/null +++ b/app/Http/Controllers/Auth/ResetPasswordController.php @@ -0,0 +1,34 @@ +middleware('guest'); + } +} diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 0ccb918..03e02a2 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -2,9 +2,12 @@ namespace App\Http\Controllers; -use Laravel\Lumen\Routing\Controller as BaseController; +use Illuminate\Foundation\Bus\DispatchesJobs; +use Illuminate\Routing\Controller as BaseController; +use Illuminate\Foundation\Validation\ValidatesRequests; +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; class Controller extends BaseController { - // + use AuthorizesRequests, DispatchesJobs, ValidatesRequests; } diff --git a/app/Http/Controllers/RandomVerbController.php b/app/Http/Controllers/RandomVerbController.php index 501440a..ceb5ff9 100644 --- a/app/Http/Controllers/RandomVerbController.php +++ b/app/Http/Controllers/RandomVerbController.php @@ -22,9 +22,8 @@ use HebrewParseTrainer\Verb; use HebrewParseTrainer\RandomLog; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; -use Laravel\Lumen\Routing\Controller as BaseController; -class RandomVerbController extends BaseController { +class RandomVerbController extends Controller { public function show() { diff --git a/app/Http/Controllers/RootController.php b/app/Http/Controllers/RootController.php index 3899754..5546a1d 100644 --- a/app/Http/Controllers/RootController.php +++ b/app/Http/Controllers/RootController.php @@ -21,12 +21,11 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Validator; -use Laravel\Lumen\Routing\Controller as BaseController; use HebrewParseTrainer\Root; use HebrewParseTrainer\RootKind; -class RootController extends BaseController { +class RootController extends Controller { public function create(Request $request) { $_kinds = RootKind::all(); diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php deleted file mode 100644 index 7036439..0000000 --- a/app/Http/Controllers/UserController.php +++ /dev/null @@ -1,67 +0,0 @@ - - * - * 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 . - */ -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/Controllers/VerbController.php b/app/Http/Controllers/VerbController.php index 18cdd2f..59289c1 100644 --- a/app/Http/Controllers/VerbController.php +++ b/app/Http/Controllers/VerbController.php @@ -29,9 +29,8 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Validator; -use Laravel\Lumen\Routing\Controller as BaseController; -class VerbController extends BaseController { +class VerbController extends Controller { public function random() { $verbs = Verb::where('active', 1)->get(); @@ -129,7 +128,7 @@ class VerbController extends BaseController { return true; } - public function vote($choice, $verb_id) { + public function vote($verb_id, $choice) { $verb = Verb::findOrFail($verb_id); $user = Auth::user(); diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php new file mode 100644 index 0000000..85782f3 --- /dev/null +++ b/app/Http/Kernel.php @@ -0,0 +1,56 @@ + [ + \App\Http\Middleware\EncryptCookies::class, + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + \Illuminate\Session\Middleware\StartSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \App\Http\Middleware\VerifyCsrfToken::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ], + + 'api' => [ + 'throttle:60,1', + 'bindings', + ], + ]; + + /** + * The application's route middleware. + * + * These middleware may be assigned to groups or used individually. + * + * @var array + */ + protected $routeMiddleware = [ + 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, + 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, + 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, + 'can' => \Illuminate\Auth\Middleware\Authorize::class, + 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, + 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, + ]; +} diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php deleted file mode 100644 index 6db8bb0..0000000 --- a/app/Http/Middleware/Authenticate.php +++ /dev/null @@ -1,45 +0,0 @@ -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/Middleware/EncryptCookies.php b/app/Http/Middleware/EncryptCookies.php new file mode 100644 index 0000000..3aa15f8 --- /dev/null +++ b/app/Http/Middleware/EncryptCookies.php @@ -0,0 +1,17 @@ +has('login') && !Auth::check()) { - return response('Unauthorized.', 401) - ->header('WWW-Authenticate', 'Basic realm="Please enter your email and password"'); - } - - return $next($request); - } -} diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php new file mode 100644 index 0000000..e27860e --- /dev/null +++ b/app/Http/Middleware/RedirectIfAuthenticated.php @@ -0,0 +1,26 @@ +check()) { + return redirect('/'); + } + + return $next($request); + } +} diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php new file mode 100644 index 0000000..a2c3541 --- /dev/null +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -0,0 +1,17 @@ + - * - * 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 . - */ - -/* -|-------------------------------------------------------------------------- -| Application Routes -|-------------------------------------------------------------------------- -| -| Here is where you can register all of the routes for an application. -| It is a breeze. Simply tell Lumen the URIs it should respond to -| and give it the Closure to call when that URI is requested. -| -*/ - -$app->group( - [ - 'prefix' => parse_url(env('APP_URL'), PHP_URL_PATH), - 'middleware' => 'login' - ], - 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\VerbController@random'); - - $app->get('/logout', function () use ($app) { - return response('You have been logged out.', 401) - ->header( - 'WWW-Authenticate', - 'Basic realm="Please click OK, then Cancel to logout."'); - }); - - $app->get('/contribute', function () use ($app) { - return view('contribute'); - }); - - $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'); - }); - - $app->get('/verb/{id}/vote/{choice}', - 'App\Http\Controllers\VerbController@vote'); - - $app->post('/verb/suggest', - 'App\Http\Controllers\VerbController@suggest'); - - $app->post('/root/create', - 'App\Http\Controllers\RootController@create'); - - }); - -}); diff --git a/app/Jobs/Job.php b/app/Jobs/Job.php deleted file mode 100644 index 2bc4975..0000000 --- a/app/Jobs/Job.php +++ /dev/null @@ -1,25 +0,0 @@ -app->singleton('mailer', function ($app) { - $app->configure('services'); - return $app->loadComponent('mail', 'Illuminate\Mail\MailServiceProvider', 'mailer'); - }); + /** + * Bootstrap any application services. + * + * @return void + */ + public function boot() + { + // + } - User::observe(UserObserver::class); - } + /** + * Register any application services. + * + * @return void + */ + public function register() + { + // + } } diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 7cf4b27..9784b1a 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -2,38 +2,29 @@ namespace App\Providers; -use App\User; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Gate; -use Illuminate\Support\ServiceProvider; +use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { - /** - * Register any application services. - * - * @return void - */ - public function register() - { - // - } + /** + * The policy mappings for the application. + * + * @var array + */ + protected $policies = [ + 'App\Model' => 'App\Policies\ModelPolicy', + ]; - /** - * Boot the authentication services for the application. - * - * @return void - */ - public function boot() - { - // Here you may define how you wish users to be authenticated for your Lumen - // application. The callback which receives the incoming request instance - // should return either a User instance or null. You're free to obtain - // the User instance via an API token or any other method necessary. - Auth::viaRequest('api', function ($request) { - if ($request->input('api_token')) { - return User::where('api_token', $request->input('api_token'))->first(); - } - }); - } + /** + * Register any authentication / authorization services. + * + * @return void + */ + public function boot() + { + $this->registerPolicies(); + + // + } } diff --git a/app/Providers/BroadcastServiceProvider.php b/app/Providers/BroadcastServiceProvider.php new file mode 100644 index 0000000..1dcf8d2 --- /dev/null +++ b/app/Providers/BroadcastServiceProvider.php @@ -0,0 +1,26 @@ +id === (int) $userId; + }); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index ff72210..a182657 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use Illuminate\Support\Facades\Event; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider @@ -16,4 +17,16 @@ class EventServiceProvider extends ServiceProvider 'App\Listeners\EventListener', ], ]; + + /** + * Register any events for your application. + * + * @return void + */ + public function boot() + { + parent::boot(); + + // + } } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php new file mode 100644 index 0000000..87ffb05 --- /dev/null +++ b/app/Providers/RouteServiceProvider.php @@ -0,0 +1,79 @@ +mapApiRoutes(); + + $this->mapWebRoutes(); + + // + } + + /** + * Define the "web" routes for the application. + * + * These routes all receive session state, CSRF protection, etc. + * + * @return void + */ + protected function mapWebRoutes() + { + Route::group([ + 'middleware' => 'web', + 'namespace' => $this->namespace, + ], function ($router) { + require base_path('routes/web.php'); + }); + } + + /** + * Define the "api" routes for the application. + * + * These routes are typically stateless. + * + * @return void + */ + protected function mapApiRoutes() + { + Route::group([ + 'middleware' => 'api', + 'namespace' => $this->namespace, + 'prefix' => 'api', + ], function ($router) { + require base_path('routes/api.php'); + }); + } +} diff --git a/app/User.php b/app/User.php index f1ff869..8853733 100644 --- a/app/User.php +++ b/app/User.php @@ -19,10 +19,16 @@ namespace HebrewParseTrainer; use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Contracts\Auth\CanResetPassword; +use Illuminate\Auth\Passwords\CanResetPassword as CanResetPasswordTrait; +use Illuminate\Notifications\Notifiable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Hash; -class User extends Model implements Authenticatable { +class User extends Model implements Authenticatable, CanResetPassword { + + use CanResetPasswordTrait; + use Notifiable; protected $table = 'users'; public $timestamps = false; @@ -52,10 +58,6 @@ class User extends Model implements Authenticatable { return floor(log($this->points, self::VOTE_WEIGHT_BASE)); } - public function setPasswordAttribute($pass) { - $this->attributes['password'] = Hash::make($pass); - } - public function verifyPassword($pass) { if (!Hash::check($pass, $this->password)) return false; @@ -81,14 +83,16 @@ class User extends Model implements Authenticatable { } public function getRememberToken() { - return null; + return $this->remember_token; } public function setRememberToken($token) { + $this->remember_token = $token; + $this->save(); } public function getRememberTokenName() { - return null; + return 'remember_token'; } } -- cgit v1.2.3