blessing-skin-server/app/Http/Middleware/CheckAuthenticated.php

64 lines
1.9 KiB
PHP
Raw Normal View History

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;
2016-09-15 09:20:02 +08:00
use App\Models\User;
use App\Exceptions\PrettyPageException;
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
{
2016-08-28 10:05:21 +08:00
public function handle($request, \Closure $next, $return_user = false)
2016-07-21 22:01:57 +08:00
{
2016-08-28 20:33:35 +08:00
if (Session::has('uid')) {
2016-10-25 21:35:09 +08:00
$user = app('users')->get(session('uid'));
2016-07-23 14:23:11 +08:00
2016-08-28 10:05:21 +08:00
if (session('token') != $user->getToken())
2016-09-15 09:20:02 +08:00
return redirect('auth/login')->with('msg', trans('auth.check.token'));
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
2016-09-15 09:20:02 +08:00
throw new PrettyPageException(trans('auth.check.banned'), 5);
2016-07-23 14:23:11 +08:00
}
2016-08-19 23:09:32 +08:00
// ask for filling email
if ($user->email == "") {
if (isset($request->email)) {
if (filter_var($request->email, FILTER_VALIDATE_EMAIL)) {
2016-10-23 11:41:52 +08:00
if (User::where('email', $request->email)->get()->isEmpty()) {
$user->setEmail($request->email);
2016-08-19 23:09:32 +08:00
// refresh token
2016-10-23 11:41:52 +08:00
Session::put('token', $user->getToken(true));
Cookie::queue('token', $user->getToken(), 60);
2016-08-29 23:08:09 +08:00
2016-09-11 15:39:55 +08:00
return $next($request);
2016-08-19 23:09:32 +08:00
} else {
2016-09-15 09:20:02 +08:00
echo View::make('auth.bind')->with('msg', trans('auth.validation.email'));
2016-08-19 23:09:32 +08:00
}
} else {
2016-09-15 09:20:02 +08:00
echo View::make('auth.bind')->with('msg', trans('auth.bind.registered'));
2016-08-19 23:09:32 +08:00
}
2016-09-11 15:39:55 +08:00
exit;
2016-08-19 23:09:32 +08:00
}
2016-09-11 15:39:55 +08:00
View::show('auth.bind');
exit;
2016-08-19 23:09:32 +08:00
}
2016-08-28 10:05:21 +08:00
if ($return_user)
return $user;
return $next($request);
2016-07-21 22:01:57 +08:00
} else {
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
}
}