Authorize context access
Implement NotificationContextAuthorizer when access to a community, team, organization, workspace, or another context depends on application-specific rules:
use NotificationCompass\Contracts\NotificationContextAuthorizer;
use NotificationCompass\ValueObjects\NotificationContext;
final class ApplicationNotificationContextAuthorizer implements NotificationContextAuthorizer
{
public function authorize(object $notifiable, NotificationContext $context): bool
{
return match ($context->type) {
'team' => $notifiable->teams()->whereKey($context->id)->exists(),
'organization' => $notifiable->organizations()->whereKey($context->id)->exists(),
default => false,
};
}
}
Bind the implementation in an application service provider:
use App\Support\ApplicationNotificationContextAuthorizer;
use NotificationCompass\Contracts\NotificationContextAuthorizer;
$this->app->bind(
NotificationContextAuthorizer::class,
ApplicationNotificationContextAuthorizer::class,
);
The package passes the notifiable and the NotificationContext to the authorizer. The default authorizer allows every context. A custom authorizer that returns false rejects preference access and prevents notification delivery for that context.
Authorize policy administrators
Implement NotificationContextPolicyAuthorizer separately when an administrator manages policies for an entire context:
use NotificationCompass\Contracts\NotificationContextPolicyAuthorizer;
use NotificationCompass\ValueObjects\NotificationContext;
final class ApplicationNotificationContextPolicyAuthorizer implements NotificationContextPolicyAuthorizer
{
public function authorize(object $administrator, NotificationContext $context): bool
{
return match ($context->type) {
'organization' => $administrator->isOrganizationAdmin($context->id),
'team' => $administrator->isTeamAdmin($context->id),
default => false,
};
}
}
Bind both contracts independently. The membership authorizer and the policy authorizer can use different application rules:
use App\Support\ApplicationNotificationContextPolicyAuthorizer;
use NotificationCompass\Contracts\NotificationContextPolicyAuthorizer;
$this->app->bind(
NotificationContextPolicyAuthorizer::class,
ApplicationNotificationContextPolicyAuthorizer::class,
);
NotificationContextPreferenceManager calls the policy authorizer before every read, inspection, write, and reset. Set notificationcompass.authorization.strict to true when a missing binding must deny contextual access instead of using the permissive defaults.