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

139 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2018-03-05 19:35:58 +01:00
/**
* Authenticate.php
2020-01-31 07:32:04 +01: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 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02: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 08:40:00 +02: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 10:08:34 +02:00
declare(strict_types=1);
namespace FireflyIII\Http\Middleware;
2015-02-06 04:39:52 +01:00
use Closure;
2018-04-16 19:25:33 +02:00
use FireflyIII\Exceptions\FireflyException;
2018-02-09 14:47:37 +01:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as Auth;
2018-04-16 19:25:33 +02:00
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
/**
2018-02-09 14:47:37 +01:00
* Class Authenticate
*/
2015-02-11 07:35:10 +01:00
class Authenticate
{
2018-02-09 14:47:37 +01:00
/**
* The authentication factory instance.
*
* @var Auth
2018-02-09 14:47:37 +01:00
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param Auth $auth
2018-02-09 14:47:37 +01:00
*
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
2015-02-11 07:35:10 +01:00
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @param string[] ...$guards
2018-02-09 14:47:37 +01:00
*
* @return mixed
*
2021-03-21 09:15:40 +01:00
* @throws FireflyException
* @throws AuthenticationException
2015-02-11 07:35:10 +01:00
*/
2018-02-09 14:47:37 +01:00
public function handle($request, Closure $next, ...$guards)
2015-02-11 07:35:10 +01:00
{
$this->authenticate($request, $guards);
2016-02-07 07:36:31 +01:00
2018-02-09 14:47:37 +01:00
return $next($request);
}
2018-07-08 12:08:53 +02:00
2018-02-09 14:47:37 +01:00
/**
* Determine if the user is logged in to any of the given guards.
*
2019-02-13 17:38:41 +01:00
* @param $request
* @param array $guards
2018-02-09 14:47:37 +01:00
*
* @return mixed
2021-03-21 09:15:40 +01:00
* @throws FireflyException
* @throws AuthenticationException
2018-02-09 14:47:37 +01:00
*/
protected function authenticate($request, array $guards)
2018-02-09 14:47:37 +01:00
{
2018-04-16 19:25:33 +02:00
2018-02-09 14:47:37 +01:00
if (empty($guards)) {
2018-04-16 19:25:33 +02:00
try {
// go for default guard:
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
2018-04-16 19:25:33 +02:00
if ($this->auth->check()) {
2018-02-09 14:47:37 +01:00
2018-04-16 19:25:33 +02:00
// do an extra check on user object.
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
2018-04-16 19:25:33 +02:00
$user = $this->auth->authenticate();
2021-03-21 09:15:40 +01:00
if (1 === (int)$user->blocked) {
$message = (string)trans('firefly.block_account_logout');
2018-04-16 19:25:33 +02:00
if ('email_changed' === $user->blocked_code) {
2021-03-21 09:15:40 +01:00
$message = (string)trans('firefly.email_changed_logout');
2018-04-16 19:25:33 +02:00
}
app('session')->flash('logoutMessage', $message);
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
2018-04-16 19:25:33 +02:00
$this->auth->logout();
throw new AuthenticationException('Blocked account.', $guards);
}
2018-02-09 14:47:37 +01:00
}
2018-04-16 19:25:33 +02:00
} catch (QueryException $e) {
2018-06-02 06:11:13 +02:00
// @codeCoverageIgnoreStart
2018-07-08 07:59:58 +02: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-02 06:11:13 +02:00
// @codeCoverageIgnoreEnd
}
2018-07-13 15:50:42 +02:00
2018-07-09 19:24:08 +02:00
/** @noinspection PhpUndefinedMethodInspection */
2018-02-09 14:47:37 +01:00
return $this->auth->authenticate();
}
2018-03-03 17:16:47 +01:00
// @codeCoverageIgnoreStart
2018-02-09 14:47:37 +01:00
foreach ($guards as $guard) {
if ($this->auth->guard($guard)->check()) {
2018-07-09 19:24:08 +02:00
/** @noinspection PhpVoidFunctionResultUsedInspection */
2018-02-09 14:47:37 +01:00
return $this->auth->shouldUse($guard);
}
}
2018-02-09 14:47:37 +01:00
throw new AuthenticationException('Unauthenticated.', $guards);
2018-03-03 17:16:47 +01:00
// @codeCoverageIgnoreEnd
2015-02-11 07:35:10 +01:00
}
2015-02-06 04:39:52 +01:00
}