Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What files are created when using the php artisan moonshine:policy command
    moonshine laravel


What files are created when using the php artisan moonshine:policy command


When you run the `php artisan moonshine:policy` command, it creates a new policy class that is bound to the admin panel user. Specifically, the command generates a class file in the `app/Policies` directory of your Laravel application. This policy class will typically contain methods that define the authorization logic for various actions related to a specific resource.

Summary of Created Files

- Policy Class: A new policy class file will be created in the `app/Policies` directory. This class will include methods for handling permissions such as:
- `viewAny`: Permission to view a list of resources.
- `view`: Permission to view a single resource.
- `create`: Permission to create a new resource.
- `update`: Permission to update an existing resource.
- `delete`: Permission to delete a resource.
- `massDelete`: Permission for bulk deletion.
- `restore`: Permission to restore a soft-deleted resource.
- `forceDelete`: Permission to permanently delete a resource.

For example, if you create a policy for a `Post` model, the generated file might look like this:

php
namespace App\Policies;

use Illuminate\Auth\Access\HandlesAuthorization;
use App\Models\Post;
use MoonShine\Models\MoonshineUser;

class PostPolicy {
    use HandlesAuthorization;

    public function viewAny(MoonshineUser $user) {
        return true; // Define your logic
    }

    public function view(MoonshineUser $user, Post $item) {
        return true; // Define your logic
    }

    public function create(MoonshineUser $user) {
        return true; // Define your logic
    }

    public function update(MoonshineUser $user, Post $item) {
        return true; // Define your logic
    }

    public function delete(MoonshineUser $user, Post $item) {
        return true; // Define your logic
    }

    public function massDelete(MoonshineUser $user) {
        return true; // Define your logic
    }

    public function restore(MoonshineUser $user, Post $item) {
        return true; // Define your logic
    }

    public function forceDelete(MoonshineUser $user, Post $item) {
        return true; // Define your logic
    }
}

This policy setup allows you to manage access control effectively within your MoonShine admin panel.

Citations:
[1] https://moonshine-laravel.com/docs/resource/advanced/advanced-commands
[2] https://moonshine-laravel.com/docs/resource/fields/fields-file
[3] https://stackoverflow.com/questions/70977373/laravel-artisan-commands-creates-new-files-as-root-instead-of-1000
[4] https://github.com/SWEET1S/moonshine-roles-permissions
[5] https://moonshine-laravel.com/docs/resource/advanced/advanced-authorization
[6] https://moonshine-laravel.com/docs/resource/models-resources/resources-authorization
[7] https://laravel.com/docs/11.x/artisan
[8] https://laravel.com/docs/11.x/structure