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.
Requirements
Moonshine: v2.0+
Spatie Laravel Permissions: v6.0+
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.
Install the Spatie Laravel Permissions package and follow the instructions in the documentation to set up the package correctly.
Install the package via composer:
composer require sweet1s/moonshine-roles-permissions
return [
// ...
'auth' => [
// ...
'providers' => [
'moonshine' => [
'driver' => 'eloquent',
'model' => \App\Models\User::class,
],
],
],
// ...
];
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,
],
<?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'];
}
<?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;
// ...
}
php artisan moonshine-rbac:install
php artisan moonshine-rbac:user
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'),
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();
}),
//...
);
}
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
// ...
use Sweet1s\MoonshineRBAC\Traits\WithRolePermissions;
class PostResource extends ModelResource
{
use WithRolePermissions;
// ...
}
If you want to create custom permissions, you can use the following command:
php artisan moonshine-rbac:permission
or in PermissionResource
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
Role Resource | User Resource |
---|---|