Google Sheet - OAuth "client" powered integrations (#415)

* fix `helpers.php`

* fix `.eslintrc.cjs`

* spreadsheet manager

* fetch providers. set `oauth_id` for integrations

* create spreadsheet on integration create event

* connect OAuth accounts

* display actions. connect account if missing

* cleanup

* handle form field change

* map integration data object to `SpreadsheetData`

* validate request

* wip

* redirect to integrations page

* fix refresh token

* add helper text

* add extra integration info

* refactor

* refresh google token

* fix validation

* add tests

* Fix linting issue

* Update composer lock file

---------

Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Boris Lepikhin
2024-06-05 06:35:46 -07:00
committed by GitHub
parent 03d695c74e
commit 24d33a9ebb
53 changed files with 3383 additions and 298 deletions

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Integrations\Data;
use Spatie\LaravelData\Data;
class SpreadsheetData extends Data
{
public function __construct(
public string $url = '',
public string $spreadsheet_id = '',
public ?array $columns = []
) {
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Integrations\Google;
use App\Integrations\Google\Sheets\SpreadsheetManager;
use App\Models\Integration\FormIntegration;
use Google\Client as Client;
class Google
{
protected Client $client;
protected ?string $token;
protected ?string $refreshToken;
public function __construct(
protected FormIntegration $formIntegration
) {
$this->client = new Client();
$this->client->setClientId(config('services.google.client_id'));
$this->client->setClientSecret(config('services.google.client_secret'));
$this->client->setAccessToken([
'access_token' => $this->formIntegration->provider->access_token,
'created' => $this->formIntegration->provider->updated_at->getTimestamp(),
'expires_in' => 3600,
]);
}
public function getClient(): Client
{
if($this->client->isAccessTokenExpired()) {
$this->refreshToken();
}
return $this->client;
}
public function refreshToken(): static
{
$this->client->refreshToken($this->formIntegration->provider->refresh_token);
$token = $this->client->getAccessToken();
$this->formIntegration->provider->update([
'access_token' => $token['access_token'],
'refresh_token' => $token['refresh_token'],
]);
return $this;
}
public function sheets(): SpreadsheetManager
{
return new SpreadsheetManager($this, $this->formIntegration);
}
}

View File

@@ -0,0 +1,189 @@
<?php
namespace App\Integrations\Google\Sheets;
use App\Integrations\Data\SpreadsheetData;
use App\Integrations\Google\Google;
use App\Models\Forms\Form;
use App\Models\Integration\FormIntegration;
use App\Service\Forms\FormSubmissionFormatter;
use Google\Service\Sheets;
use Google\Service\Sheets\BatchUpdateValuesRequest;
use Google\Service\Sheets\Spreadsheet;
use Google\Service\Sheets\ValueRange;
use Illuminate\Support\Arr;
class SpreadsheetManager
{
protected Sheets $driver;
protected SpreadsheetData $data;
public function __construct(
protected Google $google,
protected FormIntegration $integration
) {
$this->driver = new Sheets($google->getClient());
$this->data = empty($this->integration->data)
? new SpreadsheetData()
: new SpreadsheetData(
url: $this->integration->data->url,
spreadsheet_id: $this->integration->data->spreadsheet_id,
columns: array_map(
fn ($column) => (array) $column,
$this->integration->data->columns
)
);
}
protected function convertToArray(mixed $object): array
{
return is_scalar($object) || is_null($object)
? $object
: $this->convertToArray((array) $object);
}
public function get(string $id): Spreadsheet
{
$spreadsheet = $this->driver
->spreadsheets
->get($id);
return $spreadsheet;
}
public function create(Form $form): Spreadsheet
{
$body = new Spreadsheet([
'properties' => [
'title' => $form->title
]
]);
$spreadsheet = $this->driver->spreadsheets->create($body);
$this->data->url = $spreadsheet->spreadsheetUrl;
$this->data->spreadsheet_id = $spreadsheet->spreadsheetId;
$this->data->columns = [];
$this->updateHeaders($spreadsheet->spreadsheetId);
return $spreadsheet;
}
public function buildColumns(): array
{
$properties = $this->integration->form->properties;
foreach ($properties as $property) {
$key = Arr::first(
array_keys($this->data->columns),
fn (int $key) => $this->data->columns[$key]['id'] === $property['id']
);
$column = Arr::only($property, ['id', 'name']);
if (!is_null($key)) {
$this->data->columns[$key] = $column;
} else {
$this->data->columns[] = $column;
}
}
$this->integration->update([
'data' => $this->data,
]);
return $this->data->columns;
}
public function updateHeaders(string $id): static
{
$columns = $this->buildColumns();
$headers = array_map(
fn ($column) => $column['name'],
$columns
);
return $this->setHeaders($id, $headers);
}
protected function setHeaders(string $id, array $headers): static
{
$valueRange = new ValueRange([
'values' => [$headers],
]);
$valueRange->setRange(
$this->buildRange($headers)
);
$body = new BatchUpdateValuesRequest([
'valueInputOption' => 'RAW',
'data' => [$valueRange]
]);
$this->driver
->spreadsheets_values
->batchUpdate($id, $body);
return $this;
}
public function buildRow(array $submissionData): array
{
$formatter = (new FormSubmissionFormatter($this->integration->form, $submissionData))->outputStringsOnly();
$fields = $formatter->getFieldsWithValue();
return collect($this->data->columns)
->map(function (array $column) use ($fields) {
$field = Arr::first($fields, fn ($field) => $field['id'] === $column['id']);
return $field ? $field['value'] : '';
})
->toArray();
}
public function submit(array $submissionData): static
{
$this->updateHeaders($this->data->spreadsheet_id);
$row = $this->buildRow($submissionData);
$this->addRow(
$this->data->spreadsheet_id,
$row
);
return $this;
}
public function addRow(string $id, array $values): static
{
$valueRange = new ValueRange([
'values' => [$values],
]);
$params = [
'valueInputOption' => 'RAW',
];
$this->driver
->spreadsheets_values
->append(
$id,
$this->buildRange($values),
$valueRange,
$params
);
return $this;
}
protected function buildRange(array $values): string
{
return "A1:" . chr(64 + count($values)) . "1";
}
}

View File

@@ -0,0 +1,146 @@
<?php
namespace App\Integrations\Handlers;
use App\Models\Integration\FormIntegration;
use App\Events\Forms\FormSubmitted;
use App\Models\Integration\FormIntegrationsEvent;
use App\Service\Forms\FormSubmissionFormatter;
use App\Service\Forms\FormLogicConditionChecker;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Vinkla\Hashids\Facades\Hashids;
abstract class AbstractIntegrationHandler
{
protected $form = null;
protected $submissionData = null;
protected $integrationData = null;
public function __construct(
protected FormSubmitted $event,
protected FormIntegration $formIntegration,
protected array $integration
) {
$this->form = $event->form;
$this->submissionData = $event->data;
$this->integrationData = $formIntegration->data;
}
protected function getProviderName(): string
{
return $this->integration['name'] ?? '';
}
protected function logicConditionsMet(): bool
{
if (!$this->formIntegration->logic) {
return true;
}
return FormLogicConditionChecker::conditionsMet(
json_decode(json_encode($this->formIntegration->logic), true),
$this->submissionData
);
}
protected function shouldRun(): bool
{
return $this->logicConditionsMet();
}
protected function getWebhookUrl(): ?string
{
return '';
}
/**
* Default webhook payload. Can be changed in child classes.
*/
protected function getWebhookData(): array
{
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))
->useSignedUrlForFiles()
->showHiddenFields();
$formattedData = [];
foreach ($formatter->getFieldsWithValue() as $field) {
$formattedData[$field['name']] = $field['value'];
}
$data = [
'form_title' => $this->form->title,
'form_slug' => $this->form->slug,
'submission' => $formattedData,
];
if ($this->form->is_pro && $this->form->editable_submissions) {
$data['edit_link'] = $this->form->share_url . '?submission_id=' . Hashids::encode(
$this->submissionData['submission_id']
);
}
return $data;
}
final public function run(): void
{
try {
$this->handle();
$this->formIntegration->events()->create([
'status' => FormIntegrationsEvent::STATUS_SUCCESS,
]);
} catch (\Exception $e) {
$this->formIntegration->events()->create([
'status' => FormIntegrationsEvent::STATUS_ERROR,
'data' => $this->extractEventDataFromException($e),
]);
}
}
public function created(): void
{
//
}
/**
* Default handle. Can be changed in child classes.
*/
public function handle(): void
{
if (!$this->shouldRun()) {
return;
}
Http::throw()->post($this->getWebhookUrl(), $this->getWebhookData());
}
abstract public static function getValidationRules(): array;
public static function isOAuthRequired(): bool
{
return false;
}
public static function getValidationAttributes(): array
{
return [];
}
public static function formatData(array $data): array
{
return $data;
}
public function extractEventDataFromException(\Exception $e): array
{
if ($e instanceof RequestException) {
return [
'message' => $e->getMessage(),
'response' => $e->response->json(),
'status' => $e->response->status(),
];
}
return [
'message' => $e->getMessage()
];
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace App\Integrations\Handlers;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Arr;
use Vinkla\Hashids\Facades\Hashids;
class DiscordIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'discord_webhook_url' => 'required|url|starts_with:https://discord.com/api/webhooks',
'include_submission_data' => 'boolean',
'link_open_form' => 'boolean',
'link_edit_form' => 'boolean',
'views_submissions_count' => 'boolean',
'link_edit_submission' => 'boolean'
];
}
protected function getWebhookUrl(): ?string
{
return $this->integrationData->discord_webhook_url;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && $this->form->is_pro && parent::shouldRun();
}
protected function getWebhookData(): array
{
$settings = (array) $this->integrationData ?? [];
$externalLinks = [];
if (Arr::get($settings, 'link_open_form', true)) {
$externalLinks[] = '[**🔗 Open Form**](' . $this->form->share_url . ')';
}
if (Arr::get($settings, 'link_edit_form', true)) {
$editFormURL = front_url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '[**✍️ Edit Form**](' . $editFormURL . ')';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->submissionData['submission_id']);
$externalLinks[] = '[**✍️ ' . $this->form->editable_submissions_button_text . '**](' . $this->form->share_url . '?submission_id=' . $submissionId . ')';
}
$color = hexdec(str_replace('#', '', $this->form->color));
$blocks = [];
if (Arr::get($settings, 'include_submission_data', true)) {
$submissionString = '';
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
$submissionString .= '**' . ucfirst($field['name']) . '**: ' . $tmpVal . "\n";
}
$blocks[] = [
'type' => 'rich',
'color' => $color,
'description' => $submissionString,
];
}
if (Arr::get($settings, 'views_submissions_count', true)) {
$countString = '**👀 Views**: ' . (string) $this->form->views_count . " \n";
$countString .= '**🖊️ Submissions**: ' . (string) $this->form->submissions_count;
$blocks[] = [
'type' => 'rich',
'color' => $color,
'description' => $countString,
];
}
if (count($externalLinks) > 0) {
$blocks[] = [
'type' => 'rich',
'color' => $color,
'description' => implode(' - ', $externalLinks),
];
}
return [
'content' => 'New submission for your form **' . $this->form->title . '**',
'tts' => false,
'username' => config('app.name'),
'avatar_url' => asset('img/logo.png'),
'embeds' => $blocks,
];
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Integrations\Handlers;
use App\Rules\OneEmailPerLine;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use App\Notifications\Forms\FormSubmissionNotification;
class EmailIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'notification_emails' => ['required', new OneEmailPerLine()],
'notification_reply_to' => 'email|nullable',
];
}
protected function shouldRun(): bool
{
return $this->integrationData->notification_emails && parent::shouldRun();
}
public function handle(): void
{
if (!$this->shouldRun()) {
return;
}
$subscribers = collect(preg_split("/\r\n|\n|\r/", $this->integrationData->notification_emails))
->filter(function ($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
});
Log::debug('Sending email notification', [
'recipients' => $subscribers->toArray(),
'form_id' => $this->form->id,
'form_slug' => $this->form->slug,
]);
$subscribers->each(function ($subscriber) {
Notification::route('mail', $subscriber)->notify(
new FormSubmissionNotification($this->event, $this->integrationData)
);
});
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Integrations\Handlers\Events;
use App\Models\Integration\FormIntegration;
class AbstractIntegrationCreated
{
public function __construct(
protected FormIntegration $formIntegration
) {
}
public function handle(): void
{
//
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Integrations\Handlers\Events;
use App\Integrations\Google\Google;
use App\Models\Integration\FormIntegration;
class GoogleSheetsIntegrationCreated extends AbstractIntegrationCreated
{
protected Google $client;
public function __construct(
protected FormIntegration $formIntegration
) {
parent::__construct($formIntegration);
$this->client = new Google($formIntegration);
}
public function handle(): void
{
$this->client->sheets()
->create($this->formIntegration->form);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace App\Integrations\Handlers;
use App\Events\Forms\FormSubmitted;
use App\Integrations\Google\Google;
use App\Models\Integration\FormIntegration;
use Exception;
use Illuminate\Support\Facades\Log;
class GoogleSheetsIntegration extends AbstractIntegrationHandler
{
protected Google $client;
public function __construct(
protected FormSubmitted $event,
protected FormIntegration $formIntegration,
protected array $integration
) {
parent::__construct($event, $formIntegration, $integration);
$this->client = new Google($formIntegration);
}
public static function getValidationRules(): array
{
return [
];
}
public static function isOAuthRequired(): bool
{
return true;
}
public static function getValidationAttributes(): array
{
return [
'oauth_id' => 'Google Account',
];
}
public function handle(): void
{
if (!$this->shouldRun()) {
return;
}
Log::debug('Creating Google Spreadsheet record', [
'spreadsheet_id' => $this->getSpreadsheetId(),
'form_id' => $this->form->id,
'form_slug' => $this->form->slug,
]);
$this->client->sheets()->submit($this->submissionData);
}
protected function getSpreadsheetId(): string
{
if(!isset($this->integrationData->spreadsheet_id)) {
throw new Exception('The spreadsheed is not instantiated');
}
return $this->integrationData->spreadsheet_id;
}
protected function shouldRun(): bool
{
return parent::shouldRun() && $this->formIntegration->oauth_id && $this->getSpreadsheetId();
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace App\Integrations\Handlers;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Arr;
use Vinkla\Hashids\Facades\Hashids;
class SlackIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'slack_webhook_url' => 'required|url|starts_with:https://hooks.slack.com/',
'include_submission_data' => 'boolean',
'link_open_form' => 'boolean',
'link_edit_form' => 'boolean',
'views_submissions_count' => 'boolean',
'link_edit_submission' => 'boolean'
];
}
protected function getWebhookUrl(): ?string
{
return $this->integrationData->slack_webhook_url;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && $this->form->is_pro && parent::shouldRun();
}
protected function getWebhookData(): array
{
$settings = (array) $this->integrationData ?? [];
$externalLinks = [];
if (Arr::get($settings, 'link_open_form', true)) {
$externalLinks[] = '*<' . $this->form->share_url . '|🔗 Open Form>*';
}
if (Arr::get($settings, 'link_edit_form', true)) {
$editFormURL = front_url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '*<' . $editFormURL . '|✍️ Edit Form>*';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->submissionData['submission_id']);
$externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
}
$blocks = [
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'New submission for your form *' . $this->form->title . '*',
],
],
];
if (Arr::get($settings, 'include_submission_data', true)) {
$submissionString = '';
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
$submissionString .= '>*' . ucfirst($field['name']) . '*: ' . $tmpVal . " \n";
}
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $submissionString,
],
];
}
if (Arr::get($settings, 'views_submissions_count', true)) {
$countString = '*👀 Views*: ' . (string) $this->form->views_count . " \n";
$countString .= '*🖊️ Submissions*: ' . (string) $this->form->submissions_count;
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $countString,
],
];
}
if (count($externalLinks) > 0) {
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => implode(' ', $externalLinks),
],
];
}
return [
'blocks' => $blocks,
];
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace App\Integrations\Handlers;
use App\Mail\Forms\SubmissionConfirmationMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Log;
use Stevebauman\Purify\Facades\Purify;
/**
* Sends a confirmation to form respondant that form was submitted
*/
class SubmissionConfirmationIntegration extends AbstractIntegrationHandler
{
public const RISKY_USERS_LIMIT = 120;
public static function getValidationRules(): array
{
return [
'respondent_email' => [
'required',
'boolean',
function ($attribute, $value, $fail) {
if ($value !== true) {
$fail('Need at least 1 email field.');
}
},
],
'confirmation_reply_to' => 'email|nullable',
'notification_sender' => 'required',
'notification_subject' => 'required',
'notification_body' => 'required',
'notifications_include_submission' => 'boolean'
];
}
protected function shouldRun(): bool
{
return !(!$this->form->is_pro) && parent::shouldRun() && !$this->riskLimitReached();
}
public function handle(): void
{
if (!$this->shouldRun()) {
return;
}
$email = $this->getRespondentEmail();
if (!$email) {
return;
}
Log::info('Sending submission confirmation', [
'recipient' => $email,
'form_id' => $this->form->id,
'form_slug' => $this->form->slug,
]);
Mail::to($email)->send(new SubmissionConfirmationMail($this->event, $this->integrationData));
}
private function getRespondentEmail()
{
// Make sure we only have one email field in the form
$emailFields = collect($this->form->properties)->filter(function ($field) {
$hidden = $field['hidden'] ?? false;
return !$hidden && $field['type'] == 'email';
});
if ($emailFields->count() != 1) {
return null;
}
if (isset($this->submissionData[$emailFields->first()['id']])) {
$email = $this->submissionData[$emailFields->first()['id']];
if ($this->validateEmail($email)) {
return $email;
}
}
return null;
}
// To avoid phishing abuse we limit this feature for risky users
private function riskLimitReached(): bool
{
// This is a per-workspace limit for risky workspaces
if ($this->form->workspace->is_risky) {
if ($this->form->workspace->submissions_count >= self::RISKY_USERS_LIMIT) {
Log::error('!!!DANGER!!! Dangerous user detected! Attempting many email sending.', [
'form_id' => $this->form->id,
'workspace_id' => $this->form->workspace->id,
]);
return true;
}
}
return false;
}
public static function validateEmail($email): bool
{
return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}
public static function formatData(array $data): array
{
return array_merge(parent::formatData($data), [
'notification_body' => Purify::clean($data['notification_body'] ?? ''),
]);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Integrations\Handlers;
class WebhookIntegration extends AbstractIntegrationHandler
{
public static function getValidationRules(): array
{
return [
'webhook_url' => 'required|url'
];
}
protected function getWebhookUrl(): ?string
{
return $this->integrationData->webhook_url;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && parent::shouldRun();
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Integrations\OAuth\Drivers\Contracts;
use Laravel\Socialite\Contracts\User;
interface OAuthDriver
{
public function getRedirectUrl(): string;
public function getUser(): User;
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Integrations\OAuth\Drivers;
use App\Integrations\OAuth\Drivers\Contracts\OAuthDriver;
use Google\Service\Sheets;
use Laravel\Socialite\Contracts\User;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\GoogleProvider;
class OAuthGoogleDriver implements OAuthDriver
{
protected GoogleProvider $provider;
public function __construct()
{
$this->provider = Socialite::driver('google');
}
public function getRedirectUrl(): string
{
return $this->provider
->scopes([Sheets::DRIVE_FILE])
->stateless()
->with([
'access_type' => 'offline',
'prompt' => 'consent select_account'
])
->redirect()
->getTargetUrl();
}
public function getUser(): User
{
return $this->provider
->stateless()
->user();
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Integrations\OAuth;
use App\Integrations\OAuth\Drivers\Contracts\OAuthDriver;
use App\Integrations\OAuth\Drivers\OAuthGoogleDriver;
enum OAuthProviderService: string
{
case Google = 'google';
public function getDriver(): OAuthDriver
{
return match($this) {
self::Google => new OAuthGoogleDriver()
};
}
}