From c9b39327a5a5e42a5777704997d2f4b792f56b82 Mon Sep 17 00:00:00 2001 From: wuyanwen Date: Tue, 7 Jan 2020 17:27:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9auth=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 | 46 +++-- catch/login/request/LoginRequest.php | 1 + catch/user/Auth.php | 7 - catch/user/controller/User.php | 26 ++- extend/catcher/CatchAuth.php | 175 ++++++++++++++++++ extend/catcher/base/CatchRequest.php | 46 +++++ extend/catcher/base/CatchValidate.php | 41 +--- .../exceptions/LoginFailedException.php | 4 +- 8 files changed, 280 insertions(+), 66 deletions(-) create mode 100644 extend/catcher/CatchAuth.php diff --git a/catch/login/controller/Index.php b/catch/login/controller/Index.php index 3f30dd4..9f2e235 100644 --- a/catch/login/controller/Index.php +++ b/catch/login/controller/Index.php @@ -1,11 +1,13 @@ fetch(); } - /** - * 登陆 - * - * @time 2019年11月28日 - * @param LoginRequest $request - * @return bool|string - * @throws \catcher\exceptions\LoginFailedException - * @throws \cather\exceptions\LoginFailedException - * @throws LoginFailedException - */ - public function login(LoginRequest $request) + /** + * 登陆 + * + * @time 2019年11月28日 + * @param LoginRequest $request + * @param CatchAuth $auth + * @return bool|string + */ + public function login(LoginRequest $request, CatchAuth $auth) { $params = $request->param(); - $token = Auth::login($params); + + $token = $auth->attempt($params); + + $user = $auth->user(); + + if ($user->status == Users::DISABLE) { + throw new LoginFailedException('该用户已被禁用'); + } + + // 记录用户登录 + $user->last_login_ip = request()->ip(); + $user->last_login_time = time(); + $user->save(); + // 登录事件 $params['success'] = $token; + event('loginLog', $params); return $token ? CatchResponse::success([ 'token' => $token, - ], '登录成功') : - - CatchResponse::success('', '登录失败'); + ], '登录成功') : CatchResponse::success('', '登录失败'); } /** @@ -74,4 +86,4 @@ class Index extends CatchController { return $captcha->create($config); } -} \ No newline at end of file +} diff --git a/catch/login/request/LoginRequest.php b/catch/login/request/LoginRequest.php index a8b709d..772cabe 100644 --- a/catch/login/request/LoginRequest.php +++ b/catch/login/request/LoginRequest.php @@ -19,5 +19,6 @@ class LoginRequest extends CatchRequest { // TODO: Implement message() method. return []; + } } diff --git a/catch/user/Auth.php b/catch/user/Auth.php index 4e3778c..0a9740a 100644 --- a/catch/user/Auth.php +++ b/catch/user/Auth.php @@ -35,14 +35,7 @@ class Auth throw new LoginFailedException('登陆失败, 请检查用户名和密码'); } - if ($user->status == Users::DISABLE) { - throw new LoginFailedException('该用户已被禁用'); - } - // 记录用户登录 - $user->last_login_ip = request()->ip(); - $user->last_login_time = time(); - $user->save(); // Session::set(self::getLoginUserKey(), $user); diff --git a/catch/user/controller/User.php b/catch/user/controller/User.php index 7ad08a6..a800baa 100644 --- a/catch/user/controller/User.php +++ b/catch/user/controller/User.php @@ -2,12 +2,14 @@ namespace catchAdmin\user\controller; use app\Request; +use catchAdmin\permissions\model\Permissions; use catchAdmin\permissions\model\Roles; use catchAdmin\user\Auth; use catchAdmin\user\model\Users; use catchAdmin\user\request\CreateRequest; use catchAdmin\user\request\UpdateRequest; use catcher\base\CatchController; +use catcher\CatchAuth; use catcher\CatchResponse; use catcher\Tree; use catcher\Utils; @@ -33,9 +35,29 @@ class User extends CatchController return CatchResponse::paginate($this->user->getList($request->param())); } - public function info() + /** + * 获取用户信息 + * + * @time 2020年01月07日 + * @param CatchAuth $auth + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return \think\response\Json + */ + public function info(CatchAuth $auth) { - return CatchResponse::success(Auth::getUserInfo()); + $user = $auth->user(); + + $roles = $user->getRoles(); + + $user->permissions = Permissions::whereIn('id', $user->getPermissionsBy()) + ->field(['permission_name as title', 'route', 'icon']) + ->select(); + + $user->roles = $roles; + + return CatchResponse::success($user); } /** diff --git a/extend/catcher/CatchAuth.php b/extend/catcher/CatchAuth.php new file mode 100644 index 0000000..11cfb7e --- /dev/null +++ b/extend/catcher/CatchAuth.php @@ -0,0 +1,175 @@ +auth = config('catch.auth'); + + $this->guard = $this->auth['default']['guard']; + } + + /** + * set guard + * + * @time 2020年01月07日 + * @param $guard + * @return $this + */ + public function guard($guard): self + { + $this->guard = $guard; + + return $this; + } + + public function attempt($condition) + { + $user = $this->authenticate($condition); + + if (!$user) { + throw new LoginFailedException(); + } + + if (!password_verify($condition['password'], $user->password)) { + throw new LoginFailedException(); + } + + return $this->{$this->getDriver()}($user); + } + + + public function user() + { + switch ($this->getDriver()) { + case 'jwt': + $model = app($this->getProvider()['model']); + return $model->where($model->getPk(), JWTAuth::auth()['id'])->find(); + case 'session': + return Session::get($this->sessionUserKey(), null); + default: + throw new FailedException('user not found'); + } + } + + public function logout() + { + + } + + protected function jwt($user) + { + return JWTAuth::builder(['id' => $user->id]); + } + + + protected function session($user) + { + Session::set($this->sessionUserKey(), $user); + } + + + protected function sessionUserKey() + { + return $this->guard . '_user'; + } + + protected function getDriver() + { + return $this->auth['guards'][$this->guard]['driver']; + } + + + protected function getProvider() + { + return $this->auth['providers'][$this->auth['guards'][$this->guard]['provider']]; + } + + + protected function authenticate($condition) + { + $provider = $this->getProvider(); + + return $this->{$provider['driver']}($condition); + } + + /** + * + * @time 2020年01月07日 + * @param $condition + * @return void + */ + protected function database($condition): void + {} + + /** + * + * @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 + */ + protected function filter($condition): array + { + $where = []; + + foreach ($condition as $field => $value) { + if ($field != $this->password) { + $where[$field] = $value; + } + } + + return $where; + } + + /** + * + * @time 2020年01月07日 + * @param $field + * @return $this + */ + public function username($field): self + { + $this->username = $field; + + return $this; + } + + /** + * + * @time 2020年01月07日 + * @param $field + * @return $this + */ + public function password($field): self + { + $this->password = $field; + + return $this; + } +} diff --git a/extend/catcher/base/CatchRequest.php b/extend/catcher/base/CatchRequest.php index e1a0fca..d88b31c 100644 --- a/extend/catcher/base/CatchRequest.php +++ b/extend/catcher/base/CatchRequest.php @@ -8,6 +8,14 @@ use think\Validate; class CatchRequest extends Request { + /** + * 批量验证 + * + * @var bool + */ + protected $batch = false; + + /** * Request constructor. * @throws \Exception @@ -30,7 +38,45 @@ class CatchRequest extends Request { try { $validate = new Validate(); + // 批量验证 + if ($this->batch) { + $validate->batch($this->batch); + } + // 自定义规则 + if (method_exists($this, 'newRules')) { + foreach ($this->newRules() as $rule) { + $validate->extend($rule->type(), [$rule, 'verify'], $rule->message()); + } + } + + /** + // 场景设置验证 + if (property_exists($this, 'scene') && !empty($this->scene)) { + foreach ($this->scene as $scene => $rules) { + $validate->scene($scene); + // 只限制字段 + if (!isset($rules['only'])) { + $validate->only($rules); + } else { + $validate->only($rules['only']); + // 新增规则 + if (isset($rules['append'])) { + foreach ($rules['append'] as $field => $rule) { + $validate->append($field, $rule); + } + } + // 移除规则 + if (isset($rules['remove'])) { + foreach ($rules['remove'] as $field => $rule) { + $validate->remove($field, $rule); + } + } + } + } + }**/ + + // 验证 if (!$validate->check(request()->param(), $this->rules())) { throw new FailedException($validate->getError()); } diff --git a/extend/catcher/base/CatchValidate.php b/extend/catcher/base/CatchValidate.php index ede44a4..69a257a 100644 --- a/extend/catcher/base/CatchValidate.php +++ b/extend/catcher/base/CatchValidate.php @@ -1,46 +1,9 @@ register(); - - $this->rule = $this->getRules(); - } - - - abstract protected function getRules(): array ; - - /** - * - * @time 2019年12月07日 - * @return void - */ - private function register(): void - { - if (!empty($this->newValidates())) { - foreach ($this->newValidates() as $validate) { - $this->extend($validate->type(), [$validate, 'verify'], $validate->message()); - } - } - } - - /** - * - * @time 2019年12月07日 - * @return array - */ - private function newValidates(): array - { - return [ - new Sometimes(), - ]; - } + } diff --git a/extend/catcher/exceptions/LoginFailedException.php b/extend/catcher/exceptions/LoginFailedException.php index fd79521..6d4a145 100644 --- a/extend/catcher/exceptions/LoginFailedException.php +++ b/extend/catcher/exceptions/LoginFailedException.php @@ -6,4 +6,6 @@ use catcher\Code; class LoginFailedException extends CatchException { protected $code = Code::LOGIN_FAILED; -} \ No newline at end of file + + protected $message = 'Login Failed! Please check you email or password'; +}