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

79 lines
1.7 KiB
PHP
Raw Normal View History

2015-02-05 21:52:16 -06:00
<?php namespace FireflyIII\Http\Middleware;
2015-02-05 21:39:52 -06:00
use App;
2015-12-18 09:38:50 -06:00
use Auth;
2015-06-03 14:25:11 -05:00
use Carbon\Carbon;
2015-02-05 21:39:52 -06:00
use Closure;
2015-12-28 00:39:48 -06:00
use FireflyIII\User;
2015-02-05 21:39:52 -06:00
use Illuminate\Contracts\Auth\Guard;
2015-03-29 00:43:20 -05:00
use Illuminate\Http\Request;
use Preferences;
2015-12-18 09:38:50 -06:00
2015-02-11 00:35:10 -06:00
/**
* Class Authenticate
*
2015-05-10 01:26:42 -05:00
* @codeCoverageIgnore
2015-02-11 00:35:10 -06:00
* @package FireflyIII\Http\Middleware
*/
class Authenticate
{
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
* Create a new filter instance.
*
* @param Guard $auth
*
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
2015-03-29 00:43:20 -05:00
public function handle(Request $request, Closure $next)
2015-02-11 00:35:10 -06:00
{
2015-12-28 00:39:48 -06:00
2015-02-11 00:35:10 -06:00
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('auth/login');
}
}
2016-01-01 14:49:27 -06:00
/** @var User $user */
$user = $this->auth->user();
if ($user instanceof User && intval($user->blocked) == 1) {
2015-11-01 01:03:41 -06:00
Auth::logout();
2015-12-18 09:38:50 -06:00
2015-11-01 01:03:41 -06:00
return redirect()->route('index');
}
// if logged in, set user language:
2015-12-28 00:12:47 -06:00
$pref = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'));
App::setLocale($pref->data);
2015-12-28 00:12:47 -06:00
Carbon::setLocale(substr($pref->data, 0, 2));
2015-12-24 01:35:08 -06:00
$locale = explode(',', trans('config.locale'));
$locale = array_map('trim', $locale);
2015-12-24 01:35:08 -06:00
setlocale(LC_TIME, $locale);
setlocale(LC_MONETARY, $locale);
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
return $next($request);
}
2015-02-05 21:39:52 -06:00
}