Browse Source

Added default recipient updated notification

Will Browning 3 years ago
parent
commit
43a2aedf4b

+ 6 - 0
app/Http/Controllers/DefaultRecipientController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
 
 use App\Http\Requests\EditDefaultRecipientRequest;
 use App\Http\Requests\UpdateDefaultRecipientRequest;
+use App\Notifications\DefaultRecipientUpdated;
 
 class DefaultRecipientController extends Controller
 {
@@ -15,15 +16,20 @@ class DefaultRecipientController extends Controller
     public function __construct()
     {
         $this->middleware('throttle:1,1')->only('edit');
+        $this->middleware('throttle:3,1')->only('update');
     }
 
     public function update(UpdateDefaultRecipientRequest $request)
     {
         $recipient = user()->verifiedRecipients()->findOrFail($request->default_recipient);
 
+        $currentDefaultRecipient = user()->email;
+
         user()->default_recipient = $recipient;
         user()->save();
 
+        user()->notify(new DefaultRecipientUpdated($currentDefaultRecipient));
+
         return back()->with(['status' => 'Default Recipient Updated Successfully']);
     }
 

+ 94 - 0
app/Notifications/DefaultRecipientUpdated.php

@@ -0,0 +1,94 @@
+<?php
+
+namespace App\Notifications;
+
+use App\Helpers\OpenPGPSigner;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeEncrypted;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Notifications\Messages\MailMessage;
+use Illuminate\Notifications\Notification;
+use Swift_SwiftException;
+
+class DefaultRecipientUpdated extends Notification implements ShouldQueue, ShouldBeEncrypted
+{
+    use Queueable;
+
+    protected $previousDefaultRecipient;
+
+    /**
+     * Create a new notification instance.
+     *
+     * @return void
+     */
+    public function __construct($previousDefaultRecipient)
+    {
+        $this->previousDefaultRecipient = $previousDefaultRecipient;
+    }
+
+    /**
+     * Get the notification's delivery channels.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function via($notifiable)
+    {
+        return ['mail'];
+    }
+
+    /**
+     * Get the mail representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return \Illuminate\Notifications\Messages\MailMessage
+     */
+    public function toMail($notifiable)
+    {
+        $openpgpsigner = null;
+        $recipient = $notifiable->defaultRecipient;
+        $fingerprint = $recipient->should_encrypt ? $recipient->fingerprint : null;
+
+        if ($fingerprint) {
+            try {
+                $openpgpsigner = OpenPGPSigner::newInstance(config('anonaddy.signing_key_fingerprint'), [], "~/.gnupg");
+                $openpgpsigner->addRecipient($fingerprint);
+            } catch (Swift_SwiftException $e) {
+                info($e->getMessage());
+                $openpgpsigner = null;
+
+                $recipient->update(['should_encrypt' => false]);
+
+                $recipient->notify(new GpgKeyExpired);
+            }
+        }
+
+        return (new MailMessage)
+            ->subject("Your default recipient has just been updated")
+            ->markdown('mail.default_recipient_updated', [
+                'previousDefaultRecipient' => $this->previousDefaultRecipient,
+                'defaultRecipient' => $notifiable->email
+            ])
+            ->withSwiftMessage(function ($message) use ($openpgpsigner) {
+                $message->getHeaders()
+                        ->addTextHeader('Feedback-ID', 'DRU:anonaddy');
+
+                if ($openpgpsigner) {
+                    $message->attachSigner($openpgpsigner);
+                }
+            });
+    }
+
+    /**
+     * Get the array representation of the notification.
+     *
+     * @param  mixed  $notifiable
+     * @return array
+     */
+    public function toArray($notifiable)
+    {
+        return [
+            //
+        ];
+    }
+}

+ 12 - 0
resources/views/mail/default_recipient_updated.blade.php

@@ -0,0 +1,12 @@
+@component('mail::message')
+
+# Default Recipient Updated
+
+Your account's default recipient has just been updated from **{{ $previousDefaultRecipient }}** to **{{ $defaultRecipient }}**.
+
+If this change was not made by you, please visit the settings page, log out of all other browser sessions and update your account's password.
+
+@component('mail::button', ['url' => config('app.url').'/settings'])
+Check Settings
+@endcomponent
+@endcomponent