dongse7261 2016-08-09 14:44
浏览 54
已采纳

中间件会影响数据库中插入的绑定

I have a problem with my laravel app.

I want to add middleware for rank control on route.

When i add my custom middelware to my route group bindings of SQL statement are in double.

When i take off my custom middleware it's work fine.

I really don't understand why.

There is my AuthRank.php :

class AuthRank {

public function handle($request, Closure $next, $rank)
{
    $response = $next($request);

    $user = Auth::user();

    if(!$user || $user->rank < $rank)
    {
          return redirect('/');
    }

    return $next($request);
}}

There is my Kernel.php :

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Adldap\Laravel\Middleware\WindowsAuthenticate::class, // Inserted here.
    ],
    'api' => [
        'throttle:60,1',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'auth.rank' => \App\Http\Middleware\AuthRank::class, 
];

And there is an extract of my Routes.php :

 Route::group(['prefix' => 'GestionUtilisateurs','middleware' =>'auth.rank:99'], function () 
{
    Route::post('/copy', ['uses' => 'userController@copyUser']);
});

The results of problem with custom middleware:

Statement : insert into MY_TABLE (DATE, USER_ID, ROLE_ID) values (:p0, :p1, :p2) Bindings : [2016/08/09,2016/08/09,1,1,99,99]

without my custom middleware it return me :

Statement : insert into MY_TABLE (DATE, USER_ID, ROLE_ID) values (:p0, :p1, :p2) Bindings : [2016/08/09,1,99]

And this is good

I know the "web" middleware it's by default in

app/providers/RouteServiceProvider.php

But when i delete it, my auth don't work.

Why it's not work with my custom middleware ?

Thx !

  • 写回答

1条回答 默认 最新

  • douxuan4556 2016-08-09 15:27
    关注

    I don't see any code that could affect your query. What I do see is that you seem to have an error in your code:

    class AuthRank {
    
        public function handle($request, Closure $next, $rank)
        {
            // Isn't this repeated unnecessarily?
            // $response = $next($request);
    
            $user = Auth::user();
    
            if(!$user || $user->rank < $rank) {
                return redirect('/');
            }
    
            return $next($request);
        }
    }
    

    Try removing the excess code. If it doesn't work, provide more details in how it is you are creating your affected query and we might be able to help more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c