2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Middleware;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-10-23 11:41:52 +08:00
|
|
|
use App;
|
2016-08-19 23:09:32 +08:00
|
|
|
use View;
|
|
|
|
use Http;
|
2016-11-05 20:11:31 +08:00
|
|
|
use Cookie;
|
2016-08-28 10:05:21 +08:00
|
|
|
use Session;
|
2017-01-08 14:15:55 +08:00
|
|
|
use Closure;
|
2016-09-15 09:20:02 +08:00
|
|
|
use App\Models\User;
|
2016-11-17 17:32:12 +08:00
|
|
|
use App\Events\UserAuthenticated;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
class CheckAuthenticated
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2017-01-08 14:15:55 +08:00
|
|
|
public function handle($request, Closure $next, $returnUser = false)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-28 20:33:35 +08:00
|
|
|
if (Session::has('uid')) {
|
2017-01-07 22:16:30 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! app()->bound('user.current')) {
|
|
|
|
// Bind current user to container
|
2017-01-07 22:16:30 +08:00
|
|
|
$user = app('users')->get(session('uid'));
|
|
|
|
app()->instance('user.current', $user);
|
|
|
|
} else {
|
|
|
|
$user = app('user.current');
|
|
|
|
}
|
2016-07-23 14:23:11 +08:00
|
|
|
|
2017-06-28 20:42:51 +08:00
|
|
|
if (session('token') != $user->getToken()) {
|
|
|
|
$this->flashLastRequestedPath();
|
2016-09-15 09:20:02 +08:00
|
|
|
return redirect('auth/login')->with('msg', trans('auth.check.token'));
|
2017-06-28 20:42:51 +08:00
|
|
|
}
|
2016-07-22 19:36:24 +08:00
|
|
|
|
2016-07-23 14:23:11 +08:00
|
|
|
if ($user->getPermission() == "-1") {
|
2016-10-23 11:41:52 +08:00
|
|
|
delete_sessions();
|
|
|
|
delete_cookies();
|
2016-07-23 14:23:11 +08:00
|
|
|
|
2017-01-08 14:15:55 +08:00
|
|
|
abort(403, trans('auth.check.banned'));
|
2016-07-23 14:23:11 +08:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// Ask for filling email
|
2016-08-19 23:09:32 +08:00
|
|
|
if ($user->email == "") {
|
2017-01-08 14:15:55 +08:00
|
|
|
return $this->askForFillingEmail($request, $next);
|
2016-08-19 23:09:32 +08:00
|
|
|
}
|
|
|
|
|
2016-11-19 22:02:02 +08:00
|
|
|
event(new UserAuthenticated($user));
|
|
|
|
|
2017-01-07 22:16:30 +08:00
|
|
|
return $returnUser ? $user : $next($request);
|
2016-08-28 10:05:21 +08:00
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
} else {
|
2017-06-28 20:42:51 +08:00
|
|
|
$this->flashLastRequestedPath();
|
|
|
|
|
2016-09-15 09:20:02 +08:00
|
|
|
return redirect('auth/login')->with('msg', trans('auth.check.anonymous'));
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2016-08-28 10:05:21 +08:00
|
|
|
|
|
|
|
return $next($request);
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
2017-01-08 14:15:55 +08:00
|
|
|
|
|
|
|
public function askForFillingEmail($request, Closure $next)
|
|
|
|
{
|
2017-01-22 18:39:14 +08:00
|
|
|
$user = app('user.current');
|
|
|
|
|
2017-01-08 14:15:55 +08:00
|
|
|
if (isset($request->email)) {
|
|
|
|
if (filter_var($request->email, FILTER_VALIDATE_EMAIL)) {
|
2017-01-22 18:39:14 +08:00
|
|
|
|
2017-01-08 14:15:55 +08:00
|
|
|
if (User::where('email', $request->email)->get()->isEmpty()) {
|
|
|
|
$user->setEmail($request->email);
|
2018-02-16 17:31:04 +08:00
|
|
|
// Refresh token
|
2017-01-08 14:15:55 +08:00
|
|
|
Session::put('token', $user->getToken(true));
|
|
|
|
Cookie::queue('token', $user->getToken(), 60);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
} else {
|
|
|
|
return response()->view('auth.bind', ['msg' => trans('auth.bind.registered')]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return response()->view('auth.bind', ['msg' => trans('auth.validation.email')]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->view('auth.bind');
|
|
|
|
}
|
2017-06-28 20:42:51 +08:00
|
|
|
|
|
|
|
protected function flashLastRequestedPath($path = null)
|
|
|
|
{
|
|
|
|
$path = $path ?: app('request')->path();
|
2018-02-16 17:31:04 +08:00
|
|
|
|
2017-06-28 20:42:51 +08:00
|
|
|
return session(['last_requested_path' => $path]);
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|