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

111 lines
3.0 KiB
PHP
Raw Normal View History

<?php
/**
* Authenticate.php
2018-02-09 07:47:37 -06:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FireflyIII\Http\Middleware;
2015-02-05 21:39:52 -06:00
use Closure;
2018-02-09 07:47:37 -06:00
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as Auth;
/**
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 \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
*
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
2015-02-11 00:35:10 -06:00
/**
* Handle an incoming request.
*
2018-02-09 07:47:37 -06:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string[] ...$guards
2015-02-11 00:35:10 -06:00
*
* @return mixed
2018-02-09 07:47:37 -06:00
*
* @throws \Illuminate\Auth\AuthenticationException
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
{
2018-02-09 07:47:37 -06:00
$this->authenticate($guards);
2016-02-07 00:36:31 -06:00
2018-02-09 07:47:37 -06:00
return $next($request);
}
/**
* Determine if the user is logged in to any of the given guards.
*
* @param array $guards
*
* @return void
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function authenticate(array $guards)
{
if (empty($guards)) {
// go for default guard:
if ($this->auth->check()) {
// do an extra check on user object.
$user = $this->auth->authenticate();
if (1 === intval($user->blocked)) {
$message = strval(trans('firefly.block_account_logout'));
if ('email_changed' === $user->blocked_code) {
$message = strval(trans('firefly.email_changed_logout'));
}
app('session')->flash('logoutMessage', $message);
$this->auth->logout();
return redirect()->guest('login');
}
}
2018-02-09 07:47:37 -06:00
return $this->auth->authenticate();
}
2018-02-09 07:47:37 -06:00
foreach ($guards as $guard) {
if ($this->auth->guard($guard)->check()) {
return $this->auth->shouldUse($guard);
}
}
2018-02-09 07:47:37 -06:00
throw new AuthenticationException('Unauthenticated.', $guards);
2015-02-11 00:35:10 -06:00
}
2015-02-05 21:39:52 -06:00
}