mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-01-17 13:38:20 +01:00
Merge pull request #744 from cachethq/auth
Deal with 401 exceptions in a better way
This commit is contained in:
commit
a27fe6f8ad
87
app/Exceptions/Displayers/RedirectDisplayer.php
Normal file
87
app/Exceptions/Displayers/RedirectDisplayer.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Cachet.
|
||||||
|
*
|
||||||
|
* (c) Cachet HQ <support@cachethq.io>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace CachetHQ\Cachet\Exceptions\Displayers;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use GrahamCampbell\Exceptions\Displayers\DisplayerInterface;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
||||||
|
|
||||||
|
class RedirectDisplayer implements DisplayerInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The request instance.
|
||||||
|
*
|
||||||
|
* @var \Illuminate\Http\Request
|
||||||
|
*/
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new redirect displayer instance.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(Request $request)
|
||||||
|
{
|
||||||
|
$this->request = $request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the error response associated with the given exception.
|
||||||
|
*
|
||||||
|
* @param \Exception $exception
|
||||||
|
* @param int $code
|
||||||
|
* @param string[] $headers
|
||||||
|
*
|
||||||
|
* @return \Symfony\Component\HttpFoundation\Response
|
||||||
|
*/
|
||||||
|
public function display(Exception $exception, $code, array $headers)
|
||||||
|
{
|
||||||
|
return redirect()->guest('auth/login');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the supported content type.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function contentType()
|
||||||
|
{
|
||||||
|
return 'text/html';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can we display the exception?
|
||||||
|
*
|
||||||
|
* @param \Exception $exception
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function canDisplay(Exception $exception)
|
||||||
|
{
|
||||||
|
$redirect = $exception instanceof HttpExceptionInterface && $exception->getStatusCode() === 401;
|
||||||
|
|
||||||
|
return $redirect && !$this->request->is('api*');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do we provide verbose information about the exception?
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isVerbose()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -26,6 +26,7 @@ return [
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
'displayers' => [
|
'displayers' => [
|
||||||
|
'CachetHQ\Cachet\Exceptions\Displayers\RedirectDisplayer',
|
||||||
'GrahamCampbell\Exceptions\Displayers\DebugDisplayer',
|
'GrahamCampbell\Exceptions\Displayers\DebugDisplayer',
|
||||||
'GrahamCampbell\Exceptions\Displayers\HtmlDisplayer',
|
'GrahamCampbell\Exceptions\Displayers\HtmlDisplayer',
|
||||||
'GrahamCampbell\Exceptions\Displayers\JsonDisplayer',
|
'GrahamCampbell\Exceptions\Displayers\JsonDisplayer',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user