Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use polymorphic relationship #10

Closed
thibaultlavoisey opened this issue Jan 8, 2020 · 8 comments · Fixed by #49
Closed

Use polymorphic relationship #10

thibaultlavoisey opened this issue Jan 8, 2020 · 8 comments · Fixed by #49

Comments

@thibaultlavoisey
Copy link

thibaultlavoisey commented Jan 8, 2020

Could be useful for issuing tokens for organization accounts.

@rennokki
Copy link
Contributor

rennokki commented Jan 9, 2020

I think that a better idea would be the ability to change the Personal Access Token model like Passport: https://laravel.com/docs/5.8/passport#overriding-default-models

Then you would extend the PersonalAccessToken model and change the user relation in the model and set the new model using usePersonalAccessTokenModel()

It is already in #18

@thibaultlavoisey
Copy link
Author

@rennokki Correct me if I'm wrong, changing the personal access token model will allow you to issue token to another model than the user one but not several models, right?

@rennokki
Copy link
Contributor

rennokki commented Jan 10, 2020

Create a new model App\MyCustomTokenModel that extends the base model and replace the user() relation:

<?php

namespace App;

use Laravel\Airlock\PersonalAccessToken;

class MyCustomTokenModel extends PersonalAccessToken
{
    public function user()
    {
        return $this->morphTo();
    }
}

Make use of the function in boot():

use App\MyCustomTokenModel;
use Laravel\Airlock\Airlock;

Airlock::usePersonalAccessTokenModel(
    MyCustomTokenModel::class
);

Make sure you ignore the migrations in the register() in AppServiceProvider.php:

use Laravel\Airlock\Airlock;

Airlock::ignoreMigrations();

Publish the migrations or copy the migration file from the package in your database/migraitons/ folder and replace the user_id field with a morph field:

Schema::create('personal_access_tokens', function (Blueprint $table) {
    ...

    $table->morphs('user'); // let the name 'user' alone because morphTo is greedy due to the user() relationship

    ...
});

In your model (either it's User, Organisation or whatever) that uses the HasApiTokens trait in use, replace the tokens() method to make use of the morphMany:

use Laravel\Airlock\Airlock;

public function tokens()
{
    return $this->morphMany(Airlock::$personalAccessTokenModel, 'model');
}

Jobs done.

@thibaultlavoisey
Copy link
Author

IMHO it would be simpler that Airlock handle this out of the box using a polymorphic relationship :) Your solution works but it's a bit of extra work for a common use case.

@rennokki
Copy link
Contributor

I took Passport's example on this case. I think stuff should be simple out-of-the-box. :male_shrug:

@driesvints driesvints changed the title Suggestion: Use polymorphic relationship Use polymorphic relationship Jan 10, 2020
@socheatsok78
Copy link

socheatsok78 commented Jan 10, 2020

I agree to this, I was struggling to customize Laravel Passport to support polymorphic relationship. And I still resolve to use custom HTTP Header hack to get around it.

References:

@thibaultlavoisey
Copy link
Author

@taylorotwell Would you accept a PR for this?

@socheatsok78
Copy link

@taylorotwell

Thank you kind sir!
👏 👏 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants