VOOZH about

URL: https://deepwiki.com/hypervel/auth/3.2-policies-and-abilities

⇱ Policies and Abilities | hypervel/auth | DeepWiki


Loading...
Menu

Policies and Abilities

Purpose and Scope

This document explains how to define authorization rules in the hypervel/auth system using abilities and policies. Abilities are simple callback-based rules for authorizing actions, while policies are class-based collections of authorization logic organized around a specific model or resource type.

For information about the Gate class that evaluates these rules, see 3.1. For information about how to use authorization in your application (checking permissions, middleware, etc.), see 3.1, 3.4, and 4.2.


Overview

The Gate supports two complementary approaches to defining authorization logic:

ApproachUse CaseDefinition MethodStorage Location
AbilitiesSimple, one-off authorization checksGate::define()Closures or callbacks stored in $abilities array
PoliciesModel-centric authorization with multiple related checksGate::policy()Class instances resolved from $policies mapping

Both approaches support the same authorization checking methods (allows(), denies(), check(), etc.) but differ in how the authorization logic is organized and resolved.

Sources: src/Access/Gate.php28-53


Abilities

What Are Abilities

Abilities are named authorization rules defined as closures or callbacks. Each ability is registered with a unique name (e.g., "update-post", "view-dashboard") and associated with a callable that determines whether a user can perform that action.

The Gate stores abilities in two internal arrays:

Defining Abilities

Using Closures

The most straightforward way to define an ability is with a closure:


The first parameter is always the user (nullable for guest support), followed by any arguments passed during the authorization check.

Using Class@Method Syntax

Abilities can reference class methods using string notation:


String callbacks are converted to closures via buildAbilityCallback() src/Access/Gate.php164-194

Implementation Details

The define() method handles multiple callback formats src/Access/Gate.php122-139:

  1. Array format [ClassName::class, 'method'] - Converted to 'ClassName@method'
  2. Callable format - Stored directly in $abilities
  3. String format 'ClassName@method' - Stored in $stringCallbacks and wrapped in a closure

Sources: src/Access/Gate.php122-139

Resource Abilities

The resource() method provides a convenient way to define standard CRUD abilities for a resource:


This single call is equivalent to defining five separate abilities src/Access/Gate.php144-159:

Ability NamePolicy MethodPurpose
post.viewAnyviewAny()List all resources
post.viewview()View a single resource
post.createcreate()Create a new resource
post.updateupdate()Update an existing resource
post.deletedelete()Delete a resource

You can customize the ability-to-method mapping by passing a third parameter:


Sources: src/Access/Gate.php144-159


Policies

What Are Policies

Policies are classes that organize authorization logic around a particular model or resource type. Instead of defining scattered abilities, you group related authorization rules into a single policy class.

For example, a PostPolicy might contain methods like view(), update(), delete(), each handling authorization for that specific action on Post models.

Registering Policies

Policies are registered by mapping a model class to a policy class src/Access/Gate.php199-204:


The mapping is stored in the $policies array src/Access/Gate.php51:


Sources: src/Access/Gate.php199-204

Policy Resolution

When the Gate needs a policy instance, it resolves it from the DI container src/Access/Gate.php519-522:


This allows policies to have dependencies injected through their constructors.

The getPolicyFor() method determines which policy to use for a given object src/Access/Gate.php493-512:

  1. Exact match - If the object's class is registered, use that policy
  2. Subclass match - If the object extends a registered class, use the parent's policy
  3. No match - Return null, fall back to abilities

Sources: src/Access/Gate.php493-522

Policy Methods

Policy methods follow these conventions:

Method Signatures


The first parameter is always the user. Additional parameters are the arguments passed to the authorization check.

Method Naming

The Gate converts ability names to method names using camel case src/Access/Gate.php599-602:























Ability NamePolicy Method
viewview()
update-postupdatePost()
delete-commentdeleteComment()

Sources: src/Access/Gate.php599-602

The Before Method

Policies can define a special before() method that runs before any other policy method src/Access/Gate.php562-571:


Behavior:

  • If before() returns non-null, that result is used and the specific method is skipped
  • If before() returns null, evaluation continues to the specific policy method
  • The before() method receives the ability name as a parameter

Sources: src/Access/Gate.php562-571 src/Access/Gate.php537-549


Authorization Resolution Flow

Resolution Priority

When the Gate checks an ability, it follows this resolution order src/Access/Gate.php463-485:


Key Points:

  1. Before callbacks are checked first and can short-circuit src/Access/Gate.php312-316
  2. Policies take precedence when checking abilities against model instances src/Access/Gate.php465-468
  3. String callbacks are checked before regular abilities src/Access/Gate.php471-477
  4. Regular abilities are the fallback src/Access/Gate.php479-482
  5. Empty closure returns when nothing matches src/Access/Gate.php484

Sources: src/Access/Gate.php463-485

Policy Method Resolution

When a policy is used, the Gate follows this execution flow src/Access/Gate.php533-554:


Sources: src/Access/Gate.php527-554 src/Access/Gate.php562-571


Guest Access Support

Both abilities and policies can support guest (unauthenticated) users by making the user parameter nullable. The Gate determines if a callback can be called with a null user through reflection src/Access/Gate.php338-398

Making Callbacks Guest-Friendly

For Closures


For Policy Methods


Guest Detection Logic

The Gate uses reflection to determine if a callback allows guests src/Access/Gate.php394-398:


A parameter allows guests if:

  1. It has a type hint and allows null (?Authenticatable)
  2. It has a default value and that default is null

If a callback doesn't allow guests and the user is null, the callback is skipped src/Access/Gate.php418-420

Sources: src/Access/Gate.php338-398


Abilities vs Policies Comparison















































AspectAbilitiesPolicies
OrganizationFlat, name-basedModel-centric classes
Best ForSimple, standalone checksRelated authorization rules for a model
RegistrationGate::define(name, callback)Gate::policy(Model::class, Policy::class)
ResolutionBy ability nameBy object class + ability name
Before HooksGlobal before() callbacksPer-policy before() methods
Dependency InjectionNot supported (closures)Supported (resolved from container)
Code ReuseLimitedHigh (methods in a class)

Sources: src/Access/Gate.php122-139 src/Access/Gate.php199-204 src/Access/Gate.php463-485


String Callback Execution

String callbacks defined with Class@method syntax are wrapped in a special closure that handles policy resolution and before hooks src/Access/Gate.php164-194:


This allows string callbacks to benefit from:

  • Dependency injection via the container
  • The policy's before() method
  • Proper user argument handling

Sources: src/Access/Gate.php164-194


Method Name Transformation

The Gate automatically transforms ability names to method names for policy resolution:


Examples:

AbilityTransformationPolicy Method
"view"Noneview()
"update"Noneupdate()
"view-any"Camel caseviewAny()
"create-draft"Camel casecreateDraft()
"admin-delete"Camel caseadminDelete()

This transformation is performed by formatAbilityToMethod() src/Access/Gate.php599-602 and is applied when:

Sources: src/Access/Gate.php599-602 src/Access/Gate.php529 src/Access/Gate.php551


Complete Authorization Check Flow

This diagram shows the complete flow when checking authorization with both abilities and policies in the system:


Key Decision Points:

  1. Before callbacks src/Access/Gate.php312-316 - Can short-circuit entire authorization
  2. Object detection src/Access/Gate.php465 - Determines if policy resolution is attempted
  3. Policy method existence src/Access/Gate.php529 - Verifies policy can handle ability
  4. Policy before method src/Access/Gate.php537-542 - Can short-circuit policy method
  5. Fallback chain src/Access/Gate.php471-484 - Tries string callbacks, then abilities, then empty closure

Sources: src/Access/Gate.php303-333 src/Access/Gate.php411-426 src/Access/Gate.php463-485 src/Access/Gate.php527-554