这是我的管理员中间件,在中间件中,我使用管理员保护。第一次在项目上使用Laravel角色和权限。但是,当我在中间件上编写有关角色和权限的代码时,我无法访问管理面板。
有什么错误?我现在能做什么?我的中间件代码中是否存在任何逻辑错误?
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Route;
use Closure;
use Session;
use App\Admin;
use Auth;
class CheckRole {
public function handle($request, Closure $next) {
if (Auth::guard('admin')->check() && Admin::where(['status' => '1'])) {
$adminDetails = Admin::where('email', Session::get('remember_token'))->first();
$adminDetails = json_decode(json_encode($adminDetails), true);
if ($adminDetails['type'] === "admin") {
$adminDetails['categories_access'] = 1;
$adminDetails['products_access'] = 1;
$adminDetails['orders_access'] = 1;
$adminDetails['users_access'] = 1;
}
Session::put('adminDetails', $adminDetails);
// Get Current Path
$currentPath = Route::getFacadeRoot()->current()->uri();
if ($currentPath === "admin/view-category" && Session::get('adminDetails')['categories_access'] == 0) {
return redirect('/admin/dashboard')->with('flash_message_error', 'You have no access for this module');
}
if ($currentPath === "admin/view-product" && Session::get('adminDetails')['products_access'] == 0) {
return redirect('/admin/dashboard')->with('flash_message_error', 'You have no access for this module');
}
if ($currentPath === "admin/add-product" && Session::get('adminDetails')['products_access'] == 0) {
return redirect('/admin/dashboard')->with('flash_message_error', 'You have no access for this module');
}
return $next($request);
}
return redirect('admin/login');
}
}
答案 0 :(得分:0)