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 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接