Expand exception code and fix demo user redirect.

This commit is contained in:
James Cole 2018-03-07 05:51:51 +01:00
parent f349aa47ce
commit d52d8d7970
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
10 changed files with 22 additions and 22 deletions

View File

@ -22,9 +22,10 @@ declare(strict_types=1);
namespace FireflyIII\Exceptions; namespace FireflyIII\Exceptions;
use Exception;
/** /**
* Class FireflyException. * Class FireflyException.
*/ */
class FireflyException extends \Exception class FireflyException extends Exception
{ {
} }

View File

@ -70,14 +70,15 @@ class Handler extends ExceptionHandler
return parent::render($request, $exception); return parent::render($request, $exception);
} }
if ($exception instanceof NotFoundHttpException && $request->expectsJson()) { if ($exception instanceof NotFoundHttpException && $request->expectsJson()) {
// JSON error:
return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404); return response()->json(['message' => 'Resource not found', 'exception' => 'NotFoundHttpException'], 404);
} }
if ($exception instanceof AuthenticationException && $request->expectsJson()) { if ($exception instanceof AuthenticationException && $request->expectsJson()) {
// somehow Laravel handler does not catch this:
return response()->json(['message' => 'Unauthenticated', 'exception' => 'AuthenticationException'], 401); return response()->json(['message' => 'Unauthenticated', 'exception' => 'AuthenticationException'], 401);
} }
if ($request->expectsJson()) { if ($request->expectsJson()) {
$isDebug = config('app.debug', false); $isDebug = config('app.debug', false);
if ($isDebug) { if ($isDebug) {

View File

@ -61,7 +61,7 @@ class AuthenticateTwoFactor
public function handle($request, Closure $next, ...$guards) public function handle($request, Closure $next, ...$guards)
{ {
if ($this->auth->guest()) { if ($this->auth->guest()) {
return redirect()->guest('login'); return response()->redirectTo(route('login'));
} }
$is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data; $is2faEnabled = app('preferences')->get('twoFactorAuthEnabled', false)->data;
@ -71,7 +71,7 @@ class AuthenticateTwoFactor
if ($is2faEnabled && $has2faSecret && !$is2faAuthed) { if ($is2faEnabled && $has2faSecret && !$is2faAuthed) {
Log::debug('Does not seem to be 2 factor authed, redirect.'); Log::debug('Does not seem to be 2 factor authed, redirect.');
return redirect(route('two-factor.index')); return response()->redirectTo(route('two-factor.index'));
} }
return $next($request); return $next($request);

View File

@ -48,12 +48,12 @@ class IsAdmin
return response('Unauthorized.', 401); return response('Unauthorized.', 401);
} }
return redirect()->guest('login'); return response()->redirectTo(route('login'));
} }
/** @var User $user */ /** @var User $user */
$user = auth()->user(); $user = auth()->user();
if (!$user->hasRole('owner')) { if (!$user->hasRole('owner')) {
return redirect(route('home')); return response()->redirectTo(route('home'));
} }
return $next($request); return $next($request);

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Http\Middleware; namespace FireflyIII\Http\Middleware;
use Closure; use Closure;
use FireflyIII\Exceptions\IsDemoUserException;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session; use Session;
/** /**
@ -38,7 +38,6 @@ class IsDemoUser
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Closure $next * @param \Closure $next
* @param string[] ...$guards
* *
* @return mixed * @return mixed
*/ */
@ -51,11 +50,14 @@ class IsDemoUser
} }
if ($user->hasRole('demo')) { if ($user->hasRole('demo')) {
Session::flash('info', strval(trans('firefly.not_available_demo_user'))); $request->session()->flash('info', strval(trans('firefly.not_available_demo_user')));
$current = $request->url();
$previous = $request->session()->previousUrl();
if ($current !== $previous) {
return response()->redirectTo($previous);
}
redirect($request->session()->previousUrl()); return response()->redirectTo(route('index'));
return $next($request);
} }
return $next($request); return $next($request);

View File

@ -51,7 +51,7 @@ class IsSandStormUser
if (1 === intval(getenv('SANDSTORM'))) { if (1 === intval(getenv('SANDSTORM'))) {
Session::flash('warning', strval(trans('firefly.sandstorm_not_available'))); Session::flash('warning', strval(trans('firefly.sandstorm_not_available')));
return redirect(route('index')); return response()->redirectTo(route('index'));
} }
return $next($request); return $next($request);

View File

@ -43,7 +43,7 @@ class RedirectIfAuthenticated
public function handle($request, Closure $next, $guard = null) public function handle($request, Closure $next, $guard = null)
{ {
if (Auth::guard($guard)->check()) { if (Auth::guard($guard)->check()) {
return redirect(route('index')); return response()->redirectTo(route('index'));
} }
return $next($request); return $next($request);

View File

@ -51,7 +51,7 @@ class RedirectIfTwoFactorAuthenticated
$is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated'); $is2faAuthed = 'true' === $request->cookie('twoFactorAuthenticated');
if ($is2faEnabled && $has2faSecret && $is2faAuthed) { if ($is2faEnabled && $has2faSecret && $is2faAuthed) {
return redirect(route('index')); return response()->redirectTo(route('index'));
} }
} }

View File

@ -49,7 +49,6 @@ class AuthenticateTest extends TestCase
public function testMiddlewareAjax() public function testMiddlewareAjax()
{ {
Log::debug('Now at testMiddlewareAjax'); Log::debug('Now at testMiddlewareAjax');
//$this->withoutExceptionHandling();
$server = ['HTTP_X-Requested-With' => 'XMLHttpRequest']; $server = ['HTTP_X-Requested-With' => 'XMLHttpRequest'];
$response = $this->get('/_test/authenticate', $server); $response = $this->get('/_test/authenticate', $server);
$this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); $this->assertEquals(Response::HTTP_UNAUTHORIZED, $response->getStatusCode());
@ -89,15 +88,14 @@ class AuthenticateTest extends TestCase
public function testMiddlewareEmail() public function testMiddlewareEmail()
{ {
Log::debug('Now at testMiddlewareEmail'); Log::debug('Now at testMiddlewareEmail');
//$this->withoutExceptionHandling();
$user = $this->user(); $user = $this->user();
$user->blocked = 1; $user->blocked = 1;
$user->blocked_code = 'email_changed'; $user->blocked_code = 'email_changed';
$this->be($user); $this->be($user);
$response = $this->get('/_test/authenticate'); $response = $this->get('/_test/authenticate');
//$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode()); $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
$response->assertSessionHas('logoutMessage', strval(trans('firefly.email_changed_logout'))); $response->assertSessionHas('logoutMessage', strval(trans('firefly.email_changed_logout')));
//$response->assertRedirect(route('login')); $response->assertRedirect(route('login'));
} }
/** /**

View File

@ -39,7 +39,6 @@ class IsDemoUserTest extends TestCase
*/ */
public function testMiddlewareAuthenticated() public function testMiddlewareAuthenticated()
{ {
$this->withoutExceptionHandling();
$this->be($this->user()); $this->be($this->user());
$response = $this->get('/_test/is-demo'); $response = $this->get('/_test/is-demo');
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
@ -50,7 +49,6 @@ class IsDemoUserTest extends TestCase
*/ */
public function testMiddlewareNotAuthenticated() public function testMiddlewareNotAuthenticated()
{ {
$this->withoutExceptionHandling();
$response = $this->get('/_test/is-demo'); $response = $this->get('/_test/is-demo');
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
} }
@ -62,7 +60,7 @@ class IsDemoUserTest extends TestCase
{ {
$this->be($this->demoUser()); $this->be($this->demoUser());
$response = $this->get('/_test/is-demo'); $response = $this->get('/_test/is-demo');
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); $this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
$response->assertSessionHas('info'); $response->assertSessionHas('info');
} }