Action Policies
This page covers Pyle action policies only. Laravel model policies are resolved by Laravel Gate and policy discovery; do not register model policies in pyle.overrides.policies.
Steps to add a policy to an action
For a framework action policy override, create an app policy that extends the framework policy and register it in
pyle.overrides.policies. For an app-only action policy with no framework counterpart, place an instantiableActionPolicysubclass underapp/Policies/Actions, for exampleApp\Policies\Actions\Order\CreateOrderPolicy.Override the
$codesvariable to create the permissions you want. The format is project:model:name, for examplestorefront:order:create. Each permission is an array so it can carry options such asdefaultandalias.defaultmeans the permission is granted to every role when permissions are seeded.php'storefront:order:create' => [ 'alias' => 'Create Order', 'default' => true, ]Run
php artisan framework:seed-roles-and-permissionsso the permission exists in your database. The command seeds from the action policy manifest, which includes direct framework policies, configured overrides, and app-only policies.On the action that needs this permission, implement
AuthorizableAction. The interface includes:phppublic function getRequiredPermissions(): array; public function validateAuthorization($context = null): void;A finalized action would look like this.
phpuse CBOX\Framework\Base\BaseAction; use CBOX\Framework\Contracts\AuthorizableAction; use CBOX\Framework\Policies\Actions\Order\CreateOrderPolicy; class CreateOrder extends BaseAction implements AuthorizableAction { public function handle($fields = []) { $this->validateAuthorization(); } public function validateAuthorization($context = null): void { \CBOX::can(policyCollection: $this->getRequiredPermissions()); } public function getRequiredPermissions(): array { return [CreateOrderPolicy::class]; } }
Framework action policy overrides are configured like this:
'overrides' => [
'policies' => [
\CBOX\Framework\Policies\Actions\Order\UpdateOrderPolicy::class => \App\Policies\Actions\Order\UpdateOrderPolicy::class,
],
],Use direct framework action policy class strings in new PHP code. The manifest resolves those strings to configured app overrides. App-only policies under app/Policies/Actions are discovered by the manifest for permission seeding and legacy alias checks. framework:seed-roles-and-permissions seeds from ActionPolicyManifest::all(), not by iterating generated bindings. CBOX::policy(...) is a deprecated compatibility helper backed by the manifest only and should not be added to new runtime code.
Specific features:
In a Blade component,
@CBOXcanand@unlessCBOXcanaccept direct policy class strings, short aliases with or without theActions/prefix, nested policy arrays,context, andany: true. Short aliases such asOrder/UpdateOrderPolicyare still preferred in Blade. Missing aliases throw before authorization; authorization failures render as false.php@CBOXcan('Order/UpdateOrderPolicy') <div class="px-6 py-4"> </div> @endCBOXcanIf you need a specific implementation of how you want to validate the permission / the authorization of the policy you should override the authorize function in your policy and write your custom logic there. In practice it looks like this:
phpprotected static function authorize($context): bool { $user = \CBOX::getCurrentUser(); if ($user->can('admin:comment:update') && $context->author_id == $user->id) { return true; } return false; }If your policy is for a storefront-facing action that allows guests, override the
$authRequiredvariable. When it isfalse, unauthenticated users skip thepreAuthorizeandauthorizechecks.phpprotected static bool $authRequired = true;