如何将用户对象绑定到中间件中的请求 [英] How to bind user object to request in a middleware

查看:107
本文介绍了如何将用户对象绑定到中间件中的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Laravel Spark 1.0(Laravel 5.2)编写一个应用程序.我编写了一个用于代理(api)身份验证的自定义中间件.这是代码:

i'm writing an application in Laravel Spark 1.0 (Laravel 5.2). I wrote a custom middleware for agent (api) authentication. This is the code:

<?php

namespace App\Http\Middleware;

use App\Agent;
use Closure;
use Illuminate\Http\Request;

class AgentAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if( isset($request->token) &&  !empty($request->token) )
        {
            $agent = Agent::where('token', '=', $request->token)->first();

            if( $agent != NULL )
            {
                $team = $agent->Team()->first();
                $user = $team->User()->first();

                $request->merge(['team' => $team ]);
                $request->merge(['user' => $user ]);

                return $next($request);
            }
            else {
                return response('Unauthorized 2.', 401);
            }

        }
        else {
            return response('Unauthorized 1.', 401);
        }
    }
}

在默认的laravel身份验证中,将用户对象注入请求中(请参阅laravel文档): https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user

In the default laravel authentication the user object is injected in the request (see laravel docs): https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user

因此您可以使用以下方式检索用户:

So you can retrieve the user using:

$request->user();

Spark显然使用此方法来检查用户订阅是否有效(laravel \ spark \ src \ Http \ Middleware \ VerifyUserIsSubscribed):

Spark obviously use this method to check if user subscription is valid (laravel\spark\src\Http\Middleware\VerifyUserIsSubscribed):

if ($this->subscribed($request->user(), $subscription, $plan, func_num_args() === 2)) {
            return $next($request);
        }

它不起作用,因为使用我的中间件,您可以使用:$request->user;来检索用户,但是不能使用laravel默认值$request->user();

And it's not working because, with my middleware, you can retrieve the user using: $request->user; but not with the laravel defaults $request->user();

我应该如何将用户对象注入请求中?

How should i inject the user object into the request?

提前谢谢

服务提供者中的Laravel(Illuminate \ Auth \ AuthServiceProvider @ registerRequestRebindHandler)

Laravel in the service provider (Illuminate\Auth\AuthServiceProvider@registerRequestRebindHandler)

使用此代码将对象用户绑定到请求:

Use this code to bind object user to the request:

/**
     * Register a resolver for the authenticated user.
     *
     * @return void
     */
    protected function registerRequestRebindHandler()
    {
        $this->app->rebinding('request', function ($app, $request) {
            $request->setUserResolver(function ($guard = null) use ($app) {
                return call_user_func($app['auth']->userResolver(), $guard);
            });
        });
    }

我试图在中间件中插入经过适当校正的代码,但我不知道如何使它工作.

I tried to insert this code, with the appropriate correction, in the middleware but i can't figure out how to make it work.

推荐答案

我没有Spark副本可以尝试此&确保我在做什么对您来说是正确的,但是我认为这会有所帮助:

I don't have a copy of Spark to try this & ensure what I'm doing is correct for you, but I think this will help:

1)一个假设-我相信您是说是的,此行将为您提供所需的用户:

1) An assumption - I believe you are saying that yes, this line will get you the user you want:

$user = $team->User()->first();

,而您只想将其绑定到请求,以便稍后可以通过以下方式在您的应用中访问该用户:

and you merely want to bind it to the request so that you can access this user later in your app via:

$request->user()

2)如果是这样,那么我所做的就是简化您提供的要添加的代码:

2) If this is true, then all I did was simplify the code you provided to add:

$request->merge(['user' => $user ]);

//add this
$request->setUserResolver(function () use ($user) {
    return $user;
});

// if you dump() you can now see the $request has it
dump($request->user());

return $next($request);

我也在路由关闭中添加了$ request-> user(),并且它就在那里.

I also $request->user() in the route closure, and it is there.

重新绑定应用程序对我来说有点奇怪,而且似乎没有必要.我不确定您所做的事情是否真的需要这个.

The app rebinding was a little strange to me, and didn't seem necessary. I'm not sure that anything would really need this for what you are doing.

这篇关于如何将用户对象绑定到中间件中的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆