File: /home/xedaptot/ai.naniguide.com/app/Policies/NotificationPolicy.php
<?php
namespace App\Policies;
use App\Model\Admin;
use App\Model\AppNotification;
use App\Model\Customer;
use App\Model\User;
use App\Notifications\AppNotification as Event;
/**
* Authorizes who can act (mark read/unread/dismiss) on a notification row.
*
* - direct: only the named recipient (Admin or Customer matching
* notifiable_type/id) may act
* - admins: any admin may act (first-come-first-handle; row is shared)
* - customers: any customer may act
* - everyone: any authenticated user may act
*
* The default switch enforces the no-fall-through rule (project HARD RULE):
* unknown audiences throw rather than silently allowing or denying.
*/
class NotificationPolicy
{
public function act(User $user, AppNotification $notification): bool
{
return match ($notification->audience) {
Event::AUDIENCE_DIRECT => $this->isNamedRecipient($user, $notification),
Event::AUDIENCE_ADMINS => $user->admin !== null,
Event::AUDIENCE_CUSTOMERS => $user->customer !== null,
Event::AUDIENCE_EVERYONE => true,
default => throw new \LogicException(
"Unhandled notification audience in NotificationPolicy::act: {$notification->audience}"
),
};
}
private function isNamedRecipient(User $user, AppNotification $n): bool
{
return match ($n->notifiable_type) {
(new Admin)->getMorphClass() => $user->admin && $user->admin->id === (int) $n->notifiable_id,
(new Customer)->getMorphClass() => $user->customer && $user->customer->id === (int) $n->notifiable_id,
default => false,
};
}
}