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:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Forms\Integration;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Integration\FormIntegrationsRequest;
|
||||
use App\Http\Resources\FormIntegrationResource;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Integration\FormIntegration;
|
||||
|
||||
@@ -19,7 +20,12 @@ class FormIntegrationsController extends Controller
|
||||
$form = Form::findOrFail((int)$id);
|
||||
$this->authorize('view', $form);
|
||||
|
||||
return FormIntegration::where('form_id', $form->id)->get();
|
||||
$integrations = FormIntegration::query()
|
||||
->where('form_id', $form->id)
|
||||
->with('provider.user')
|
||||
->get();
|
||||
|
||||
return FormIntegrationResource::collection($integrations);
|
||||
}
|
||||
|
||||
public function create(FormIntegrationsRequest $request, string $id)
|
||||
@@ -27,15 +33,19 @@ class FormIntegrationsController extends Controller
|
||||
$form = Form::findOrFail((int)$id);
|
||||
$this->authorize('update', $form);
|
||||
|
||||
/** @var FormIntegration $formIntegration */
|
||||
$formIntegration = FormIntegration::create(
|
||||
array_merge([
|
||||
'form_id' => $form->id,
|
||||
], $request->toIntegrationData())
|
||||
);
|
||||
|
||||
$formIntegration->refresh();
|
||||
$formIntegration->load('provider.user');
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form Integration was created.',
|
||||
'form_integration' => $formIntegration
|
||||
'form_integration' => FormIntegrationResource::make($formIntegration)
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -46,10 +56,11 @@ class FormIntegrationsController extends Controller
|
||||
|
||||
$formIntegration = FormIntegration::findOrFail((int)$integrationid);
|
||||
$formIntegration->update($request->toIntegrationData());
|
||||
$formIntegration->load('provider.user');
|
||||
|
||||
return $this->success([
|
||||
'message' => 'Form Integration was updated.',
|
||||
'form_integration' => $formIntegration
|
||||
'form_integration' => FormIntegrationResource::make($formIntegration)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
64
app/Http/Controllers/OAuth/OAuthProviderController.php
Normal file
64
app/Http/Controllers/OAuth/OAuthProviderController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\OAuth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\OAuthProviderResource;
|
||||
use App\Integrations\OAuth\OAuthProviderService;
|
||||
use App\Models\OAuthProvider;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class OAuthProviderController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
/** @var \App\Models\User $user */
|
||||
$user = Auth::user();
|
||||
|
||||
$providers = $user->oauthProviders()->get();
|
||||
|
||||
return OAuthProviderResource::collection($providers);
|
||||
}
|
||||
|
||||
public function connect(Request $request, OAuthProviderService $service)
|
||||
{
|
||||
$userId = Auth::id();
|
||||
cache()->put("oauth-intention:{$userId}", $request->input('intention'), 60 * 5);
|
||||
|
||||
return response()->json([
|
||||
'url' => $service->getDriver()->getRedirectUrl(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function handleRedirect(OAuthProviderService $service)
|
||||
{
|
||||
$driverUser = $service->getDriver()->getUser();
|
||||
|
||||
$provider = OAuthProvider::query()
|
||||
->updateOrCreate(
|
||||
[
|
||||
'user_id' => Auth::id(),
|
||||
'provider' => $service,
|
||||
'provider_user_id' => $driverUser->getId(),
|
||||
],
|
||||
[
|
||||
'access_token' => $driverUser->token,
|
||||
'refresh_token' => $driverUser->refreshToken,
|
||||
'name' => $driverUser->getName(),
|
||||
'email' => $driverUser->getEmail(),
|
||||
]
|
||||
);
|
||||
|
||||
return OAuthProviderResource::make($provider);
|
||||
}
|
||||
|
||||
public function destroy(OAuthProvider $provider)
|
||||
{
|
||||
$this->authorize('delete', $provider);
|
||||
|
||||
$provider->delete();
|
||||
|
||||
return response()->json();
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,9 @@ class FormIntegrationsRequest extends FormRequest
|
||||
// Load integration class, and get rules
|
||||
$integration = FormIntegration::getIntegration($request->integration_id);
|
||||
if ($integration && isset($integration['file_name']) && class_exists(
|
||||
'App\Service\Forms\Integrations\\' . $integration['file_name']
|
||||
'App\Integrations\Handlers\\' . $integration['file_name']
|
||||
)) {
|
||||
$this->integrationClassName = 'App\Service\Forms\Integrations\\' . $integration['file_name'];
|
||||
$this->integrationClassName = 'App\Integrations\Handlers\\' . $integration['file_name'];
|
||||
$this->loadIntegrationRules();
|
||||
return;
|
||||
}
|
||||
@@ -40,9 +40,13 @@ class FormIntegrationsRequest extends FormRequest
|
||||
{
|
||||
return array_merge([
|
||||
'integration_id' => ['required', Rule::in(array_keys(FormIntegration::getAllIntegrations()))],
|
||||
'oauth_id' => [
|
||||
$this->isOAuthRequired() ? 'required' : 'nullable',
|
||||
Rule::exists('oauth_providers', 'id')
|
||||
],
|
||||
'settings' => 'present|array',
|
||||
'status' => 'required|boolean',
|
||||
'logic' => [new IntegrationLogicRule()]
|
||||
'logic' => [new IntegrationLogicRule()],
|
||||
], $this->integrationRules);
|
||||
}
|
||||
|
||||
@@ -53,16 +57,24 @@ class FormIntegrationsRequest extends FormRequest
|
||||
*/
|
||||
public function attributes()
|
||||
{
|
||||
$attributes = $this->integrationClassName::getValidationAttributes();
|
||||
|
||||
$fields = [];
|
||||
foreach ($this->rules() as $key => $value) {
|
||||
$fields[$key] = Str::of($key)
|
||||
$fields[$key] = $attributes[$key] ?? Str::of($key)
|
||||
->replace('settings.', '')
|
||||
->headline();
|
||||
->headline()
|
||||
->toString();
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
protected function isOAuthRequired(): bool
|
||||
{
|
||||
return $this->integrationClassName::isOAuthRequired();
|
||||
}
|
||||
|
||||
private function loadIntegrationRules()
|
||||
{
|
||||
foreach ($this->integrationClassName::getValidationRules() as $key => $value) {
|
||||
@@ -78,7 +90,8 @@ class FormIntegrationsRequest extends FormRequest
|
||||
)) ? FormIntegration::STATUS_ACTIVE : FormIntegration::STATUS_INACTIVE,
|
||||
'integration_id' => $this->validated('integration_id'),
|
||||
'data' => $this->validated('settings') ?? [],
|
||||
'logic' => $this->validated('logic') ?? []
|
||||
'logic' => $this->validated('logic') ?? [],
|
||||
'oauth_id' => $this->validated('oauth_id'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
19
app/Http/Resources/FormIntegrationResource.php
Normal file
19
app/Http/Resources/FormIntegrationResource.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @property \App\Models\Integration\FormIntegration $resource
|
||||
*/
|
||||
class FormIntegrationResource extends JsonResource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
...parent::toArray($request),
|
||||
'provider' => OAuthProviderResource::make($this->resource->provider),
|
||||
];
|
||||
}
|
||||
}
|
||||
37
app/Http/Resources/OAuthProviderResource.php
Normal file
37
app/Http/Resources/OAuthProviderResource.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
/**
|
||||
* @property \App\Models\OAuthProvider $resource
|
||||
*/
|
||||
class OAuthProviderResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$userId = Auth::id();
|
||||
$intention = cache()->get("oauth-intention:{$userId}");
|
||||
|
||||
return [
|
||||
'id' => $this->resource->id,
|
||||
'provider' => $this->resource->provider,
|
||||
'name' => $this->resource->name,
|
||||
'email' => $this->resource->email,
|
||||
'intention' => $intention,
|
||||
'user' => $this->whenLoaded(
|
||||
'user',
|
||||
fn () => OAuthProviderUserResource::make($this->resource->user),
|
||||
null,
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Http/Resources/OAuthProviderUserResource.php
Normal file
19
app/Http/Resources/OAuthProviderUserResource.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @property \App\Models\User $resource
|
||||
*/
|
||||
class OAuthProviderUserResource extends JsonResource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'name' => $this->resource->name,
|
||||
'email' => $this->resource->email,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user