robypz / laravel-mongodb-permission
Simple roles and permissions package for Laravel using MongoDB
dev-main
2025-05-26 05:01 UTC
Requires
- php: ^8.2
- laravel/framework: ^12.0
- mongodb/laravel-mongodb: ^5.2
This package is auto-updated.
Last update: 2025-05-26 05:06:57 UTC
README
A simple roles and permissions package for Laravel using MongoDB.
Installation
You can install the package via composer:
composer require robypz/laravel-mongodb-permission
Requirements
- PHP ^8.2
- Laravel ^12.0
- MongoDB Laravel ^5.2
Configuration
- Add the service provider to your
config/app.php
:
'providers' => [ // ... RobYpz\LaravelMongodbPermission\Providers\LaravelMongoDBPermissionServiceProvider::class, ];
- Run the migrations:
php artisan migrate
Usage
Setup
Add the HasRoles
trait to your User model:
use RobYpz\LaravelMongodbPermission\Models\HasRoles; class User extends Authenticatable { use HasRoles; // ... }
Managing Roles
// Assign a role to a user $user->assignRole('admin'); // or $user->assignRole(['admin', 'editor']); // Check if a user has a role $user->hasRole('admin'); // Check if a user has any of the given roles $user->hasAnyRole(['admin', 'editor']); // Remove a role from a user $user->removeRole('admin');
Managing Permissions
// Give permission to a user $user->givePermissionTo('edit-posts'); // or $user->givePermissionTo(['edit-posts', 'delete-posts']); // Check if a user has a permission $user->hasPermission('edit-posts'); // Check if a user has any of the given permissions $user->hasAnyPermission(['edit-posts', 'delete-posts']); // Remove a permission from a user $user->removePermission('edit-posts');
Middleware
This package includes middleware for protecting routes:
// Protect a route with role middleware Route::get('/admin', function () { // ... })->middleware('role:admin'); // Protect a route with permission middleware Route::get('/posts/create', function () { // ... })->middleware('permission:create-posts');
License
The MIT License (MIT). Please see the License File for more information.
Author
- Robert Yepez (robertyepez0208@hotmail.com)