Laravel 5.2-从中间件覆盖供应商功能

时间:2018-07-23 09:05:50

标签: php laravel oauth-2.0

我正在处理包裹:

https://github.com/lucadegasperi/oauth2-server-laravel

我想在https://github.com/lucadegasperi/oauth2-server-laravel/blob/master/src/Authorizer.php

中覆盖函数“ getResourceOwnerId()

所以我需要重写该函数,并向该类添加新函数“ getAccessToken_custom ”:

public function getResourceOwnerId()
    {
        $check = $this->getAccessToken_custom();

        if($check != 0){
            return $this->getAccessToken()->getSession()->getOwnerId();
        }else{
            return 0;
        }

    }

    public function getAccessToken_custom(){

        $accessToken = $this->getChecker()->getAccessToken();

        if (is_null($accessToken)) {

            return 0;

        }else{
            return $accessToken;
        }
        //return $accessToken;
    }

我正在这样从中间件调用函数:

use LucaDegasperi\OAuth2Server\Facades\Authorizer;

public function handle($request, Closure $next)
    {
        // Return user ID or return 0
        $user_id = Authorizer::getResourceOwnerId();

        if($user_id == 0){
            return $next($request);
        }else{
            $user=\App\User::find($user_id);
            Auth::login($user);
            return $next($request);
        }

    }

您能帮我知道如何重写功能供应商吗?这样我就可以返回我的自定义值。谢谢你们!

1 个答案:

答案 0 :(得分:0)

您可以在您的应用文件夹中创建Authorizer.php并将此代码放入

namespace app;

class Authorizer extends \LucaDegasperi\OAuth2Server\Authorizer
{
   public static function getResourceOwnerId()
   {
       //your code
   }
}

在中间件中,请使用您的Authorizer

use app\Authorizer;

public function handle($request, Closure $next){}