Implement webhook inspector
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\WebhookEndpoint;
|
||||
|
||||
class WebhookEndpointPolicy
|
||||
{
|
||||
public function viewAny(?User $user): bool
|
||||
{
|
||||
return $user !== null;
|
||||
}
|
||||
|
||||
public function view(?User $user, WebhookEndpoint $webhookEndpoint): bool
|
||||
{
|
||||
return $webhookEndpoint->is_public || $this->isOwner($user, $webhookEndpoint);
|
||||
}
|
||||
|
||||
public function create(?User $user): bool
|
||||
{
|
||||
return $user !== null;
|
||||
}
|
||||
|
||||
public function update(?User $user, WebhookEndpoint $webhookEndpoint): bool
|
||||
{
|
||||
return ! $webhookEndpoint->is_public && $this->isOwner($user, $webhookEndpoint);
|
||||
}
|
||||
|
||||
public function delete(?User $user, WebhookEndpoint $webhookEndpoint): bool
|
||||
{
|
||||
return ($webhookEndpoint->is_public && $webhookEndpoint->user_id === null)
|
||||
|| $this->isOwner($user, $webhookEndpoint);
|
||||
}
|
||||
|
||||
private function isOwner(?User $user, WebhookEndpoint $webhookEndpoint): bool
|
||||
{
|
||||
return $user !== null && (string) $webhookEndpoint->user_id === (string) $user->getKey();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\WebhookRequest;
|
||||
|
||||
class WebhookRequestPolicy
|
||||
{
|
||||
public function viewAny(?User $user): bool
|
||||
{
|
||||
return $user !== null;
|
||||
}
|
||||
|
||||
public function view(?User $user, WebhookRequest $webhookRequest): bool
|
||||
{
|
||||
return $this->canAccessEndpoint($user, $webhookRequest);
|
||||
}
|
||||
|
||||
public function create(?User $user): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(?User $user, WebhookRequest $webhookRequest): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function delete(?User $user, WebhookRequest $webhookRequest): bool
|
||||
{
|
||||
return $this->canAccessEndpoint($user, $webhookRequest);
|
||||
}
|
||||
|
||||
private function canAccessEndpoint(?User $user, WebhookRequest $webhookRequest): bool
|
||||
{
|
||||
$webhookRequest->loadMissing('endpoint');
|
||||
|
||||
return (new WebhookEndpointPolicy)->view($user, $webhookRequest->endpoint);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user