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.

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

报告相同问题?

悬赏问题

  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移