41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?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();
|
|
}
|
|
}
|