Top Related Projects
Associate users with roles and permissions
A framework agnostic authentication & authorization system.
Role-based Permissions for Laravel 5
Quick Overview
Laratrust is a role-based access control (RBAC) package for Laravel applications. It provides a flexible and easy-to-use solution for managing user roles, permissions, and teams, allowing developers to implement complex authorization systems with minimal effort.
Pros
- Easy integration with Laravel's authentication system
- Supports multiple user models and teams
- Provides middleware for route protection
- Offers a seeder for quick setup and testing
Cons
- May introduce additional complexity for simple projects
- Requires careful planning of role and permission structures
- Performance impact on large-scale applications with many roles/permissions
- Learning curve for developers new to RBAC concepts
Code Examples
- Checking if a user has a role:
if ($user->hasRole('admin')) {
// User is an admin
}
- Assigning a permission to a role:
$admin = Role::findByName('admin');
$admin->givePermission('edit-users');
- Using middleware to protect routes:
Route::group(['middleware' => ['role:admin|editor']], function () {
Route::get('/dashboard', 'DashboardController@index');
});
- Checking for multiple permissions:
if ($user->isAbleTo(['edit-post', 'publish-post'])) {
// User can edit and publish posts
}
Getting Started
- Install Laratrust via Composer:
composer require santigarcor/laratrust
- Publish the configuration file:
php artisan vendor:publish --tag="laratrust"
- Run the migrations:
php artisan migrate
- Add the Laratrust trait to your User model:
use Laratrust\Traits\LaratrustUserTrait;
class User extends Authenticatable
{
use LaratrustUserTrait;
// ...
}
- Define roles and permissions in your
database/seeders/LaratrustSeeder.php
file and run the seeder:
php artisan db:seed --class=LaratrustSeeder
Competitor Comparisons
Associate users with roles and permissions
Pros of Laravel Permission
- More actively maintained with frequent updates
- Better documentation and extensive examples
- Supports caching for improved performance
Cons of Laravel Permission
- Slightly steeper learning curve for beginners
- Less flexible role inheritance system
Code Comparison
Laratrust:
$user->attachRole('admin');
$user->can('edit-post');
Laravel Permission:
$user->assignRole('admin');
$user->hasPermissionTo('edit-post');
Both packages offer similar functionality for managing roles and permissions in Laravel applications. Laravel Permission has gained popularity due to its active development and comprehensive documentation. It also provides built-in caching support, which can be beneficial for larger applications.
On the other hand, Laratrust has a simpler API that might be easier for beginners to grasp. It also offers a more flexible role inheritance system, allowing for more complex hierarchies.
In terms of code, both packages have similar syntax for assigning roles and checking permissions. The main differences lie in method names and some additional features specific to each package.
Ultimately, the choice between Laratrust and Laravel Permission depends on your specific project requirements, team expertise, and preference for documentation and community support.
A framework agnostic authentication & authorization system.
Pros of Sentinel
- More comprehensive user management features, including throttling and activation
- Built-in support for social authentication
- More extensive documentation and examples
Cons of Sentinel
- Steeper learning curve due to more complex architecture
- Paid product, which may not be suitable for all projects
- Less frequent updates compared to Laratrust
Code Comparison
Sentinel:
Sentinel::register([
'email' => 'john.doe@example.com',
'password' => 'password',
'first_name' => 'John',
'last_name' => 'Doe',
]);
Laratrust:
$user = User::create([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'password' => bcrypt('password'),
]);
$user->attachRole('admin');
Both packages offer role-based access control for Laravel applications, but they differ in their approach and feature set. Sentinel provides a more comprehensive user management system with additional features like throttling and social authentication. However, it comes at the cost of being a paid product with a steeper learning curve.
Laratrust, on the other hand, is a free and open-source package that focuses primarily on role and permission management. It's simpler to set up and use, making it a good choice for projects that don't require the advanced features offered by Sentinel. Laratrust also benefits from more frequent updates and community contributions.
Role-based Permissions for Laravel 5
Pros of Entrust
- More established project with longer history and larger user base
- Simpler setup process for basic role-based permissions
- Lightweight and focused solely on roles and permissions
Cons of Entrust
- No longer actively maintained (last commit in 2017)
- Lacks some advanced features like teams and ownership
- Not compatible with newer Laravel versions (5.0+)
Code Comparison
Entrust:
$user->hasRole('admin');
$user->can('edit-post');
$user->attachRole('moderator');
Laratrust:
$user->hasRole('admin');
$user->isAbleTo('edit-post');
$user->attachRole('moderator');
$user->syncRoles(['admin', 'moderator']);
$user->attachPermission('edit-post');
Laratrust offers more flexibility and advanced features, such as syncing multiple roles and directly attaching permissions to users. It also provides better support for newer Laravel versions and is actively maintained, making it a more suitable choice for modern Laravel applications. However, Entrust may still be preferable for simpler projects or those requiring minimal setup for basic role-based permissions.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Laratrust (Laravel Package)
Version Compatibility
Laravel | Laratrust |
---|---|
10.x, 11.X | 8.x |
9.x-10.x | 7.x |
8.x | 6.x |
7.x | 6.x |
6.x | 6.x |
5.6.x - 5.8.x | 5.2 |
5.3.x - 5.5.x | 5.1 |
5.0.x - 5.2.x | 4.0. |
Installation, Configuration and Usage
To install, configure and learn how to use Laratrust please go to the Documentation.
What does Laratrust support?
- Multiple user models.
- Multiple roles and permissions assignable to users.
- Multiple permissions assignable to roles.
- Roles and permissions verification.
- Roles and permissions caching.
- Events when roles and permissions are added, removed or synced.
- Multiple roles and permissions can be added to users within teams.
- Multiple guards for the middleware.
- A simple administration panel for roles and permissions.
- Laravel gates and policies.
License
Laratrust is open-sourced software licensed under the MIT license.
Contributing
Please report any issue you find in the issues page. Pull requests are more than welcome.
Top Related Projects
Associate users with roles and permissions
A framework agnostic authentication & authorization system.
Role-based Permissions for Laravel 5
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot