修改auth认证

This commit is contained in:
wuyanwen 2020-01-07 17:27:55 +08:00
parent f7ac1a23bf
commit c9b39327a5
8 changed files with 280 additions and 66 deletions

View File

@ -1,11 +1,13 @@
<?php
namespace catchAdmin\login\controller;
use app\exceptions\LoginFailedException;
use catchAdmin\user\Auth;
use catchAdmin\login\request\LoginRequest;
use catchAdmin\user\model\Users;
use catcher\base\CatchController;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\exceptions\LoginFailedException;
use think\captcha\Captcha;
class Index extends CatchController
@ -22,29 +24,39 @@ class Index extends CatchController
return $this->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);
}
}
}

View File

@ -19,5 +19,6 @@ class LoginRequest extends CatchRequest
{
// TODO: Implement message() method.
return [];
}
}

View File

@ -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);

View File

@ -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);
}
/**

View File

@ -0,0 +1,175 @@
<?php
namespace catcher;
use catcher\exceptions\FailedException;
use catcher\exceptions\LoginFailedException;
use thans\jwt\facade\JWTAuth;
use think\facade\Session;
class CatchAuth
{
protected $auth;
protected $guard;
// 默认获取
protected $username = 'email';
// 校验字段
protected $password = 'password';
public function __construct()
{
$this->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;
}
}

View File

@ -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());
}

View File

@ -1,46 +1,9 @@
<?php
namespace catcher\base;
use catcher\validates\Sometimes;
use think\Validate;
abstract class CatchValidate extends Validate
class CatchValidate extends Validate
{
public function __construct()
{
parent::__construct();
$this->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(),
];
}
}

View File

@ -6,4 +6,6 @@ use catcher\Code;
class LoginFailedException extends CatchException
{
protected $code = Code::LOGIN_FAILED;
}
protected $message = 'Login Failed! Please check you email or password';
}