Webhook API

This commit is contained in:
James Cole
2021-03-07 12:13:22 +01:00
parent c4882231d1
commit ba163f82d1
19 changed files with 800 additions and 90 deletions

View File

@@ -128,11 +128,11 @@ class Webhook extends Model
public static function routeBinder(string $value): Webhook
{
if (auth()->check()) {
$budgetId = (int)$value;
$webhookId = (int)$value;
/** @var User $user */
$user = auth()->user();
/** @var Webhook $webhook */
$webhook = $user->webhooks()->find($budgetId);
$webhook = $user->webhooks()->find($webhookId);
if (null !== $webhook) {
return $webhook;
}

View File

@@ -44,8 +44,11 @@ declare(strict_types=1);
namespace FireflyIII\Models;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class WebhookAttempt
@@ -74,6 +77,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
*/
class WebhookAttempt extends Model
{
use SoftDeletes;
/**
* @codeCoverageIgnore
* @return BelongsTo
@@ -82,4 +86,29 @@ class WebhookAttempt extends Model
{
return $this->belongsTo(WebhookMessage::class);
}
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
* @param string $value
*
* @return WebhookAttempt
* @throws NotFoundHttpException
*/
public static function routeBinder(string $value): WebhookAttempt
{
if (auth()->check()) {
$attemptId = (int)$value;
/** @var User $user */
$user = auth()->user();
/** @var WebhookAttempt $attempt */
$attempt = self::find($attemptId);
if (null !== $attempt) {
if($attempt->webhookMessage->webhook->user_id === $user->id) {
return $attempt;
}
}
}
throw new NotFoundHttpException;
}
}

View File

@@ -44,9 +44,11 @@ declare(strict_types=1);
namespace FireflyIII\Models;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* FireflyIII\Models\WebhookMessage
@@ -93,6 +95,31 @@ class WebhookMessage extends Model
'logs' => 'json',
];
/**
* Route binder. Converts the key in the URL to the specified object (or throw 404).
*
* @param string $value
*
* @return WebhookMessage
* @throws NotFoundHttpException
*/
public static function routeBinder(string $value): WebhookMessage
{
if (auth()->check()) {
$messageId = (int)$value;
/** @var User $user */
$user = auth()->user();
/** @var WebhookMessage $message */
$message = self::find($messageId);
if (null !== $message) {
if($message->webhook->user_id === $user->id) {
return $message;
}
}
}
throw new NotFoundHttpException;
}
/**
* @codeCoverageIgnore
* @return BelongsTo