firefly-iii/app/Http/Middleware/Authenticate.php

139 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2018-03-05 12:35:58 -06:00
/**
* Authenticate.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
namespace FireflyIII\Http\Middleware;
2015-02-05 21:39:52 -06:00
use Closure;
2018-04-16 12:25:33 -05:00
use FireflyIII\Exceptions\FireflyException;
2018-02-09 07:47:37 -06:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as Auth;
2018-04-16 12:25:33 -05:00
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
/**
2018-02-09 07:47:37 -06:00
* Class Authenticate
*/
2015-02-11 00:35:10 -06:00
class Authenticate
{
2018-02-09 07:47:37 -06:00
/**
* The authentication factory instance.
*
* @var Auth
2018-02-09 07:47:37 -06:00
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param Auth $auth
2018-02-09 07:47:37 -06:00
*
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
2015-02-11 00:35:10 -06:00
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @param string[] ...$guards
2018-02-09 07:47:37 -06:00
*
2018-04-27 23:23:13 -05:00
* @throws AuthenticationException
* @throws FireflyException
* @return mixed
*
2015-02-11 00:35:10 -06:00
*/
2018-02-09 07:47:37 -06:00
public function handle($request, Closure $next, ...$guards)
2015-02-11 00:35:10 -06:00
{
$this->authenticate($request, $guards);
2016-02-07 00:36:31 -06:00
2018-02-09 07:47:37 -06:00
return $next($request);
}
2018-07-08 05:08:53 -05:00
2018-02-09 07:47:37 -06:00
/**
* Determine if the user is logged in to any of the given guards.
*
2019-02-13 10:38:41 -06:00
* @param $request
* @param array $guards
2018-02-09 07:47:37 -06:00
*
2019-02-13 10:38:41 -06:00
* @throws AuthenticationException
2018-04-16 12:25:33 -05:00
* @throws FireflyException
* @return mixed
2018-02-09 07:47:37 -06:00
*/
protected function authenticate($request, array $guards)
2018-02-09 07:47:37 -06:00
{
2018-04-16 12:25:33 -05:00
2018-02-09 07:47:37 -06:00
if (empty($guards)) {
2018-04-16 12:25:33 -05:00
try {
// go for default guard:
2018-07-09 12:24:08 -05:00
/** @noinspection PhpUndefinedMethodInspection */
2018-04-16 12:25:33 -05:00
if ($this->auth->check()) {
2018-02-09 07:47:37 -06:00
2018-04-16 12:25:33 -05:00
// do an extra check on user object.
2018-07-09 12:24:08 -05:00
/** @noinspection PhpUndefinedMethodInspection */
2018-04-16 12:25:33 -05:00
$user = $this->auth->authenticate();
if (1 === (int) $user->blocked) {
$message = (string) trans('firefly.block_account_logout');
2018-04-16 12:25:33 -05:00
if ('email_changed' === $user->blocked_code) {
$message = (string) trans('firefly.email_changed_logout');
2018-04-16 12:25:33 -05:00
}
app('session')->flash('logoutMessage', $message);
2018-07-09 12:24:08 -05:00
/** @noinspection PhpUndefinedMethodInspection */
2018-04-16 12:25:33 -05:00
$this->auth->logout();
throw new AuthenticationException('Blocked account.', $guards);
}
2018-02-09 07:47:37 -06:00
}
2018-04-16 12:25:33 -05:00
} catch (QueryException $e) {
2018-06-01 23:11:13 -05:00
// @codeCoverageIgnoreStart
2018-07-08 00:59:58 -05:00
throw new FireflyException(
sprintf(
'It seems the database has not yet been initialized. Did you run the correct upgrade or installation commands? Error: %s',
$e->getMessage()
)
);
2018-06-01 23:11:13 -05:00
// @codeCoverageIgnoreEnd
}
2018-07-13 08:50:42 -05:00
2018-07-09 12:24:08 -05:00
/** @noinspection PhpUndefinedMethodInspection */
2018-02-09 07:47:37 -06:00
return $this->auth->authenticate();
}
2018-03-03 10:16:47 -06:00
// @codeCoverageIgnoreStart
2018-02-09 07:47:37 -06:00
foreach ($guards as $guard) {
if ($this->auth->guard($guard)->check()) {
2018-07-09 12:24:08 -05:00
/** @noinspection PhpVoidFunctionResultUsedInspection */
2018-02-09 07:47:37 -06:00
return $this->auth->shouldUse($guard);
}
}
2018-02-09 07:47:37 -06:00
throw new AuthenticationException('Unauthenticated.', $guards);
2018-03-03 10:16:47 -06:00
// @codeCoverageIgnoreEnd
2015-02-11 00:35:10 -06:00
}
2015-02-05 21:39:52 -06:00
}