140 lines
5.2 KiB
PHP
140 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\AdminLog;
|
|
use App\Models\Rbac\Permission;
|
|
use Closure;
|
|
use App\Models\Matchmaker;
|
|
use App\Models\PaasWorker;
|
|
use App\Models\Admin;
|
|
use App\Models\Paas;
|
|
use App\Services\UserService;
|
|
use App\Models\MatchmakerClient;
|
|
class CheckAdmin
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Closure $next
|
|
* @return mixed
|
|
*/
|
|
public function handle($request, Closure $next)
|
|
{
|
|
$user = auth()->user();
|
|
if (empty($user)) {
|
|
$user = $this->authCheck();
|
|
}
|
|
if (empty($user)) {
|
|
return $this->failure('请登录后访问');
|
|
}
|
|
//是不是红娘
|
|
$maker = Matchmaker::where('user_id', $user->id)->where('status', 1)->count();
|
|
$admin_type = '';
|
|
if ($maker) {
|
|
$admin_type = 'matcher';
|
|
}
|
|
//是不是同工
|
|
// $worker = PaasWorker::where('user_id', $user->id)->count();
|
|
// if ($worker) {
|
|
// $admin_type = 'worker';
|
|
// }
|
|
if (in_array($user->mobile, ["18714411592","13643855391"])) {
|
|
$admin_type = 'card_admin';
|
|
}
|
|
//是否是平台管理员
|
|
$admin = Admin::where('user_id', $user->id)->pluck('type')->toArray();
|
|
if (count($admin) && !in_array('SUPER', $admin)) {
|
|
$admin_type = 'paas_admin';
|
|
$paas_obj = Paas::where('name', $admin[0])->first();
|
|
$paas = $request->session()->put('paas_obj', $paas_obj);
|
|
}
|
|
if (count($admin) && in_array('SUPER', $admin)) {
|
|
$admin_type = 'admin';
|
|
}
|
|
if ($user->is_admin) {
|
|
$admin_type = 'admin';
|
|
}
|
|
if (empty($admin_type)) {
|
|
return $this->failure('你还不是管理员');
|
|
}
|
|
//储存类型
|
|
$request->session()->put('admin_type', $admin_type);
|
|
|
|
$method = $request->method();
|
|
$path = $request->path();
|
|
if($method != 'GET' && config('app.env') == 'production'){
|
|
$admin_log = new AdminLog();
|
|
$admin_log->path = $path;
|
|
$admin_log->method = $method;
|
|
$admin_log->user_id = $user->id;
|
|
$admin_log->param = json_encode($request->all());
|
|
$admin_log->save();
|
|
}
|
|
// $route_name = $request->route()->getName();
|
|
// if ($route_name == 'user') {
|
|
// $user_id = $request->user_id;
|
|
// if ($admin_type == 'paas_admin') {
|
|
// $user_ids = MatchmakerClient::where('user_id', auth()->id())->pluck('client_user_id')->toArray();
|
|
// $userService = new UserService;
|
|
// $paas_user_ids = $userService->paasUserIds($paas_obj->name, 'MAIN')->toArray();
|
|
// $user_ids = array_merge($user_ids, $paas_user_ids);
|
|
// if (!in_array($user_id, $user_ids)) {
|
|
// return $this->failure('没有权限访问');
|
|
// }
|
|
// }elseif ($admin_type == 'worker' || $admin_type == 'matcher') {
|
|
// $user_ids = MatchmakerClient::where('user_id', auth()->id())->pluck('client_user_id')->toArray();
|
|
// if (!in_array($user_id, $user_ids)) {
|
|
// return $this->failure('没有权限访问');
|
|
// }
|
|
// }
|
|
// }
|
|
// if($user->id == 49408){
|
|
// $admin = Admin::with('permission')->where('user_id', $user->id)->first();
|
|
// $permission_arr = [];
|
|
// if(!empty($admin->permission)){
|
|
// foreach ($admin->permission as $permission){
|
|
// $permission_arr[] = $permission->name;
|
|
// }
|
|
// }
|
|
//
|
|
// if(empty($permission_arr)){
|
|
// return $this->failure('没有权限访问');
|
|
// }
|
|
// $route = $request->path();
|
|
// $method = $request->method();
|
|
// $route_method_permission = Permission::where('http_path', $route)->where('http_method', $method)->pluck('name')->toArray();
|
|
// $route_permission = Permission::where('http_path', $route)->where('http_method', 'ANY')->pluck('name')->toArray();
|
|
// $method_permission = Permission::where('http_path', '*')->where('http_method', $method)->pluck('name')->toArray();
|
|
// $all_permission = Permission::where('http_path', "*")->where('http_method', "ANY")->pluck('name')->toArray();
|
|
//
|
|
// if(!array_intersect($permission_arr, $route_method_permission) && !array_intersect($permission_arr, $route_permission) && !array_intersect($permission_arr, $method_permission) && !array_intersect($permission_arr, $all_permission)){
|
|
// return $this->failure('没有权限访问');
|
|
// }
|
|
// }
|
|
return $next($request);
|
|
}
|
|
|
|
public function failure($msg)
|
|
{
|
|
$result = [
|
|
'code'=> 1,
|
|
'message'=> $msg,
|
|
];
|
|
return Response()->json($result);
|
|
}
|
|
|
|
public function authCheck(){
|
|
$guards = config('auth.guards');
|
|
$result = false;
|
|
foreach($guards as $key=>$guard){
|
|
if(\Auth::guard($key)->check()){
|
|
$result = \Auth::guard($key)->user();
|
|
continue;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
}
|