bind user.current at CheckAuthenticated middleware

This commit is contained in:
printempw 2017-01-07 22:16:30 +08:00
parent fbf6b203e1
commit f5dd7c8c5f
3 changed files with 21 additions and 20 deletions

View File

@ -11,7 +11,6 @@ use Option;
use Session;
use App\Events;
use App\Models\User;
use App\Models\UserModel;
use Illuminate\Http\Request;
use App\Exceptions\PrettyPageException;
use App\Services\Repositories\UserRepository;
@ -256,7 +255,7 @@ class AuthController extends Controller
return \Response::png();
}
private function checkCaptcha($request)
protected function checkCaptcha($request)
{
return (strtolower($request->input('captcha')) == strtolower(session('phrase')));
}

View File

@ -13,10 +13,17 @@ use App\Exceptions\PrettyPageException;
class CheckAuthenticated
{
public function handle($request, \Closure $next, $return_user = false)
public function handle($request, \Closure $next, $returnUser = false)
{
if (Session::has('uid')) {
$user = app('users')->get(session('uid'));
if (!app()->bound('user.current')) {
// bind current user to container
$user = app('users')->get(session('uid'));
app()->instance('user.current', $user);
} else {
$user = app('user.current');
}
if (session('token') != $user->getToken())
return redirect('auth/login')->with('msg', trans('auth.check.token'));
@ -40,23 +47,20 @@ class CheckAuthenticated
return $next($request);
} else {
echo View::make('auth.bind')->with('msg', trans('auth.validation.email'));
return response()->view('auth.bind', ['msg' => trans('auth.bind.registered')]);
}
} else {
echo View::make('auth.bind')->with('msg', trans('auth.bind.registered'));
return response()->view('auth.bind', ['msg' => trans('auth.validation.email')]);
}
exit;
}
View::show('auth.bind');
exit;
return response()->view('auth.bind');
}
event(new UserAuthenticated($user));
if ($return_user)
return $user;
return $returnUser ? $user : $next($request);
return $next($request);
} else {
return redirect('auth/login')->with('msg', trans('auth.check.anonymous'));
}

View File

@ -103,18 +103,16 @@ class User extends Model
$user = static::firstOrNew(['email' => $email]);
// if the email is already registered
if ($user->uid)
return false;
// save to get uid
$user->save();
$user->password = static::encryptPassword($password, $user);
if ($user->uid) return false;
// pass the user instance to the callback
call_user_func($callback, $user);
// save again with password etc.
// save to get uid
$user->save();
// save again with password
$user->password = static::encryptPassword($password, $user);
$user->save();
return $user;