From 4506219fb237cb4901a8ade4fe2fe9042f817ee2 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 26 Oct 2020 19:09:28 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96auth=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/login/controller/Index.php | 98 +++++++++--- extend/catcher/CatchAuth.php | 251 +++++++++++++------------------ 2 files changed, 186 insertions(+), 163 deletions(-) diff --git a/catch/login/controller/Index.php b/catch/login/controller/Index.php index 593e0e3..214b89d 100644 --- a/catch/login/controller/Index.php +++ b/catch/login/controller/Index.php @@ -12,33 +12,95 @@ use thans\jwt\facade\JWTAuth; class Index extends CatchController { - /** - * 登陆 - * - * @time 2019年11月28日 - * @param LoginRequest $request - * @param CatchAuth $auth - * @return bool|string - */ + /** + * 登陆 + * + * @time 2019年11月28日 + * @param LoginRequest $request + * @param CatchAuth $auth + * @return bool|string + */ public function login(LoginRequest $request, CatchAuth $auth) { + $condition = $request->param(); + try { + $token = $auth->attempt($condition); + + $user = $auth->user(); + + $this->afterLoginSuccess($user); + // 登录事件 + $this->loginEvent($user->username); + return CatchResponse::success([ - 'token' => $auth->attempt($request->param()), + 'token' => $token, ], '登录成功'); } catch (\Exception $exception) { - $code = $exception->getCode(); - return CatchResponse::fail($code == Code::USER_FORBIDDEN ? - '该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED); + $this->detailWithLoginFailed($exception, $condition); + $code = $exception->getCode(); + return CatchResponse::fail($code == Code::USER_FORBIDDEN ? + '该账户已被禁用,请联系管理员' : '登录失败,请检查邮箱和密码', Code::LOGIN_FAILED); } } - /** - * 登出 - * - * @time 2019年11月28日 - * @return \think\response\Json - */ + /** + * 处理登录失败 + * + * @time 2020年10月26日 + * @param $exception + * @param $condition + * @return void + */ + protected function detailWithLoginFailed($exception, $condition) + { + $message = $exception->getMessage(); + + if (strpos($message, '|') !== false) { + $username = explode('|', $message)[1]; + } else { + $username = $condition['email']; + } + + $this->loginEvent($username, false); + } + + /** + * 用户登录成功后 + * + * @time 2020年09月09日 + * @param $user + * @return void + */ + protected function afterLoginSuccess($user) + { + $user->last_login_ip = request()->ip(); + $user->last_login_time = time(); + $user->save(); + } + + /** + * 登录事件 + * + * @time 2020年09月09日 + * @param $name + * @param bool $success + * @return void + */ + protected function loginEvent($name, $success = true) + { + $params['login_name'] = $name; + $params['success'] = $success ? 1 : 2; + event('loginLog', $params); + } + + + /** + * 登出 + * + * @time 2019年11月28日 + * @return \think\response\Json + */ public function logout(): \think\response\Json { return CatchResponse::success(); diff --git a/extend/catcher/CatchAuth.php b/extend/catcher/CatchAuth.php index 799b1de..0af992f 100644 --- a/extend/catcher/CatchAuth.php +++ b/extend/catcher/CatchAuth.php @@ -29,31 +29,30 @@ class CatchAuth $this->guard = $this->auth['default']['guard']; } - /** - * set guard - * - * @time 2020年01月07日 - * @param $guard - * @return $this - */ + /** + * set guard + * + * @time 2020年01月07日 + * @param $guard + * @return $this + */ public function guard($guard) { - $this->guard = $guard; + $this->guard = $guard; - return $this; + return $this; } - /** - * - * @time 2020年01月07日 - * @param $condition - * @return mixed - */ + /** + * + * @time 2020年01月07日 + * @param $condition + * @return mixed + */ public function attempt($condition) { try { $user = $this->authenticate($condition); - if (!$user) { throw new LoginFailedException(); } @@ -65,51 +64,13 @@ class CatchAuth throw new LoginFailedException('登录失败|' . $user->username); } - $token = $this->{$this->getDriver()}($user); - $this->afterLoginSuccess($user); - // 登录事件 - $this->loginEvent($user->username); - return $token; + return $this->{$this->getDriver()}($user); + } catch (\Exception $exception) { - $message = $exception->getMessage(); - if (strpos($message, '|') !== false) { - $username = explode('|', $message)[1]; - } else { - $username = $condition['email']; - } - $this->loginEvent($username, false); - throw new LoginFailedException('登录失败', $exception->getCode()); + // } } - /** - * 用户登录成功后 - * - * @time 2020年09月09日 - * @param $user - * @return void - */ - protected function afterLoginSuccess($user) - { - $user->last_login_ip = request()->ip(); - $user->last_login_time = time(); - $user->save(); - } - - /** - * 登录事件 - * - * @time 2020年09月09日 - * @param $name - * @param bool $success - * @return void - */ - protected function loginEvent($name, $success = true) - { - $params['login_name'] = $name; - $params['success'] = $success ? 1 : 2; - event('loginLog', $params); - } /** * user @@ -142,30 +103,30 @@ class CatchAuth return $user; } - /** - * - * @time 2020年01月07日 - * @return mixed - */ + /** + * + * @time 2020年01月07日 + * @return mixed + */ public function logout() { - switch ($this->getDriver()) { - case 'jwt': - return true; - case 'session': - Session::delete($this->sessionUserKey()); - return true; - default: - throw new FailedException('user not found'); - } + switch ($this->getDriver()) { + case 'jwt': + return true; + case 'session': + Session::delete($this->sessionUserKey()); + return true; + default: + throw new FailedException('user not found'); + } } - /** - * - * @time 2020年01月07日 - * @param $user - * @return string - */ + /** + * + * @time 2020年01月07日 + * @param $user + * @return string + */ protected function jwt($user) { $token = JWTAuth::builder([$this->jwtKey() => $user->id]); @@ -175,52 +136,52 @@ class CatchAuth return $token; } - /** - * - * @time 2020年01月07日 - * @param $user - * @return void - */ + /** + * + * @time 2020年01月07日 + * @param $user + * @return void + */ protected function session($user) { Session::set($this->sessionUserKey(), $user); } - /** - * - * @time 2020年01月07日 - * @return string - */ + /** + * + * @time 2020年01月07日 + * @return string + */ protected function sessionUserKey() { - return $this->guard . '_user'; + return $this->guard . '_user'; } - /** - * - * @time 2020年01月07日 - * @return string - */ + /** + * + * @time 2020年01月07日 + * @return string + */ protected function jwtKey() { - return $this->guard . '_id'; + return $this->guard . '_id'; } - /** - * - * @time 2020年01月07日 - * @return mixed - */ + /** + * + * @time 2020年01月07日 + * @return mixed + */ protected function getDriver() { - return $this->auth['guards'][$this->guard]['driver']; + return $this->auth['guards'][$this->guard]['driver']; } - /** - * - * @time 2020年01月07日 - * @return mixed - */ + /** + * + * @time 2020年01月07日 + * @return mixed + */ protected function getProvider() { if (!isset($this->auth['guards'][$this->guard])) { @@ -230,12 +191,12 @@ class CatchAuth return $this->auth['providers'][$this->auth['guards'][$this->guard]['provider']]; } - /** - * - * @time 2020年01月07日 - * @param $condition - * @return mixed - */ + /** + * + * @time 2020年01月07日 + * @param $condition + * @return mixed + */ protected function authenticate($condition) { $provider = $this->getProvider(); @@ -243,51 +204,51 @@ class CatchAuth return $this->{$provider['driver']}($condition); } - /** - * - * @time 2020年01月07日 - * @param $condition - * @return void - */ + /** + * + * @time 2020年01月07日 + * @param $condition + * @return void + */ protected function database($condition): void {} - /** - * - * @time 2020年01月07日 - * @param $condition - * @return mixed - */ + /** + * + * @time 2020年01月07日 + * @param $condition + * @return mixed + */ protected function orm($condition) { return app($this->getProvider()['model'])->where($this->filter($condition))->find(); } - /** - * - * @time 2020年01月07日 - * @param $condition - * @return array - */ + /** + * + * @time 2020年01月07日 + * @param $condition + * @return array + */ protected function filter($condition): array { $where = []; foreach ($condition as $field => $value) { - if ($field != $this->password) { - $where[$field] = $value; - } + if ($field != $this->password) { + $where[$field] = $value; + } } return $where; } - /** - * - * @time 2020年01月07日 - * @param $field - * @return $this - */ + /** + * + * @time 2020年01月07日 + * @param $field + * @return $this + */ public function username($field): self { $this->username = $field; @@ -295,12 +256,12 @@ class CatchAuth return $this; } - /** - * - * @time 2020年01月07日 - * @param $field - * @return $this - */ + /** + * + * @time 2020年01月07日 + * @param $field + * @return $this + */ public function password($field): self { $this->password = $field;