sweet1s / moonshine-roles-permissions
Moonshine Role and Permission management
Requires
- php: ^8.1|^8.2|^8.3
- ext-curl: *
- ext-json: *
- lee-to/laravel-package-command: ^0.9.1
- spatie/laravel-permission: ^6.2
Requires (Dev)
- brianium/paratest: ^6.8
- mockery/mockery: ^1.4.4
- moonshine/moonshine: ^2.0
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0|^8.0
- phpstan/phpstan: ^1.4.7
- phpunit/phpunit: ^9.5.8
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.
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
-
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
- 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, ], ], ], // ... ];
- In the
Spatie permission config file
, change the models.role toApp\Models\Role::class
(Model need extend \Spatie\Permission\Models\Role), like this:
'models' => [ // ... 'role' => App\Models\Role::class, ],
- 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']; }
- 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; // ... }
- Run the following command to install the package and follow the installation steps:
php artisan moonshine-rbac:install
- (Optional) Create a user with new modal and assign automatically the role "Super Admin" to it.
php artisan moonshine-rbac:user
- Add to your
RoleResource
traitWithPermissionsFormComponent
:
<?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
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
- 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