Skip to content

Commit 7b43b33

Browse files
authored
Merge pull request #588 from Joel-Jensen/main
[Bugfix]: Jobs with `ShouldBeEncrypted` need to be decrypted
2 parents ae75c18 + 236af33 commit 7b43b33

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Actions/MakeQueueTenantAwareAction.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\Multitenancy\Actions;
44

5+
use Illuminate\Contracts\Encryption\Encrypter;
56
use Illuminate\Queue\Events\JobProcessing;
67
use Illuminate\Queue\Events\JobRetryRequested;
78
use Illuminate\Support\Arr;
@@ -49,8 +50,14 @@ protected function isTenantAware(JobProcessing|JobRetryRequested $event): bool
4950
{
5051
$payload = $this->getEventPayload($event);
5152

53+
$serializedCommand = $payload['data']['command'];
54+
55+
if (! str_starts_with($serializedCommand, 'O:')) {
56+
$serializedCommand = app(Encrypter::class)->decrypt($serializedCommand);
57+
}
58+
5259
try {
53-
$command = unserialize($payload['data']['command']);
60+
$command = unserialize($serializedCommand);
5461
} catch (Throwable) {
5562
/**
5663
* We might need the tenant to unserialize jobs as models could
@@ -62,7 +69,7 @@ protected function isTenantAware(JobProcessing|JobRetryRequested $event): bool
6269
$tenant?->makeCurrent();
6370
}
6471

65-
$command = unserialize($payload['data']['command']);
72+
$command = unserialize($serializedCommand);
6673
}
6774

6875
$job = $this->getJobFromQueueable($command);

tests/Feature/TenantAwareJobs/QueueIsTenantAwareByDefaultTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Illuminate\Queue\Events\JobFailed;
55
use Illuminate\Support\Facades\Event;
66
use Spatie\Multitenancy\Models\Tenant;
7+
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\EncryptedTenantAware;
78
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\NotTenantAwareTestJob;
89
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\TenantAwareTestJob;
910
use Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses\TestJob;
@@ -102,3 +103,16 @@
102103
$currentTenantIdInJob = $this->valuestore->get('tenantId');
103104
expect($currentTenantIdInJob)->toBeNull();
104105
});
106+
107+
it('will decrypt encrypted jobs', function () {
108+
config()->set('multitenancy.queues_are_tenant_aware_by_default', true);
109+
110+
$this->tenant->makeCurrent();
111+
112+
$job = new EncryptedTenantAware($this->valuestore);
113+
app(Dispatcher::class)->dispatch($job);
114+
115+
$this->artisan('queue:work --once')->assertExitCode(0);
116+
117+
Event::assertNotDispatched(JobFailed::class);
118+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Spatie\Multitenancy\Tests\Feature\TenantAwareJobs\TestClasses;
4+
5+
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
6+
use Spatie\Multitenancy\Jobs\TenantAware;
7+
8+
class EncryptedTenantAware extends TestJob implements TenantAware, ShouldBeEncrypted
9+
{
10+
}

0 commit comments

Comments
 (0)