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 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 68 All time: 9,493 This week: 179Up
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.

Innovation Award
PHP Programming Innovation award nominee
August 2019
Number 3
Laravel is a well known framework for developing PHP applications.

There are many components based on this framework including those that are used to control the access of users with different application roles in an application.

This package is one of those components that uses PHP traits to make it simple to implement role based access control in existing Laravel based components.

Manuel Lemos
  Performance   Level  
Name: Thiago Przyczynski <contact>
Classes: 8 packages by
Country: Brazil Brazil
Age: 30
All time rank: 3785306 in Brazil Brazil
Week rank: 332 Up13 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

 

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:68
This week:0
All time:9,493
This week:179Up