2021-03-07 12:13:22 +01:00
< ? php
2024-11-25 04:18:55 +01:00
2021-03-07 12:13:22 +01:00
/*
* AttemptController . php
* Copyright ( c ) 2021 james @ firefly - iii . org
*
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2021-03-28 11:39:26 +02:00
declare ( strict_types = 1 );
2021-03-07 12:13:22 +01:00
2021-03-28 11:39:26 +02:00
namespace FireflyIII\Api\V1\Controllers\Webhook ;
2021-03-07 12:13:22 +01:00
use FireflyIII\Api\V1\Controllers\Controller ;
use FireflyIII\Exceptions\FireflyException ;
use FireflyIII\Models\Webhook ;
use FireflyIII\Models\WebhookAttempt ;
use FireflyIII\Models\WebhookMessage ;
use FireflyIII\Repositories\Webhook\WebhookRepositoryInterface ;
use FireflyIII\Transformers\WebhookAttemptTransformer ;
use Illuminate\Http\JsonResponse ;
use Illuminate\Pagination\LengthAwarePaginator ;
2023-12-29 08:19:04 +01:00
use Illuminate\Support\Facades\Log ;
2021-03-07 12:13:22 +01:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter ;
use League\Fractal\Resource\Collection as FractalCollection ;
use League\Fractal\Resource\Item ;
2023-12-29 08:42:03 +01:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException ;
2021-03-07 12:13:22 +01:00
/**
* Class AttemptController
*/
class AttemptController extends Controller
{
2023-12-02 12:56:48 +01:00
public const string RESOURCE_KEY = 'webhook_attempts' ;
2021-03-21 09:15:40 +01:00
private WebhookRepositoryInterface $repository ;
2021-03-07 12:13:22 +01:00
public function __construct ()
{
parent :: __construct ();
$this -> middleware (
function ( $request , $next ) {
$this -> repository = app ( WebhookRepositoryInterface :: class );
2021-04-06 08:51:27 +02:00
$this -> repository -> setUser ( auth () -> user ());
2021-03-07 12:13:22 +01:00
return $next ( $request );
}
);
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at :
2023-02-12 06:53:36 +01:00
* https :// api - docs . firefly - iii . org / ? urls . primaryName = 2.0 . 0 % 20 ( v1 ) #/webhooks/getWebhookMessageAttempts
2021-09-19 17:22:16 +02:00
*
2021-05-24 08:50:17 +02:00
* @ throws FireflyException
2021-03-07 12:13:22 +01:00
*/
public function index ( Webhook $webhook , WebhookMessage $message ) : JsonResponse
{
if ( $message -> webhook_id !== $webhook -> id ) {
2023-01-21 12:21:06 +01:00
throw new FireflyException ( '200040: Webhook and webhook message are no match' );
2021-03-07 12:13:22 +01:00
}
2024-02-22 01:29:01 +01:00
if ( false === config ( 'firefly.allow_webhooks' )) {
2024-01-09 20:48:17 +01:00
Log :: channel ( 'audit' ) -> warning ( sprintf ( 'User lists webhook attempts of webhook #%d and message #%d, but webhooks are DISABLED.' , $webhook -> id , $message -> id ));
2023-12-29 20:25:32 +01:00
2023-12-29 08:42:03 +01:00
throw new NotFoundHttpException ( 'Webhooks are not enabled.' );
}
2023-12-29 08:19:04 +01:00
Log :: channel ( 'audit' ) -> info ( sprintf ( 'User lists webhook attempts of webhook #%d and message #%d.' , $webhook -> id , $message -> id ));
2023-12-29 08:42:03 +01:00
2024-01-01 14:41:31 +01:00
$manager = $this -> getManager ();
$pageSize = $this -> parameters -> get ( 'limit' );
$collection = $this -> repository -> getAttempts ( $message );
$count = $collection -> count ();
$attempts = $collection -> slice (( $this -> parameters -> get ( 'page' ) - 1 ) * $pageSize , $pageSize );
2021-03-07 12:13:22 +01:00
// make paginator:
2024-01-01 14:41:31 +01:00
$paginator = new LengthAwarePaginator ( $attempts , $count , $pageSize , $this -> parameters -> get ( 'page' ));
2023-12-20 19:35:52 +01:00
$paginator -> setPath ( route ( 'api.v1.webhooks.attempts.index' , [ $webhook -> id , $message -> id ]) . $this -> buildParams ());
2021-03-07 12:13:22 +01:00
/** @var WebhookAttemptTransformer $transformer */
$transformer = app ( WebhookAttemptTransformer :: class );
$transformer -> setParameters ( $this -> parameters );
2024-01-01 14:41:31 +01:00
$resource = new FractalCollection ( $attempts , $transformer , 'webhook_attempts' );
2021-03-07 12:13:22 +01:00
$resource -> setPaginator ( new IlluminatePaginatorAdapter ( $paginator ));
return response () -> json ( $manager -> createData ( $resource ) -> toArray ()) -> header ( 'Content-Type' , self :: CONTENT_TYPE );
}
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at :
2023-02-12 06:53:36 +01:00
* https :// api - docs . firefly - iii . org / ? urls . primaryName = 2.0 . 0 % 20 ( v1 ) #/webhooks/getSingleWebhookMessageAttempt
2021-09-19 17:22:16 +02:00
*
2021-03-07 12:13:22 +01:00
* Show single instance .
*
* @ throws FireflyException
*/
public function show ( Webhook $webhook , WebhookMessage $message , WebhookAttempt $attempt ) : JsonResponse
{
2021-03-21 09:15:40 +01:00
if ( $message -> webhook_id !== $webhook -> id ) {
2023-01-21 12:21:06 +01:00
throw new FireflyException ( '200040: Webhook and webhook message are no match' );
2021-03-07 12:13:22 +01:00
}
2021-03-21 09:15:40 +01:00
if ( $attempt -> webhook_message_id !== $message -> id ) {
2023-01-21 12:21:06 +01:00
throw new FireflyException ( '200041: Webhook message and webhook attempt are no match' );
2021-03-07 12:13:22 +01:00
}
2024-02-22 01:29:01 +01:00
if ( false === config ( 'firefly.allow_webhooks' )) {
2024-01-09 20:48:17 +01:00
Log :: channel ( 'audit' ) -> warning ( sprintf ( 'User views single webhook attempt #%d of webhook #%d and message #%d, but webhooks are DISABLED' , $attempt -> id , $webhook -> id , $message -> id ));
2023-12-29 20:25:32 +01:00
2023-12-29 08:42:03 +01:00
throw new NotFoundHttpException ( 'Webhooks are not enabled.' );
}
Log :: channel ( 'audit' ) -> info ( sprintf ( 'User views single webhook attempt #%d of webhook #%d and message #%d.' , $attempt -> id , $webhook -> id , $message -> id ));
2024-01-01 14:41:31 +01:00
$manager = $this -> getManager ();
2021-03-07 12:13:22 +01:00
/** @var WebhookAttemptTransformer $transformer */
$transformer = app ( WebhookAttemptTransformer :: class );
$transformer -> setParameters ( $this -> parameters );
2024-01-01 14:41:31 +01:00
$resource = new Item ( $attempt , $transformer , self :: RESOURCE_KEY );
2021-03-07 12:13:22 +01:00
return response () -> json ( $manager -> createData ( $resource ) -> toArray ()) -> header ( 'Content-Type' , self :: CONTENT_TYPE );
}
2021-03-28 11:39:26 +02:00
}