How Do I

Authorize context access

Restrict contextual notification preferences to authorized members.

Implement NotificationContextAuthorizer when access to a community, team, organization, workspace, or another context depends on application-specific rules:

app/Support/ApplicationNotificationContextAuthorizer.php
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:

app/Providers/AppServiceProvider.php
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:

app/Support/ApplicationNotificationContextPolicyAuthorizer.php
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:

app/Providers/AppServiceProvider.php
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.

The authorizer must enforce the application's real membership and permission rules. Do not authorize access from the context type and identifier alone.