Skip to content

Commit

Permalink
[8.x] Make Listeners, Mailables, and Notifications accept ShouldBeEnc…
Browse files Browse the repository at this point in the history
…rypted (#36036)

* accept a command in object form in Bus::assertChained

* Revert "         accept a command in object form in Bus::assertChained"

This reverts commit 137623f.

* make listeners notifictaions and mailables encryptable in queue

* Update Queue.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
themsaid and taylorotwell authored Jan 25, 2021
1 parent 958386a commit ff33775
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Illuminate/Events/CallQueuedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class CallQueuedListener implements ShouldQueue
*/
public $timeout;

/**
* Indicates if the job should be encrypted.
*
* @var bool
*/
public $shouldBeEncrypted = false;

/**
* Create a new job instance.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Events/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Container\Container as ContainerContract;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -600,6 +601,8 @@ protected function propagateListenerOptions($listener, $job)

$job->retryUntil = method_exists($listener, 'retryUntil')
? $listener->retryUntil() : null;

$job->shouldBeEncrypted = $listener instanceof ShouldBeEncrypted;
});
}

Expand Down
9 changes: 9 additions & 0 deletions src/Illuminate/Mail/SendQueuedMailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Mail\Factory as MailFactory;
use Illuminate\Contracts\Mail\Mailable as MailableContract;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;

class SendQueuedMailable
{
Expand All @@ -31,6 +32,13 @@ class SendQueuedMailable
*/
public $timeout;

/**
* Indicates if the job should be encrypted.
*
* @var bool
*/
public $shouldBeEncrypted = false;

/**
* Create a new job instance.
*
Expand All @@ -43,6 +51,7 @@ public function __construct(MailableContract $mailable)
$this->tries = property_exists($mailable, 'tries') ? $mailable->tries : null;
$this->timeout = property_exists($mailable, 'timeout') ? $mailable->timeout : null;
$this->afterCommit = property_exists($mailable, 'afterCommit') ? $mailable->afterCommit : null;
$this->shouldBeEncrypted = $mailable instanceof ShouldBeEncrypted;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Illuminate/Notifications/SendQueuedNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -49,6 +50,13 @@ class SendQueuedNotifications implements ShouldQueue
*/
public $timeout;

/**
* Indicates if the job should be encrypted.
*
* @var bool
*/
public $shouldBeEncrypted = false;

/**
* Create a new job instance.
*
Expand All @@ -65,6 +73,7 @@ public function __construct($notifiables, $notification, array $channels = null)
$this->tries = property_exists($notification, 'tries') ? $notification->tries : null;
$this->timeout = property_exists($notification, 'timeout') ? $notification->timeout : null;
$this->afterCommit = property_exists($notification, 'afterCommit') ? $notification->afterCommit : null;
$this->shouldBeEncrypted = $notification instanceof ShouldBeEncrypted;
}

/**
Expand Down
17 changes: 16 additions & 1 deletion src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function createObjectPayload($job, $queue)
],
]);

$command = $job instanceof ShouldBeEncrypted && $this->container->bound(Encrypter::class)
$command = $this->jobShouldBeEncrypted($job) && $this->container->bound(Encrypter::class)
? $this->container[Encrypter::class]->encrypt(serialize(clone $job))
: serialize(clone $job);

Expand Down Expand Up @@ -213,6 +213,21 @@ public function getJobExpiration($job)
? $expiration->getTimestamp() : $expiration;
}

/**
* Determine if the job should be encrypted.
*
* @param object $job
* @return bool
*/
protected function jobShouldBeEncrypted($job)
{
if ($job instanceof ShouldBeEncrypted) {
return true;
}

return isset($job->shouldBeEncrypted) && $job->shouldBeEncrypted;
}

/**
* Create a typical, string based queue payload array.
*
Expand Down

0 comments on commit ff33775

Please sign in to comment.