dimanche 16 septembre 2018

Access user via Authorizer in model definition

In Laravel 5.1 with OAuth2, I want to eager load a model, but place query params (from user model) on the eager loaded model. The easiest way I thought to do this was directly on the model.

More specifically, Activity belongsTo Task. I only want to load activities for the task where they match certain criteria on the user object. Using OAuth2, I attempt to find the user via Authorizer, then access its properties. The route is wrapped in Lucadegasperi's middleware

$router->group(['middleware' => 'oauth'], function(Router $router) {
       $router->get('tasks/{task}', 'Resources\Tasks@show');

And the request header seems to pass along the bearer token just fine.

I'm able to access the user via Authorizer within the controller, but not in the model definition. Why is this?

I'm getting error:

NoActiveAccessTokenException in Authorizer.php line 104: Tried to access session data without an active access token

use LucaDegasperi\OAuth2Server\Facades\Authorizer;
use App\User;

class Task extends BaseModel
{
    protected $with = ['activities'];

    public function activities()
    {
        $user = User::find(Authorizer::getResourceOwnerId());
        return $this->hasMany(Activity::class)->where('id', '=', $user->...);
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire