Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADVAPP-32] & [ADVAPP-35] Ensure handling of invalid refresh_token for Outlook and Google #435

Merged
merged 11 commits into from
Jan 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use AdvisingApp\Notification\Notifications\BaseNotification;
use AdvisingApp\Notification\Notifications\EmailNotification;
use AdvisingApp\Notification\Notifications\Messages\MailMessage;
use AdvisingApp\Notification\Models\Contracts\NotifiableInterface;
use AdvisingApp\Notification\Notifications\Concerns\EmailChannelTrait;

class EngagementEmailNotification extends BaseNotification implements EmailNotification
Expand All @@ -51,7 +52,7 @@ public function __construct(
public EngagementDeliverable $deliverable
) {}

public function toEmail(object $notifiable): MailMessage
public function toEmail(NotifiableInterface $notifiable): MailMessage
{
return MailMessage::make()
->subject($this->deliverable->engagement->subject)
Expand All @@ -60,15 +61,15 @@ public function toEmail(object $notifiable): MailMessage
->salutation("Regards, {$this->deliverable->engagement->user->name}");
}

public function beforeSendHook(object $notifiable, OutboundDeliverable $deliverable, string $channel): void
public function beforeSendHook(NotifiableInterface $notifiable, OutboundDeliverable $deliverable, string $channel): void
{
$deliverable->update([
'related_id' => $this->deliverable->id,
'related_type' => $this->deliverable->getMorphClass(),
]);
}

public function afterSendHook(object $notifiable, OutboundDeliverable $deliverable): void
public function afterSendHook(NotifiableInterface $notifiable, OutboundDeliverable $deliverable): void
{
$updateData = array_filter([
'external_reference_id' => $deliverable->external_reference_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use AdvisingApp\Notification\Notifications\DatabaseNotification;
use AdvisingApp\Notification\Notifications\Messages\MailMessage;
use Filament\Notifications\Notification as FilamentNotification;
use AdvisingApp\Notification\Models\Contracts\NotifiableInterface;
use AdvisingApp\Notification\Notifications\Concerns\EmailChannelTrait;
use AdvisingApp\Notification\Notifications\Concerns\DatabaseChannelTrait;

Expand All @@ -56,15 +57,15 @@ public function __construct(
public Engagement $engagement
) {}

public function toEmail(object $notifiable): MailMessage
public function toEmail(NotifiableInterface $notifiable): MailMessage
{
return MailMessage::make()
->settings($this->resolveNotificationSetting($notifiable))
->subject('Your Engagement email has successfully been delivered.')
->line("Your engagement was successfully delivered to {$this->engagement->recipient->display_name}.");
}

public function toDatabase(object $notifiable): array
public function toDatabase(NotifiableInterface $notifiable): array
{
return FilamentNotification::make()
->success()
Expand All @@ -73,8 +74,8 @@ public function toDatabase(object $notifiable): array
->getDatabaseMessage();
}

private function resolveNotificationSetting(User $notifiable): ?NotificationSetting
private function resolveNotificationSetting(NotifiableInterface $notifiable): ?NotificationSetting
{
return $this->engagement->createdBy->teams()->first()?->division?->notificationSetting?->setting;
return $notifiable instanceof User ? $this->engagement->createdBy->teams()->first()?->division?->notificationSetting?->setting : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use AdvisingApp\Notification\Models\OutboundDeliverable;
use AdvisingApp\Notification\Notifications\SmsNotification;
use AdvisingApp\Notification\Notifications\BaseNotification;
use AdvisingApp\Notification\Models\Contracts\NotifiableInterface;
use AdvisingApp\Notification\Notifications\Messages\TwilioMessage;
use AdvisingApp\Notification\Notifications\Concerns\SmsChannelTrait;

Expand All @@ -51,21 +52,21 @@ public function __construct(
public EngagementDeliverable $deliverable
) {}

public function toSms(object $notifiable): TwilioMessage
public function toSms(NotifiableInterface $notifiable): TwilioMessage
{
return TwilioMessage::make($notifiable)
->content($this->deliverable->engagement->getBody());
}

public function beforeSendHook(object $notifiable, OutboundDeliverable $deliverable, string $channel): void
public function beforeSendHook(NotifiableInterface $notifiable, OutboundDeliverable $deliverable, string $channel): void
{
$deliverable->update([
'related_id' => $this->deliverable->id,
'related_type' => $this->deliverable->getMorphClass(),
]);
}

public function afterSendHook(object $notifiable, OutboundDeliverable $deliverable): void
public function afterSendHook(NotifiableInterface $notifiable, OutboundDeliverable $deliverable): void
{
$this->deliverable->update([
'external_reference_id' => $deliverable->external_reference_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use AdvisingApp\Form\Models\FormSubmission;
use AdvisingApp\Notification\Notifications\SmsNotification;
use AdvisingApp\Notification\Notifications\BaseNotification;
use AdvisingApp\Notification\Models\Contracts\NotifiableInterface;
use AdvisingApp\Notification\Notifications\Messages\TwilioMessage;
use AdvisingApp\Notification\Notifications\Concerns\SmsChannelTrait;

Expand All @@ -50,7 +51,7 @@ public function __construct(
public FormSubmission $submission,
) {}

public function toSms(object $notifiable): TwilioMessage
public function toSms(NotifiableInterface $notifiable): TwilioMessage
{
$body = "You have been sent a request to complete {$this->submission->submissible->name} by {$this->submission->requester->name}." .
(filled($this->submission->request_note) ? " {$this->submission->request_note}" : '') .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use AdvisingApp\Notification\Notifications\BaseNotification;
use AdvisingApp\Notification\Notifications\EmailNotification;
use AdvisingApp\Notification\Notifications\Messages\MailMessage;
use AdvisingApp\Notification\Models\Contracts\NotifiableInterface;
use AdvisingApp\IntegrationAwsSesEventHandling\Settings\SesSettings;
use AdvisingApp\Notification\Notifications\Concerns\EmailChannelTrait;

Expand Down Expand Up @@ -102,7 +103,7 @@ class TestEmailNotification extends BaseNotification implements EmailNotificatio
{
use EmailChannelTrait;

public function toEmail(object $notifiable): MailMessage
public function toEmail(NotifiableInterface $notifiable): MailMessage
{
return MailMessage::make()
->subject('Test Subject')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public function up(): void
$table->string('provider_type');
$table->text('provider_id')->nullable();
$table->text('provider_email');
$table->text('oauth_token');
$table->text('oauth_refresh_token');
$table->text('oauth_token')->nullable();
$table->text('oauth_refresh_token')->nullable();

$table->foreignUuid('user_id')->constrained('users')->cascadeOnDelete();

$table->timestamp('oauth_token_expires_at');
$table->timestamp('oauth_token_expires_at')->nullable();

$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,27 @@

</COPYRIGHT>
--}}

@php
use AdvisingApp\MeetingCenter\Enums\CalendarProvider;
@endphp

<div class="flex justify-evenly">
<a href="{{ route('calendar.google.login') }}">
<x-filament::icon
class="h-16 w-16"
icon="icon-google"
/>
</a>

<a href="{{ route('calendar.outlook.login') }}">
<x-filament::icon
class="h-16 w-16"
icon="icon-outlook"
/>
</a>
@if (is_null($calendar->provider_type) || $calendar->provider_type === CalendarProvider::Google)
<a href="{{ route('calendar.google.login') }}">
<x-filament::icon
class="h-16 w-16"
icon="icon-google"
/>
</a>
@endif

@if (is_null($calendar->provider_type) || $calendar->provider_type === CalendarProvider::Outlook)
<a href="{{ route('calendar.outlook.login') }}">
<x-filament::icon
class="h-16 w-16"
icon="icon-outlook"
/>
</a>
@endif
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
<COPYRIGHT>

Copyright © 2022-2023, Canyon GBS LLC. All rights reserved.

Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.

Notice:

- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.

For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at legal@canyongbs.com.

</COPYRIGHT>
*/

namespace AdvisingApp\MeetingCenter\Console\Commands;

use Illuminate\Console\Command;
use AdvisingApp\MeetingCenter\Models\Calendar;
use AdvisingApp\MeetingCenter\Jobs\RefreshCalendarRefreshToken;

class RefreshCalendarRefreshTokens extends Command
{
protected $signature =
'meeting-center:refresh-calendar-refresh-tokens';

protected $description = 'Triggers a refresh of all calendar refresh tokens that are needed.';

public function handle(): void
{
Calendar::query()
->whereNotNull('oauth_refresh_token')
->where('updated_at', '<=', now()->subDays(14))
->each(fn (Calendar $calendar) => RefreshCalendarRefreshToken::dispatch($calendar));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ class CalendarEventResource extends Resource

protected static ?string $modelLabel = 'appointment';

public static function getRelations(): array
{
return [];
}

public static function getPages(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function setupCalendarProviderAction(): Action
return Action::make('setupCalendarProviderAction')
->modalHeading('Choose a provider')
->modalDescription('This feature requires synchronization with Google Calendar or Outlook Calendar. Please select the service you would like to connect to by selecting the appropriate icon below.')
->modalContent(view('meeting-center::filament.components.calendar-setup-modal'))
->modalContent(view('meeting-center::filament.components.calendar-setup-modal', ['calendar' => auth()->user()->calendar]))
->modalSubmitAction(false)
->modalCancelAction(Action::make('cancel')->color('gray')->url(Filament::getUrl()))
->modalCloseButton(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
<COPYRIGHT>

Copyright © 2022-2023, Canyon GBS LLC. All rights reserved.

Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.

Notice:

- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.

For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at legal@canyongbs.com.

</COPYRIGHT>
*/

namespace AdvisingApp\MeetingCenter\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use AdvisingApp\MeetingCenter\Models\Calendar;
use AdvisingApp\MeetingCenter\Managers\CalendarManager;
use AdvisingApp\MeetingCenter\Managers\Contracts\CalendarInterface;

class RefreshCalendarRefreshToken implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public function __construct(public Calendar $calendar) {}

public function handle(): void
{
/** @var CalendarInterface $calendarManager */
$calendarManager = resolve(CalendarManager::class)
->driver($this->calendar->provider_type->value);

$calendarManager->refreshToken($this->calendar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ public function deleteEvent(CalendarEvent $event): void;

public function syncEvents(Calendar $calendar, ?Datetime $start = null, ?Datetime $end = null, ?int $perPage = null): void;

public function refreshToken(Calendar $calendar): Calendar;

public function revokeToken(Calendar $calendar): bool;
}
Loading
Loading