VOOZH about

URL: https://deepwiki.com/hypervel/devtool/10-authorization-generators

⇱ Authorization Generators | hypervel/devtool | DeepWiki


Loading...
Menu

Authorization Generators

Purpose and Scope

This document covers the policy generator command (make:policy) provided by the Hypervel/Devtool package. Policies are authorization classes that organize authorization logic around a particular model or resource, containing methods that determine whether a user can perform specific actions.

For event-based model lifecycle hooks, see Observer Generation. For general code generation patterns, see Code Generation System.


Policy Generator Overview

The PolicyCommand class generates authorization policy classes that contain methods for determining user permissions. The generator supports two modes:

  1. Model-based policies - Generate policies with standard CRUD authorization methods tied to a specific model
  2. Plain policies - Generate empty policy classes for custom authorization logic

The command is registered as make:policy and extends the GeneratorCommand base class from Hyperf.


Sources: src/Generator/PolicyCommand.php1-89


Command Options and Flags

The make:policy command accepts the following options:

OptionShortTypeDescriptionDefault Behavior
--model-mOptionalThe model class that the policy applies toInfers model name from policy class name
--guard-gOptionalThe authentication guard configuration to useUses auth.defaults.guard from configuration

Usage Examples


Sources: src/Generator/PolicyCommand.php82-88


Generated Policy Structure

Model-Based Policy (Full)

When the --model option is provided, the generator creates a policy class with seven standard authorization methods:


Each method receives the authenticated user as the first parameter. Methods that operate on a specific model instance receive the model as the second parameter.

MethodParametersReturn TypePurpose
viewAnyUser $userboolDetermine if user can view any models
viewUser $user, Model $modelboolDetermine if user can view a specific model
createUser $userboolDetermine if user can create models
updateUser $user, Model $modelboolDetermine if user can update a specific model
deleteUser $user, Model $modelboolDetermine if user can delete a specific model
restoreUser $user, Model $modelboolDetermine if user can restore a soft-deleted model
forceDeleteUser $user, Model $modelboolDetermine if user can permanently delete a model

All methods return false by default, requiring explicit authorization logic to be added.

Sources: src/Generator/stubs/policy.stub1-67

Plain Policy

When no --model option is provided, the generator creates a minimal policy class:

  • Contains only a constructor method
  • Imports the user model namespace
  • Suitable for policies that don't follow standard CRUD patterns

Sources: src/Generator/stubs/policy.plain.stub1-18


Placeholder Replacement System

The policy generator performs complex placeholder replacement to inject model and user information into the generated class:


Model Name Inference

If the --model option is not provided, the generator attempts to infer the model name from the policy class name:

  1. Extract the final segment of the policy class namespace path
  2. Remove the "Policy" suffix using Str::before($className, 'Policy')
  3. Capitalize the result using Str::ucfirst()

For example, PostPolicy infers the model name Post.

Sources: src/Generator/PolicyCommand.php31-38

User Model Resolution

The generator resolves the user model class through the authentication configuration chain:


The method userProviderModel() implements this resolution logic:

  1. Determine the authentication guard (from --guard option or default)
  2. Lookup the provider for that guard in auth.guards.{guard}.provider
  3. Retrieve the model class from auth.providers.{provider}.model
  4. Fall back to App\Models\User if no model is configured

If the specified guard is not defined, a LogicException is thrown with a descriptive error message.

Sources: src/Generator/PolicyCommand.php54-66


Stub Selection Logic

The generator selects between two stub templates based on the presence of the --model option:


The stub path resolution follows this priority:

  1. Custom stub path from configuration: getConfig()['stub']
  2. Default stubs based on --model option:
    • With --model: src/Generator/stubs/policy.stub
    • Without --model: src/Generator/stubs/policy.plain.stub

Sources: src/Generator/PolicyCommand.php68-75


Configuration and Customization

Default Namespace

Generated policies are placed in the App\Policies namespace by default. This can be overridden through configuration:


The getDefaultNamespace() method checks configuration before returning the default:

Sources: src/Generator/PolicyCommand.php77-80

Model Namespace Configuration

The model_namespace configuration key determines where the generator looks for model classes. The default is App\Models, but this can be customized to support different project structures:

Sources: src/Generator/PolicyCommand.php40

Custom Stub Templates

Custom stub templates can be specified through the configuration system. This allows teams to:

  • Add custom methods to all generated policies
  • Include additional imports or traits
  • Modify the default authorization logic
  • Add documentation or code style preferences

Sources: src/Generator/PolicyCommand.php70


Code-Level Integration

Class Hierarchy


The PolicyCommand class extends GeneratorCommand and overrides four key methods to implement policy-specific generation logic:

MethodPurposeLines
replaceClass()Performs model and user model placeholder replacement31-52
getStub()Selects appropriate stub template based on options68-75
getDefaultNamespace()Returns the default policy namespace77-80
getOptions()Defines command-line options82-88
userProviderModel()Resolves user model from auth configuration54-66

Sources: src/Generator/PolicyCommand.php14-89


Placeholder Reference

The following placeholders are replaced during policy generation:

PlaceholderExample ValueDescription
%NAMESPACE%App\PoliciesThe namespace for the policy class
%CLASS%PostPolicyThe policy class name
%NAMESPACED_MODEL%App\Models\PostFully qualified model class name
%NAMESPACED_USER_MODEL%App\Models\UserFully qualified user model class name
%USER%UserUser model class basename
%MODEL%PostTarget model class basename
%MODEL_VARIABLE%postCamelcase variable name for model instance

These placeholders are replaced during the replaceClass() method execution, which first calls the parent implementation for standard placeholders (%NAMESPACE%, %CLASS%) and then performs policy-specific replacements.

Sources: src/Generator/PolicyCommand.php47-51 src/Generator/stubs/policy.stub5-23