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

New tenant registration + Passport #62

Closed
dambridge opened this issue Jun 25, 2020 · 10 comments
Closed

New tenant registration + Passport #62

dambridge opened this issue Jun 25, 2020 · 10 comments

Comments

@dambridge
Copy link
Sponsor

As discussed in #29 I have likely a simple question. When registering a new tenant, should we be calling php artisan passport:install each time to generate keys and clients as part of the initilization?

@masterix21
Copy link
Collaborator

Hi @dambridge, it's logic dependent.

If you have a separated users logic tenant by tenant, I can suggest to call php artisan tenants:artisan "passport:install".

If your users logic is included in your landlord, it isn't needed.

@dambridge
Copy link
Sponsor Author

dambridge commented Jun 25, 2020

@masterix21 In my registration handler, I'm basically going to do this then:

exec('cd ' . base_path() . ' && php artisan tenants:artisan "migrate --seed" --tenant=' . $tenant->id);
exec('cd ' . base_path() . ' && php artisan tenants:artisan "passport:install" --tenant=' . $tenant->id);

@masterix21
Copy link
Collaborator

masterix21 commented Jun 25, 2020

Yes, it seems ok What are your concerns?

PS: why don't you use: Artisan::call('tenants:artisan', [ "artisanCommand" => "passport:install", "--tenant" => $tenant->id ]);?

@dambridge
Copy link
Sponsor Author

Per #23 , there are issues I had running Artisan directly.

Also, I just tried it but the Passport tables were still installed in the landlord database.

@masterix21
Copy link
Collaborator

Have you moved the passport migrations inside the tenant directory?

@dambridge
Copy link
Sponsor Author

dambridge commented Jun 25, 2020

Yes. Well, the default migrations folder is the tenant directory, so that's where they are. Must be using the wrong connection somehow, but the other migrate command runs just fine. My Task:

<?php

namespace App\Tasks;

use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
use Laravel\Passport\Passport;

class PassportTask implements SwitchTenantTask
{
    public function __construct()
    {
        //
    }

    public function makeCurrent(Tenant $tenant): void
    {
        $this->setTenantPassport($tenant);
    }

    public function forgetCurrent(): void
    {
        $this->setTenantPassport();
    }

    protected function setTenantPassport(?Tenant $tenant = null): void
    {
        Passport::useTokenModel(TokenTenantAware::class);
        Passport::useClientModel(ClientTenantAware::class);
        Passport::useAuthCodeModel(AuthCodeTenantAware::class);
        Passport::usePersonalAccessClientModel(PersonalAccessClientTenantAware::class);
    }
}

@dambridge
Copy link
Sponsor Author

Ah, hold up. This is the culprit methinks (in the default Passport migrations):

public function getConnection()
    {
        return config('passport.storage.database.connection');
    }

@masterix21
Copy link
Collaborator

masterix21 commented Jun 25, 2020

Change it:
exec('cd ' . base_path() . ' && php artisan tenants:artisan "migrate --seed" --tenant=' . $tenant->id);

with:
exec('cd ' . base_path() . ' && php artisan tenants:artisan "migrate --seed --connection=tenant" --tenant=' . $tenant->id);

It's really important to specify the connection.

@dambridge
Copy link
Sponsor Author

dambridge commented Jun 25, 2020

I published the Passport config and set the connection to 'tenant', migrations ran properly. However, NOW I'm getting an error when trying to create a token for the user:

Class 'App\Tasks\PersonalAccessClientTenantAware' not found

when trying

$token = $user->createToken('API')->accessToken;

@dambridge
Copy link
Sponsor Author

Oh God. Nothing to see here. Forgot to import classes. Thanks @masterix21 .

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

No branches or pull requests

2 participants