1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Controllers/WebhookController.php

500 lines
18 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
2020-07-06 13:22:36 +02:00
use App\Factory\WebhookFactory;
use App\Filters\WebhookFilters;
use App\Http\Requests\Webhook\CreateWebhookRequest;
use App\Http\Requests\Webhook\DestroyWebhookRequest;
use App\Http\Requests\Webhook\EditWebhookRequest;
use App\Http\Requests\Webhook\ShowWebhookRequest;
use App\Http\Requests\Webhook\StoreWebhookRequest;
use App\Http\Requests\Webhook\UpdateWebhookRequest;
use App\Models\Webhook;
use App\Repositories\BaseRepository;
2020-07-06 13:22:36 +02:00
use App\Transformers\WebhookTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Response;
2020-07-06 13:22:36 +02:00
class WebhookController extends BaseController
{
use MakesHash;
2020-07-06 13:22:36 +02:00
protected $entity_type = Webhook::class;
2020-07-06 13:22:36 +02:00
protected $entity_transformer = WebhookTransformer::class;
public $base_repo;
public function __construct(BaseRepository $base_repo)
{
parent::__construct();
$this->base_repo = $base_repo;
}
/**
2020-10-28 11:10:49 +01:00
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks",
* operationId="getWebhooks",
* tags={"webhooks"},
* summary="Gets a list of Webhooks",
* description="Lists Webhooks, search and filters allow fine grained lists to be generated.
*
2020-07-06 13:22:36 +02:00
* Query parameters can be added to performed more fine grained filtering of the Webhooks, these are handled by the WebhookFilters class which defines the methods available",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="A list of Webhooks",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
2020-10-28 11:10:49 +01:00
* @param WebhookFilters $filters
* @return Response|mixed
*/
2020-07-06 13:22:36 +02:00
public function index(WebhookFilters $filters)
{
2020-07-06 13:22:36 +02:00
$webhooks = Webhook::filter($filters);
2020-07-06 13:22:36 +02:00
return $this->listResponse($webhooks);
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/{id}",
* operationId="showWebhook",
* tags={"webhooks"},
* summary="Shows a Webhook",
* description="Displays a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function show(ShowWebhookRequest $request, Webhook $webhook)
{
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/{id}/edit",
* operationId="editWebhook",
* tags={"webhooks"},
* summary="Shows a Webhook for editting",
* description="Displays a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function edit(EditWebhookRequest $request, Webhook $webhook)
{
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
*
* @OA\Put(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/{id}",
* operationId="updateWebhook",
* tags={"webhooks"},
* summary="Updates a Webhook",
* description="Handles the updating of a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function update(UpdateWebhookRequest $request, Webhook $webhook)
{
2020-07-06 13:22:36 +02:00
if ($request->entityIsDeleted($webhook)) {
return $request->disallowUpdate();
}
2020-07-06 13:22:36 +02:00
$webhook->fill($request->all());
$webhook->save();
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateWebhookRequest $request
* @return Response
*
*
*
* @OA\Get(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/create",
* operationId="getWebhooksCreate",
* tags={"webhooks"},
* summary="Gets a new blank Webhook object",
* description="Returns a blank object with default values",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="A blank Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function create(CreateWebhookRequest $request)
{
2020-07-06 13:22:36 +02:00
$webhook = WebhookFactory::create(auth()->user()->company()->id, auth()->user()->id);
$webhook->fill($request->all());
$webhook->save();
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreWebhookRequest $request
* @return Response
*
*
*
* @OA\Post(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks",
* operationId="storeWebhook",
* tags={"webhooks"},
* summary="Adds a Webhook",
* description="Adds an Webhook to a company",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="Returns the saved Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function store(StoreWebhookRequest $request)
{
2020-07-06 13:22:36 +02:00
$event_id = $request->input('event_id');
$target_url = $request->input('target_url');
if (! in_array($event_id, Webhook::$valid_events)) {
return response()->json('Invalid event', 400);
2020-07-06 13:22:36 +02:00
}
$webhook = new Webhook;
$webhook->company_id = auth()->user()->company()->id;
$webhook->user_id = auth()->user()->id;
$webhook->event_id = $event_id;
$webhook->target_url = $target_url;
2021-04-08 23:57:01 +02:00
$webhook->fill($request->all());
2020-07-06 13:22:36 +02:00
$webhook->save();
if (! $webhook->id) {
2021-01-25 01:57:49 +01:00
return response()->json(ctrans('texts.create_webhook_failure'), 400);
2020-07-06 13:22:36 +02:00
}
return $this->itemResponse($webhook);
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyWebhookRequest $request
* @param Webhook $webhook
* @return Response
*
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
* @OA\Delete(
2020-11-06 05:43:10 +01:00
* path="/api/v1/webhooks/{id}",
2020-07-06 13:22:36 +02:00
* operationId="deleteWebhook",
* tags={"Webhooks"},
* summary="Deletes a Webhook",
* description="Handles the deletion of a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
2020-07-06 13:22:36 +02:00
* description="The Webhook Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2020-07-06 13:22:36 +02:00
public function destroy(DestroyWebhookRequest $request, Webhook $webhook)
{
//may not need these destroy routes as we are using actions to 'archive/delete'
2020-07-06 13:22:36 +02:00
$webhook->delete();
2020-07-06 13:22:36 +02:00
return $this->itemResponse($webhook);
}
/**
* Perform bulk actions on the list view.
*
2020-10-28 11:10:49 +01:00
* @return Response
*
*
* @OA\Post(
2020-07-06 13:22:36 +02:00
* path="/api/v1/webhooks/bulk",
* operationId="bulkWebhooks",
* tags={"webhooks"},
* summary="Performs bulk actions on an array of Webhooks",
* description="",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/index"),
* @OA\RequestBody(
* description="User credentials",
* required=true,
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="array",
* @OA\Items(
* type="integer",
* description="Array of hashed IDs to be bulk 'actioned",
* example="[0,1,2,3]",
* ),
* )
* )
* ),
* @OA\Response(
* response=200,
2020-07-06 13:22:36 +02:00
* description="The Webhook User response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
2020-07-06 13:22:36 +02:00
* @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function bulk()
{
$action = request()->input('action');
$ids = request()->input('ids');
2020-07-06 13:22:36 +02:00
$webhooks = Webhook::withTrashed()->find($this->transformKeys($ids));
2020-07-06 13:22:36 +02:00
$webhooks->each(function ($webhook, $key) use ($action) {
if (auth()->user()->can('edit', $webhook)) {
$this->base_repo->{$action}($webhook);
}
});
2020-07-06 13:22:36 +02:00
return $this->listResponse(Webhook::withTrashed()->whereIn('id', $this->transformKeys($ids)));
}
}