VOOZH about

URL: https://deepwiki.com/hypervel/auth/3.1-gate

⇱ Gate | hypervel/auth | DeepWiki


Loading...
Menu

Gate

The Gate class serves as the central authorization engine for the hypervel/auth package. It provides a unified interface for defining and evaluating authorization rules through abilities and policies, managing access control decisions across the application.

For information about authorization responses and exception handling, see Authorization Responses and Exceptions. For details about authorization events, see Authorization Events.

Core Architecture

The Gate implements a comprehensive authorization system built around abilities, policies, and callback mechanisms. It integrates with Hyperf's dependency injection container and provides flexible user resolution.


Architecture Overview

Sources: src/Access/Gate.php23-55

Dependency Injection and Factory

The Gate is registered in the dependency injection container through GateFactory, which handles instantiation and configuration:


Gate Factory and DI Registration

Sources: src/ConfigProvider.php22 src/Access/Gate.php47-55

Constructor and Initialization

The Gate constructor accepts core dependencies and initial configuration for the authorization system:

ParameterTypePurpose
containerContainerInterfaceDI container for resolving policy classes
userResolverClosureCallback to resolve the current user
abilitiesarrayPre-defined ability callbacks
policiesarrayClass-to-policy mappings
beforeCallbacksarrayGlobal before callbacks
afterCallbacksarrayGlobal after callbacks

Gate Initialization Parameters

Sources: src/Access/Gate.php47-55

Ability Management

The Gate provides multiple ways to define and manage authorization abilities:

Direct Ability Definition

The define() method registers authorization abilities with their corresponding callbacks:


Ability Definition Process

Sources: src/Access/Gate.php122-139

Resource-Based Abilities

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

Default AbilityMethodPurpose
viewAnyviewAnyList/index permissions
viewviewView single resource
createcreateCreate new resource
updateupdateUpdate existing resource
deletedeleteDelete resource

Sources: src/Access/Gate.php144-159

Ability Callback Resolution

The Gate builds ability callbacks that integrate with the policy system:


Ability Callback Execution

Sources: src/Access/Gate.php164-194

Policy System

The Gate integrates with policy classes to provide organized, class-based authorization logic:

Policy Registration

The policy() method maps model classes to their corresponding policy classes:


Policy Registration and Resolution

Sources: src/Access/Gate.php199-204 src/Access/Gate.php493-522

Policy Method Resolution

The Gate automatically formats ability names to policy method names and handles policy execution:


Policy Callback Resolution

Sources: src/Access/Gate.php527-555 src/Access/Gate.php599-602

Authorization Flow

The Gate provides multiple methods for performing authorization checks with different return types and behaviors:

Authorization Methods

MethodReturn TypeBehavior
allows()boolReturns true if authorized
denies()boolReturns true if denied
check()boolChecks all abilities (AND logic)
any()boolChecks any ability (OR logic)
none()boolReturns true if all denied
authorize()ResponseThrows exception if denied
inspect()ResponseReturns Response object
raw()mixedReturns raw callback result

Core Authorization Flow

Sources: src/Access/Gate.php229-333

On-Demand Authorization

The Gate provides allowIf() and denyIf() methods for conditional authorization checks:


On-Demand Authorization

Sources: src/Access/Gate.php78-115

Callback System

The Gate implements a comprehensive callback system with before and after hooks:

Before Callbacks

Before callbacks run before any authorization check and can override authorization decisions:


Before Callback Processing

Sources: src/Access/Gate.php209-213 src/Access/Gate.php415-426

After Callbacks

After callbacks run after authorization checks for logging, auditing, or result modification:


After Callback Processing

Sources: src/Access/Gate.php219-223 src/Access/Gate.php431-444

Guest User Handling

The Gate provides sophisticated guest user support through reflection-based parameter analysis:

Guest User Detection

The Gate determines if authorization callbacks can be called with guest users (null users) by examining method signatures:


Guest User Authorization Flow

Sources: src/Access/Gate.php338-398

Event System Integration

The Gate integrates with Hyperf's event system to provide authorization auditing:

GateEvaluated Event

After each authorization check, the Gate dispatches a GateEvaluated event for auditing purposes:


Authorization Event Dispatch

Sources: src/Access/Gate.php449-458

User Context Management

The Gate provides flexible user context management through user resolution and user-specific instances:

User Resolution

The Gate resolves the current user through a configurable closure:


User Context Resolution

Sources: src/Access/Gate.php607-627

Configuration and State Management

The Gate maintains several types of state and provides access to its configuration:

MethodReturnsPurpose
abilities()arrayGet all defined abilities
policies()arrayGet all policy mappings
has()boolCheck if ability exists
defaultDenialResponse()staticSet default denial response

Sources: src/Access/Gate.php60-71 src/Access/Gate.php632-653