Update http related classes after upgrade to Laravel 5.2

This commit is contained in:
James Cole 2016-01-08 16:01:21 +01:00
parent ba32a665f1
commit 35deed1d10
5 changed files with 179 additions and 146 deletions

201
app/Http/Controllers/Auth/AuthController.php Normal file → Executable file
View File

@ -1,4 +1,6 @@
<?php namespace FireflyIII\Http\Controllers\Auth; <?php
namespace FireflyIII\Http\Controllers\Auth;
use Auth; use Auth;
use Config; use Config;
@ -9,6 +11,7 @@ use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins; use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Mail\Message; use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Lang;
use Log; use Log;
use Mail; use Mail;
use Request as Rq; use Request as Rq;
@ -16,25 +19,38 @@ use Session;
use Twig; use Twig;
use Validator; use Validator;
/**
* Class AuthController
*
* @package FireflyIII\Http\Controllers\Auth
*/
class AuthController extends Controller class AuthController extends Controller
{ {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins; use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/** /**
* Log the user out of the application. * Where to redirect users after login / registration.
* *
* @return \Illuminate\Http\Response * @var string
*/ */
public function getLogout() protected $redirectTo = '/home';
{
Auth::logout();
return redirect('/auth/login');
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
} }
/** /**
@ -42,13 +58,14 @@ class AuthController extends Controller
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function getRegister() public function showRegistrationForm()
{ {
$host = Rq::getHttpHost(); $host = Rq::getHttpHost();
return view('auth.register', compact('host')); return view('auth.register', compact('host'));
} }
/** /**
* Handle a login request to the application. * Handle a login request to the application.
* *
@ -56,9 +73,17 @@ class AuthController extends Controller
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function postLogin(Request $request) public function login(Request $request)
{ {
$this->validate($request, [$this->loginUsername() => 'required', 'password' => 'required',]); $this->validate(
$request, [
$this->loginUsername() => 'required', 'password' => 'required',
]
);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait(); $throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) { if ($throttles && $this->hasTooManyLoginAttempts($request)) {
@ -68,67 +93,112 @@ class AuthController extends Controller
$credentials = $this->getCredentials($request); $credentials = $this->getCredentials($request);
$credentials['blocked'] = 0; // most not be blocked. $credentials['blocked'] = 0; // most not be blocked.
if (Auth::attempt($credentials, $request->has('remember'))) { if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles); return $this->handleUserWasAuthenticated($request, $throttles);
} }
$message = $this->getFailedLoginMessage(); // check if user is blocked:
$message = '';
/** @var User $foundUser */ /** @var User $foundUser */
$foundUser = User::where('email', $credentials['email'])->where('blocked', 1)->first(); $foundUser = User::where('email', $credentials['email'])->where('blocked', 1)->first();
if (!is_null($foundUser)) { if (!is_null($foundUser)) {
// if it exists, show message: // if it exists, show message:
$code = $foundUser->blocked_code; $code = $foundUser->blocked_code;
if (strlen($code) == 0) { if (strlen($code) == 0) {
$code = 'general_blocked'; $code = 'general_blocked';
} }
$message = trans('firefly.' . $code . '_error', ['email' => $credentials['email']]); $message = trans('firefly.' . $code . '_error', ['email' => $credentials['email']]);
} }
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) { if ($throttles) {
$this->incrementLoginAttempts($request); $this->incrementLoginAttempts($request);
} }
return redirect($this->loginPath()) return $this->sendFailedLoginResponse($request, $message);
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors([$this->loginUsername() => $message,]);
}
public $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @codeCoverageIgnore
*
*/
public function __construct()
{
parent::__construct();
$this->middleware('guest', ['except' => 'getLogout']);
} }
/** /**
* Show the application login form. * Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
* *
* @codeCoverageIgnore
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*
*/ */
public function getLogin() protected function sendFailedLoginResponse(Request $request, $message)
{ {
return Twig::render('auth.login'); return redirect()->back()
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors(
[
$this->loginUsername() => $this->getFailedLoginMessage($message),
]
);
}
/**
* Get the failed login message.
*
* @return string
*/
protected function getFailedLoginMessage($message)
{
if (strlen($message) > 0) {
return $message;
}
return Lang::has('auth.failed')
? Lang::get('auth.failed')
: 'These credentials do not match our records.';
}
/**
* 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, [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]
);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
*
* @return User
*/
protected function create(array $data)
{
return User::create(
[
'email' => $data['email'],
'password' => bcrypt($data['password']),
]
);
} }
/** /**
* Handle a registration request for the application. * Handle a registration request for the application.
* *
* @param Request $request * @param \Illuminate\Http\Request $request
* *
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\Response
*/ */
public function postRegister(Request $request) public function register(Request $request)
{ {
$validator = $this->validator($request->all()); $validator = $this->validator($request->all());
@ -136,10 +206,7 @@ class AuthController extends Controller
$this->throwValidationException( $this->throwValidationException(
$request, $validator $request, $validator
); );
// @codeCoverageIgnoreStart
} }
// @codeCoverageIgnoreEnd
$data = $request->all(); $data = $request->all();
$data['password'] = bcrypt($data['password']); $data['password'] = bcrypt($data['password']);
@ -152,7 +219,8 @@ class AuthController extends Controller
); );
} }
Auth::login($this->create($data));
Auth::login($this->create($request->all()));
// get the email address // get the email address
if (Auth::user() instanceof User) { if (Auth::user() instanceof User) {
@ -187,8 +255,8 @@ class AuthController extends Controller
// @codeCoverageIgnoreStart // @codeCoverageIgnoreStart
abort(500, 'Not a user!'); abort(500, 'Not a user!');
return redirect('/');
// @codeCoverageIgnoreEnd return redirect($this->redirectPath());
} }
/** /**
@ -225,37 +293,4 @@ class AuthController extends Controller
return false; return false;
} }
/**
* Get a validator for an incoming registration request.
*
* @param array $data
*
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make(
$data, [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]
);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
*
* @return User
*/
public function create(array $data)
{
return User::create(
[
'email' => $data['email'],
'password' => $data['password'],
]
);
}
} }

29
app/Http/Controllers/Auth/PasswordController.php Normal file → Executable file
View File

@ -1,4 +1,6 @@
<?php namespace FireflyIII\Http\Controllers\Auth; <?php
namespace FireflyIII\Http\Controllers\Auth;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
use FireflyIII\User; use FireflyIII\User;
@ -7,15 +9,9 @@ use Illuminate\Http\Request;
use Illuminate\Mail\Message; use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password; use Illuminate\Support\Facades\Password;
/**
* Class PasswordController
*
* @codeCoverageIgnore
* @package FireflyIII\Http\Controllers\Auth
*/
class PasswordController extends Controller class PasswordController extends Controller
{ {
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Password Reset Controller | Password Reset Controller
@ -29,14 +25,10 @@ class PasswordController extends Controller
use ResetsPasswords; use ResetsPasswords;
protected $redirectPath = '/';
/** /**
* Create a new password controller instance. * Create a new password controller instance.
* *
* @codeCoverageIgnore * @return void
*
*/ */
public function __construct() public function __construct()
{ {
@ -52,7 +44,7 @@ class PasswordController extends Controller
* *
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function postEmail(Request $request) public function sendResetLinkEmail(Request $request)
{ {
$this->validate($request, ['email' => 'required|email']); $this->validate($request, ['email' => 'required|email']);
@ -70,16 +62,13 @@ class PasswordController extends Controller
switch ($response) { switch ($response) {
case Password::RESET_LINK_SENT: case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response)); return $this->getSendResetLinkEmailSuccessResponse($response);
case Password::INVALID_USER: case Password::INVALID_USER:
case 'passwords.blocked': case 'passwords.blocked':
return redirect()->back()->withErrors(['email' => trans($response)]); default:
return $this->getSendResetLinkEmailFailureResponse($response);
} }
abort(404);
return '';
} }
} }

25
app/Http/Controllers/Controller.php Normal file → Executable file
View File

@ -1,30 +1,27 @@
<?php namespace FireflyIII\Http\Controllers; <?php
use Auth; namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController; use Illuminate\Routing\Controller as BaseController;
use Preferences;
use View; use View;
use Auth;
use Preferences;
use Carbon\Carbon;
/** /**
* Class Controller * Class Controller
* *
* @package FireflyIII\Http\Controllers * @package FireflyIII\Http\Controllers
*/ */
abstract class Controller extends BaseController class Controller extends BaseController
{ {
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
use DispatchesJobs, ValidatesRequests;
/** @var string */
protected $monthAndDayFormat;
/** @var string */
protected $monthFormat;
/** /**
* @codeCoverageIgnore * Controller constructor.
*/ */
public function __construct() public function __construct()
{ {
@ -73,4 +70,6 @@ abstract class Controller extends BaseController
return $sum; return $sum;
} }
} }

53
app/Http/Kernel.php Normal file → Executable file
View File

@ -1,43 +1,56 @@
<?php namespace FireflyIII\Http; <?php
namespace FireflyIII\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel; use Illuminate\Foundation\Http\Kernel as HttpKernel;
/**
* Class Kernel
*
* @package FireflyIII\Http
*/
class Kernel extends HttpKernel class Kernel extends HttpKernel
{ {
/** /**
* The application's global HTTP middleware stack. * The application's global HTTP middleware stack.
* *
* These middleware are run during every request to your application.
*
* @var array * @var array
*/ */
protected $middleware protected $middleware
= [ = [
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode', \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
'Illuminate\Cookie\Middleware\EncryptCookies', ];
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
'Illuminate\Session\Middleware\StartSession', /**
'Illuminate\View\Middleware\ShareErrorsFromSession', * The application's route middleware groups.
'FireflyIII\Http\Middleware\ReplaceTestVars', *
'FireflyIII\Http\Middleware\VerifyCsrfToken', * @var array
*/
protected $middlewareGroups
= [
'web' => [
\FireflyIII\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\FireflyIII\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
]; ];
/** /**
* The application's route middleware. * The application's route middleware.
* *
* These middleware may be assigned to groups or used individually.
*
* @var array * @var array
*/ */
protected $routeMiddleware protected $routeMiddleware
= [ = [
'auth' => 'FireflyIII\Http\Middleware\Authenticate', 'auth' => \FireflyIII\Http\Middleware\Authenticate::class,
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth', 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => 'FireflyIII\Http\Middleware\RedirectIfAuthenticated', 'guest' => \FireflyIII\Http\Middleware\RedirectIfAuthenticated::class,
'range' => 'FireflyIII\Http\Middleware\Range', 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'range' => \FireflyIII\Http\Middleware\Range::class,
]; ];
} }

View File

@ -260,21 +260,18 @@ Route::bind(
); );
/** // auth routes, i think
* Auth\AuthController Route::group(
*/ ['middleware' => 'web'], function () {
Route::get('/register', ['uses' => 'Auth\AuthController@getRegister', 'as' => 'register']); Route::auth();
Route::controllers( Route::get('/home', 'HomeController@index');
[ }
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]
); );
Route::group( Route::group(
['middleware' => ['auth', 'range']], function () { ['middleware' => ['auth', 'range', 'web']], function () {
/** /**
* Home Controller * Home Controller