samedi 3 novembre 2018

Get `Authorizer::getResourceOwnerId()` at the model level

I'm using OAuth2 from Lucasdegasperi for Laravel 5.1. Before I make any other requests, I request an access token via my server and log the user in.

Then, with this access_token, I get model1.

If I try to access Authorizer::getResourceOwnerId() in the controller, I can get it no problem. But if I try to access Authorizer::getResourceOwnerId() in the eager loaded model definition, it breaks with error:

Tried to access session data without an active access token laravel

Question: how can I get the Authorizer::getResourceOwnerId() at the model level?

Routes:

$router->get('model1s/{model1}', 'Resources\Model1Controller@show');

Model1Controller:

use LucaDegasperi\OAuth2Server\Facades\Authorizer;
use App\User;
class Model1Controller extends Controller
{
    ...
    public function show(Model $model)
    {
        $owner_id = Authorizer::getResourceOwnerId(); // no error

        die($owner_id); // has no issue getting owner ID

        return $model;
    }

Model 1:

class Model1 extends BaseModel
{
    protected $with = ['model2'];

    ...

}

Model 2:

use LucaDegasperi\OAuth2Server\Facades\Authorizer;
use App\User;
class Model2 extends BaseModel
{
    protected $with = ['model3'];

    ...

    public function model3()
    {
        $owner_id = Authorizer::getResourceOwnerId(); // ERROR: Tried to access session data without an active access token laravel

        ...

        $user = User::find(owner_id);
        $locale_id = $user->profiles[0]->locale_id;

        ...

        return $this->hasMany(Model3::class, 'model3_id', 'id')->where('locale_id', '=', $locale_id);
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire