diff --git a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index 6e8ed357..7c1dcbb6 100644 --- a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php @@ -65,7 +65,7 @@ class SendIncidentEmailNotificationHandler // Only send emails for public incidents. if ($event->incident->visible === 1) { - foreach ($this->subscriber->all() as $subscriber) { + foreach ($this->subscriber->verified()->get() as $subscriber) { $mail = [ 'email' => $subscriber->email, 'subject' => 'New incident reported.', diff --git a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php index 70530ce2..77c61454 100644 --- a/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendMaintenanceEmailNotificationHandler.php @@ -62,7 +62,7 @@ class SendMaintenanceEmailNotificationHandler $data = AutoPresenter::decorate($event->incident); - foreach ($this->subscriber->all() as $subscriber) { + foreach ($this->subscriber->verified()->get() as $subscriber) { $mail = [ 'email' => $subscriber->email, 'subject' => 'Scheduled maintenance.', diff --git a/app/Http/Controllers/SubscribeController.php b/app/Http/Controllers/SubscribeController.php index 6cb348ad..8c4e5145 100644 --- a/app/Http/Controllers/SubscribeController.php +++ b/app/Http/Controllers/SubscribeController.php @@ -15,6 +15,7 @@ use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand; use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand; use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand; +use CachetHQ\Cachet\Exceptions\AlreadySubscribedException; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Subscriber; use GrahamCampbell\Binput\Facades\Binput; @@ -50,6 +51,10 @@ class SubscribeController extends Controller { try { $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'))); + } catch (AlreadySubscribedException $e) { + return Redirect::route('subscribe.subscribe') + ->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('cachet.subscriber.email.failure'))) + ->withErrors($e->getMessage()); } catch (ValidationException $e) { return Redirect::route('subscribe.subscribe') ->withInput(Binput::all()) @@ -76,7 +81,7 @@ class SubscribeController extends Controller $subscriber = Subscriber::where('verify_code', '=', $code)->first(); - if (!$subscriber || $subscriber->verified()) { + if (!$subscriber || $subscriber->is_verified) { throw new BadRequestHttpException(); } @@ -101,7 +106,7 @@ class SubscribeController extends Controller $subscriber = Subscriber::where('verify_code', '=', $code)->first(); - if (!$subscriber || !$subscriber->verified()) { + if (!$subscriber || !$subscriber->is_verified) { throw new BadRequestHttpException(); } diff --git a/app/Models/Subscriber.php b/app/Models/Subscriber.php index 4d3adbe5..8303641f 100644 --- a/app/Models/Subscriber.php +++ b/app/Models/Subscriber.php @@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Models; use AltThree\Validator\ValidatingTrait; use CachetHQ\Cachet\Presenters\SubscriberPresenter; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use McCool\LaravelAutoPresenter\HasPresenter; @@ -62,12 +63,24 @@ class Subscriber extends Model implements HasPresenter }); } + /** + * Scope a query to only include verified subscribers. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeVerified(Builder $query) + { + return $query->whereNotNull('verified_at'); + } + /** * Determines if the subscriber is verified. * * @return bool */ - public function verified() + public function getIsVerifiedAttribute() { return $this->verified_at !== null; }