PHP Classes
elePHPant
Icontem

Simple Laravel Role Management: Control the access to resources based on roles

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-08-09 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 26 This week: 26All time: 9,641 This week: 8Up
Version License PHP version Categories
simple-role 1.0MIT/X Consortium ...7.1.3PHP 5, User Management, Libraries
Description Author

This package can control the access to resources based on roles.

It provides a trait for use in user objects that allows to easily set the roles of any user.

Alternatively, user model classes may also have a fillable role property.

The package provides middleware to check for acceptable users with roles that allow to execute certain controller actions.

  Performance   Level  
Name: Thiago Przyczynski <contact>
Classes: 7 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 2x

 

Details

SimpleRole

Package that adds a simple role based authentication system to any Laravel project.

Installing and using

composer require thiagoprz/simple-role

Run the migration that will add role column to your users table, as: php artisan migrate

>Add roles using Enrollable Trait

... 
use Thiagoprz\SimpleRole\Enrollable;
...
class User ...
{
    Use Enrollable;
    ...    
}

This trait will add setRole($role) method to the User model and you will be able to update users role with it:

$user->setRole('admin')

>Add roles using Laravel standard attributes

If you don't want to use the Enrollable trait you can add 'role' column to your fillable attributes on User model:

class User
{
    ...
    protected $fillable = [
        'name', 'email', 'password', ... , 'role',
    ];
    ...    
}

Define the different types of role that you will be working and use the middleware to be handle on your routes as shown above:

web.php

// Route enrolment will be only acessible to "employee" users
Route::get('enrolment', 'EnrolmentController@index')->middleware('role:employee');

// Routes inside Admin namespace will be only acessible to users with the role "admin"
Route::namespace('Admin')->middleware(['auth', 'role:admin'])->group(function() {
...
});

// Routes inside Customer namespace will be only acessible to users with the role "customer"
Route::namespace('Customer')->middleware(['auth', 'role:customer'])->group(function() {
...
});

A route can require more than one role, to do that you just need to add comma separated roles.

// Route enrolment will be only acessible to "employee" users
Route::get('product', 'ProductController')->middleware('role:admin,manager,employee');

I'm aware of other packages that can do the same with more power allowing multiple roles and other stuff, but this package contains the simplest way of doing that and will allow small projects to run a simple role based with a small footprint.

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageHttp (1 directory)
Files folder imagemigrations (1 file)
  Plain text file Enrollable.php Class Class source
  Plain text file SimpleRoleServiceProvider.php Class Class source

  Files folder image Files  /  src  /  Http  
File Role Description
Files folder imageMiddleware (1 file)

  Files folder image Files  /  src  /  Http  /  Middleware  
File Role Description
  Plain text file SimpleRole.php Class Class source

  Files folder image Files  /  src  /  migrations  
File Role Description
  Plain text file 2017_08_08_060525_..._to_users_table.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:26
This week:26
All time:9,641
This week:8Up