Git Product home page Git Product logo

moonshine-roles-permissions's Introduction

MoonShine Roles-Permissions

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'),
], '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;

    // ...
}

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


How does it look in the Admin Panel ?

Role Resource User Resource
How does RoleResource it look in the Admin Panel How does UserResource it look in the Admin Panel

moonshine-roles-permissions's People

Contributors

forest-lynx avatar sweet1s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sozdy forest-lynx

moonshine-roles-permissions's Issues

A small error in the readme.md file leads to issues when installing the package according to the instructions provided in the readme.md

Hello!

In the installation documentation for the 'moonshine-roles-permissions' package, there is no information about using the 'spatie/laravel-permission': '^5.11' package. If you run the command 'composer require sweet1s/moonshine-roles-permissions,' it installs version 1.2^ in which the 'Traits' are missing, making further configuration impossible. Therefore, please add information to the Readme.md file about the need for 'Spatie Laravel Permissions ^6'. Thank you.

The traits folder is missing

If you use 'spatie/laravel-permission': '^6', everything is okay.

The traits folder exists

Error in README.md

7. In the AuthServiceProvider.php file, add the following ( Super Admin Role ):

use Illuminate\Support\Facades\Gate;

public function boot()
{
    Gate::before(function ($user, $ability) {
        return $user?->role?->id === 1;
    });
}

Line error:
return $user?->role?->id === 1;
With this option, all users whose role?->id !== 1 will be denied access

Correct option:
return $user?->role?->id === 1 ? true : null;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.