sweet1s/moonshine-roles-permissions

Moonshine Role and Permission management

2.3.3 2024-08-09 12:43 UTC

This package is auto-updated.

Last update: 2024-11-09 13:09:22 UTC


README

Description

This package is an extension exclusively designed for the MoonShine Admin Panel, building upon the functionality of the Spatie Laravel Permissions package. The primary purpose of this extension is to streamline role-based access control (RBAC) within the MoonShine Admin Panel. By utilizing this package, you can efficiently assign permissions to roles and then grant those roles to users, simplifying the process of managing permissions on a role-based level rather than individually assigning them to each user.

Total Downloads Latest Stable Version License

Laravel 9+ PHP 8+ Moonshine Admin Panel

Requirements

Moonshine: v2.0+

Spatie Laravel Permissions: v6.0+

Features

  • Role-Based Access Control (RBAC): Enhance your MoonShine Admin Panel with a comprehensive role-based permission system, allowing you to group users with similar permissions into roles and manage access more efficiently.
  • Role Assignment: Seamlessly associate permissions with roles, making it effortless to define the access rights for specific groups of users.
  • Bulk Role Assignment: Grant multiple users the same role simultaneously, reducing the manual effort required to manage permissions across large user bases.
  • Seamless Integration: The package seamlessly integrates with the MoonShine Admin Panel and extends the capabilities of the Spatie Laravel Permissions package specifically for this panel.

Important

Before using the package, it is crucial to understand that you need to use a different user model instead of "MoonShineUser" and use the table users. The package requires the utilization of the Spatie Laravel.

Installation

  1. Install the Spatie Laravel Permissions package and follow the instructions in the documentation to set up the package correctly.

  2. Install the package via composer:

composer require sweet1s/moonshine-roles-permissions
  1. In the MoonShine config file, change the user model to the default User model or the model you want to use for the admin panel.
return [
    // ...
    'auth' => [
        // ...
        'providers' => [
            'moonshine' => [
                'driver' => 'eloquent',
                'model' => \App\Models\User::class,
            ],
        ],
    ],
    // ...
];
  1. In the Spatie permission config file, change the models.role to App\Models\Role::class (Model need extend \Spatie\Permission\Models\Role), like this:
'models' => [
    // ...
    'role' => App\Models\Role::class,
],
  1. For your Role model, add the following:
<?php

namespace App\Models;

use Sweet1s\MoonshineRBAC\Traits\HasMoonShineRolePermissions;
use Spatie\Permission\Models\Role as SpatieRole;

class Role extends SpatieRole
{
    use HasMoonShineRolePermissions;

    protected $with = ['permissions'];
}
  1. For the user model, add the following:
<?php

namespace App\Models;

// ...
use Illuminate\Database\Eloquent\Relations\BelongsTo;

use Sweet1s\MoonshineRBAC\Traits\MoonshineRBACHasRoles;

class User extends Authenticatable
{
    use MoonshineRBACHasRoles;

    const SUPER_ADMIN_ROLE_ID = 1;

    // ...
}
  1. Run the following command to install the package and follow the installation steps:
php artisan moonshine-rbac:install
  1. (Optional) Create a user with new modal and assign automatically the role "Super Admin" to it.
php artisan moonshine-rbac:user
  1. Add to your RoleResource trait WithPermissionsFormComponent:
<?php

namespace App\MoonShine\Resources;

use Sweet1s\MoonshineRBAC\Traits\WithPermissionsFormComponent;
use Sweet1s\MoonshineRBAC\Traits\WithRolePermissions;

class RoleResource extends ModelResource
{
    use WithRolePermissions;
    use WithPermissionsFormComponent;

    // ...
}

Add to your UserResource trait WithRoleFormComponent:

<?php

namespace App\MoonShine\Resources;

use Sweet1s\MoonshineRBAC\Traits\WithRoleFormComponent;
use Sweet1s\MoonshineRBAC\Traits\WithRolePermissions;

class UserResource extends ModelResource
{
    use WithRolePermissions;
    use WithRoleFormComponent;

    // ...
}

Or add new MoonShine resource to your MoonShineServiceProvider file, like this (you can use other UserResource):

MenuGroup::make('System', [
    MenuItem::make('Admins', new \Sweet1s\MoonshineRBAC\Resource\UserResource(), 'heroicons.outline.users'),
    MenuItem::make('Roles', new \Sweet1s\MoonshineRBAC\Resource\RoleResource(), 'heroicons.outline.shield-exclamation'),
    MenuItem::make('Permissions', new \Sweet1s\MoonshineRBAC\Resource\PermissionResource(), 'heroicons.outline.shield-exclamation'),
], 'heroicons.outline.user-group'),

Dynamic Items on Menu

If you want to add dynamic items to the menu that depend on the role right, you just need to add an array of menus to the MenuRBAC::menu() adapter.

protected function menu(): array
{
    return MenuRBAC::menu(
        MenuGroup::make('System', [
            MenuItem::make('Admins', new \Sweet1s\MoonshineRBAC\Resource\UserResource(), 'heroicons.outline.users'),
            MenuItem::make('Roles', new \Sweet1s\MoonshineRBAC\Resource\RoleResource(), 'heroicons.outline.shield-exclamation'),
        ], 'heroicons.outline.user-group'),

        MenuItem::make(trans('moonshine::general.orders'), new OrderResource(), 'heroicons.outline.shopping-cart')
            ->badge(function(){
                return Order::where('status', Status::Completed->name)->count();
            }),

        //...
    );
}

Usage

  1. Creating a section in the admin panel with MoonShine
php artisan moonshine:resource Post
php artisan moonshine-rbac:permissions PostResource

You can use the following command to generate a resource and permissions at the same time:

php artisan moonshine-rbac:resource Post
  1. For Resource, add the following:
// ...
use Sweet1s\MoonshineRBAC\Traits\WithRolePermissions;

class PostResource extends ModelResource
{
    use WithRolePermissions;

    // ...
}

Custom Permissions

If you want to create custom permissions, you can use the following command:

php artisan moonshine-rbac:permission

or in PermissionResource

Localization

The package comes with default translation files in English, Russian and Romanian. If you want to customise the translations, you can publish the package translation files in your project using the following command:

php artisan vendor:publish --tag=moonshine-rbac-lang

Commands documentation

How does it look in the Admin Panel ?