Skip to content

Generate Policies Command

The mm:generate-policies command is used to generate policy classes for models in specific modules.

Usage

bash
php artisan mm:generate-policies [options]

Options

OptionDescription
--module=MODULEThe specific module to generate policies for

Description

This command scans the selected modules for models and generates corresponding policy classes. Policies are critical for authorization in Laravel applications, allowing fine-grained control over user actions.

When executed, this command performs the following actions:

  1. Scan Modules: Scans the enabled modules (or a specific module if provided) for models.

  2. Select Models: For each module with models, displays all available models and allows you to choose which ones to generate policies for. You can select individual models or choose "All Models".

  3. Generate Policies: Creates policy files for each selected model in the app/Policies/{ModuleName}/ directory.

Examples

Interactive Mode

bash
$ php artisan mm:generate-policies
Scanning modules for models...

Select models to generate policies for in Blog module:
> All Models
> Post
> Comment
> Category

Generated policy for Post: App\Policies\Blog\PostPolicy
Generated policy for Comment: App\Policies\Blog\CommentPolicy
Generated policy for Category: App\Policies\Blog\CategoryPolicy

Policy generation completed!

With Module Option

bash
$ php artisan mm:generate-policies --module=Blog
Scanning modules for models...

Select models to generate policies for in Blog module:
> All Models
> Post
> Comment
> Category

Generated policy for Post: App\Policies\Blog\PostPolicy
Generated policy for Comment: App\Policies\Blog\CommentPolicy
Generated policy for Category: App\Policies\Blog\CategoryPolicy

Policy generation completed!

Generated Policy Structure

Each generated policy includes methods for standard CRUD operations and more:

  • viewAny - Check if the user can view a list of resources
  • view - Check if the user can view a specific resource
  • viewRow - Filter query results based on user permissions
  • update - Check if the user can update a specific resource
  • updateAny - Check if the user can update any resource
  • create - Check if the user can create resources
  • delete - Check if the user can delete a specific resource
  • deleteAny - Check if the user can delete any resource
  • restore - Check if the user can restore a soft-deleted resource
  • forceDelete - Check if the user can permanently delete a resource
  • reorder - Check if the user can reorder resources

Notes

  • Policies are automatically discovered by the Module Manager through a custom policy discovery mechanism.
  • The generated policies include all necessary methods for Filament resource authorization.
  • If a policy already exists, it won't be overwritten unless you confirm the replacement.
  • By default, all policy methods return true, but you should implement your business logic for proper authorization.