2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
2022-07-19 03:11:36 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class User
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-01-11 13:36:52 +08:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
2019-10-29 15:33:36 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2021-08-05 13:48:24 +08:00
|
|
|
$authorization = $request->input('auth_data') ?? $request->header('authorization');
|
2022-07-11 14:48:35 +08:00
|
|
|
if (!$authorization) abort(403, '未登录或登陆已过期');
|
|
|
|
|
|
|
|
$authData = explode(':', base64_decode($authorization));
|
2022-07-19 03:11:36 +08:00
|
|
|
if (!Cache::has($authorization)) {
|
|
|
|
if (!isset($authData[1]) || !isset($authData[0])) abort(403, '鉴权失败,请重新登入');
|
|
|
|
$user = \App\Models\User::where('password', $authData[1])
|
|
|
|
->where('email', $authData[0])
|
|
|
|
->select([
|
|
|
|
'id',
|
|
|
|
'email',
|
|
|
|
'is_admin',
|
|
|
|
'is_staff'
|
|
|
|
])
|
|
|
|
->first();
|
|
|
|
if (!$user) abort(403, '鉴权失败,请重新登入');
|
|
|
|
Cache::put($authorization, $user->toArray(), 3600);
|
|
|
|
}
|
2022-07-11 14:48:35 +08:00
|
|
|
$request->merge([
|
2022-07-19 03:11:36 +08:00
|
|
|
'user' => Cache::get($authorization)
|
2022-07-11 14:48:35 +08:00
|
|
|
]);
|
2019-10-29 15:33:36 +08:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|