记一次神奇的 bug,无限 302 重定向

一切按照教程走,居然出现了无限302,user和admin两个地址互相重定向的问题。于是各种检查问题,甚至复制粘贴情况仍旧出现。
后来追到源码中发现。
Frozennode\Administrator\Http\Middleware

public function handle($request, Closure $next)
    {
        $config = app('itemconfig');

        //if the model doesn't exist at all, redirect to 404
        if (!$config) {
            abort(404, 'Page not found');
        }

        //check the permission
        $p = $config->getOption('permission');

        //if the user is simply not allowed permission to this model, redirect them to the dashboard
        if (!$p) {
            return redirect()->route('admin_dashboard');
        }
      /**这里的$p是子菜单users的权限**/
        //get the settings data if it's a settings page
        if ($config->getType() === 'settings') {
            $config->fetchData(app('admin_field_factory')->getEditFields());
        }

        //otherwise if this is a response, return that
        if (is_a($p, 'Illuminate\Http\JsonResponse') || is_a($p, 'Illuminate\Http\Response') || is_a($p, 'Illuminate\\Http\\RedirectResponse')) {
            return $p;
        }

        return $next($request);
    }

最终发现是,manage_users少了一个s,于是造成了用户有后台的进入权限,但是没有users菜单的权限。
但是进入后台的首页被重定向到了users页面,而users页面在没有权限的时候,根据return redirect()->route('admin_dashboard');可见被重定向到了菜单主页,也就是http://larabbs.test/admin,自此循环重定向构成。

所以能后台管理的用户一定也要有默认菜单的访问权限呀。(手动掩面)

Nickel
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 1
Apress

我也遇到了,只有权限最高的角色能够登陆。
我觉得是权限判断的时候出了问题,然后我做了一下修改

因为可能是由于权限太高的问题导致,在 config/administrator 中全局搜索 ‘premiss’,所以把 config/administrator/user.php 中 的权限验证 修改:

// 设置当前页面的访问权限,通过返回布尔值来控制权限。
// 返回 True 即通过权限验证,False 则无权访问并从 Menu 中隐藏
'permission'=> function()
{
return Auth::user()->can('manage_users');
},
把修改 ‘manage_users’ 修改为 ‘manage_contents' 即可

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!