Appearance
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
Option | Description |
---|---|
--module=MODULE | The 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:
Scan Modules: Scans the enabled modules (or a specific module if provided) for models.
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".
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 resourcesview
- Check if the user can view a specific resourceviewRow
- Filter query results based on user permissionsupdate
- Check if the user can update a specific resourceupdateAny
- Check if the user can update any resourcecreate
- Check if the user can create resourcesdelete
- Check if the user can delete a specific resourcedeleteAny
- Check if the user can delete any resourcerestore
- Check if the user can restore a soft-deleted resourceforceDelete
- Check if the user can permanently delete a resourcereorder
- 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.