1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 13:54:03 +00:00

Reinitialize tenancy for queued jobs if tenant id has changed (#276)

* Reinitialize tenancy for queued jobs if tenant id has changed

* Refactor condition logic for better readability
This commit is contained in:
Sean Taylor 2020-02-22 12:58:30 +00:00 committed by GitHub
parent 06ee1ff0e2
commit 5bb743f73d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -109,11 +109,21 @@ class TenancyServiceProvider extends ServiceProvider
// Queue tenancy
$this->app['events']->listen(\Illuminate\Queue\Events\JobProcessing::class, function ($event) {
if (array_key_exists('tenant_id', $event->job->payload())) {
if (! tenancy()->initialized) { // dispatchNow
tenancy()->initialize(tenancy()->find($event->job->payload()['tenant_id']));
}
$tenantId = $event->job->payload()['tenant_id'] ?? null;
// The job is not tenant-aware
if (! $tenantId) {
return;
}
// Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
if (tenancy()->initialized && tenant('id') === $tenantId) {
return;
}
// Tenancy was either not initialized, or initialized for a different tenant.
// Therefore, we initialize it for the correct tenant.
tenancy()->initById($tenantId);
});
}
}