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;
|
2018-07-20 14:42:43 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
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
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
public function handle($request, Closure $next)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
if (Auth::check()) {
|
2017-01-07 22:16:30 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2016-07-23 14:23:11 +08:00
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
if ($user->permission == User::BANNED) {
|
|
|
|
Auth::logout();
|
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));
|
|
|
|
|
2018-07-20 14:42:43 +08:00
|
|
|
return $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
|
|
|
}
|
|
|
|
}
|
2017-01-08 14:15:55 +08:00
|
|
|
|
|
|
|
public function askForFillingEmail($request, Closure $next)
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$user = Auth::user();
|
2017-01-22 18:39:14 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
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
|
|
|
}
|