SlideShare a Scribd company logo
Simple ACL with Laravel
Based on the tutorial by Ollie Read
http://ollieread.com/blog/2014/03/18/a-simplified-laravel-acl/
Migrations
php artisan migrate:make create_acl_groups_table
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateAclGroupsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::create('acl_groups', function ($table){
$table->increments('id');
$table->string('name', 50);
$table->string('description', 255);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('acl_groups');
}
}
php artisan migrate:make create_acl_permissions_table
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateAclPermissionsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::create('acl_permissions', function($table){
$table->increments('id');
$table->string('ident', 255);
$table->string('description', 255);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('acl_permissions');
}
}
php artisan migrate:make create_acl_group_permissions_table
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateAclGroupPermissionsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::create('acl_group_permissions', function($table){
$table->integer('group_id', false);
$table->integer('permission_id', false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('acl_group_permissions');
}
}
php artisan migrate:make create acl_user_groups_table
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateAclUserGroupsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::create('acl_user_groups', function($table){
$table->integer('user_id', false);
$table->integer('group_id', false);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('acl_user_groups');
}
}
Models
AclGroup.php
<?php
class AclGroup extends Eloquent {
protected $table = 'acl_groups';
protected $fillable = array('name', 'description');
public $timestamps = false;
public function users() {
return $this->belongsToMany('User', 'acl_user_groups', 'group_id', 'user_id');
}
public function permissions() {
return $this->belongsToMany('AclPermission', 'acl_group_permissions', 'group_id',
'permission_id');
}
}
AclPermission.php
<?php
class AclPermission extends Eloquent {
protected $table = 'acl_permissions';
protected $fillable = array('ident', 'description');
public $timestamps = false;
public function groups(){
return $this->belongsToMany('AclGroup', 'acl_group_permissions', 'group_id',
'permission_id');
}
public function getKey()
{
return $this->attributes['ident'];
}
}
AclPermitted.php
<?php
class AclPermittedFilter {
public function filter($route, $request){
$user = Auth::user();
$user->load('groups', 'groups.permissions');
$permitted = false;
foreach($user->groups as $group){
if ( $group->permissions->contains($route->getName()) ){
$permitted = true;
break;
}
}
if (!$permitted) {
return Redirect::route('user.denied');
}
}
public static function checkPermission($route)
{
$user = Auth::user();
$user->load('groups', 'groups.permissions');
$permitted = false;
foreach($user->groups as $group){
if ( $group->permissions->contains($route) ){
$permitted = true;
break;
}
}
return $permitted;
}
}
Sample Usage
routes.php
Route::filter('acl.permitted', 'AclPermittedFilter');
Route::group(array('prefix'=>'user'), function () {
Route::get('supersecret', array(
'before'=> ['auth.ldap', 'acl.permitted'],
'as' => 'user.supersecret',
'uses' => 'UserController@supersecret'
));
Route::get('denied', array(
'as' => 'user.denied',
'uses' => 'UserController@denied'
));
});
From a view
@if ( AclPermittedFilter::checkPermission('user.supersecret') )
<h5> You are allowed to view secret stuff</h5>
@endif

More Related Content

PPTX
An introduction to Laravel Passport
PPTX
Laravel Beginners Tutorial 2
ODP
Javascript laravel's friend
PPTX
REST APIs in Laravel 101
PDF
Laravel Restful API and AngularJS
PPTX
Laravel Beginners Tutorial 1
PPTX
Laravel for Web Artisans
PPT
Web service with Laravel
An introduction to Laravel Passport
Laravel Beginners Tutorial 2
Javascript laravel's friend
REST APIs in Laravel 101
Laravel Restful API and AngularJS
Laravel Beginners Tutorial 1
Laravel for Web Artisans
Web service with Laravel

What's hot (20)

PDF
Introduction to AngularJS For WordPress Developers
PDF
RESTful API development in Laravel 4 - Christopher Pecoraro
PPT
Learn Dashing Widget in 90 minutes
KEY
Asynchronous Interfaces
PDF
Getting Started-with-Laravel
PDF
Laravel 5 In Depth
PDF
Silex Cheat Sheet
PDF
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
PPTX
Figcaption radovolsky
PDF
Caldera Learn - LoopConf WP API + Angular FTW Workshop
PPTX
19.imagini in laravel5
PDF
My Top 5 APEX JavaScript API's
PDF
Codeigniter : Custom Routing - Manipulate Uri
KEY
Rack is Spectacular
PDF
Creating a modern web application using Symfony API Platform Atlanta
PDF
Web services with laravel
PPTX
How to write not breakable unit tests
PDF
WP Weekend #2 - Corcel, aneb WordPress přes Laravel
PDF
Laravel Design Patterns
PDF
Rest api titouan benoit
Introduction to AngularJS For WordPress Developers
RESTful API development in Laravel 4 - Christopher Pecoraro
Learn Dashing Widget in 90 minutes
Asynchronous Interfaces
Getting Started-with-Laravel
Laravel 5 In Depth
Silex Cheat Sheet
Connecting Content Silos: One CMS, Many Sites With The WordPress REST API
Figcaption radovolsky
Caldera Learn - LoopConf WP API + Angular FTW Workshop
19.imagini in laravel5
My Top 5 APEX JavaScript API's
Codeigniter : Custom Routing - Manipulate Uri
Rack is Spectacular
Creating a modern web application using Symfony API Platform Atlanta
Web services with laravel
How to write not breakable unit tests
WP Weekend #2 - Corcel, aneb WordPress přes Laravel
Laravel Design Patterns
Rest api titouan benoit
Ad

Similar to Simple acl with laravel (20)

PDF
Andy Postnikov - Drupal 7 vs Drupal 8: от бутстрапа до рендера
PDF
Lviv 2013 d7 vs d8
PDF
Lviv 2013 d7 vs d8
PDF
JQuery In Drupal
PDF
Intro to Laravel 4
PDF
Phinx talk
KEY
Apostrophe (improved Paris edition)
DOCX
Laravel
PDF
Building Lithium Apps
PDF
Apostrophe
PDF
関西PHP勉強会 php5.4つまみぐい
PDF
PHP and Rich Internet Applications
KEY
Yii Introduction
PPTX
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
PDF
SPL: The Missing Link in Development
PDF
All I Need to Know I Learned by Writing My Own Web Framework
PDF
Intro To Mvc Development In Php
PDF
Demystifying AJAX Callback Commands in Drupal 8
PDF
Doctrine For Beginners
PDF
以 Laravel 經驗開發 Hyperf 應用
Andy Postnikov - Drupal 7 vs Drupal 8: от бутстрапа до рендера
Lviv 2013 d7 vs d8
Lviv 2013 d7 vs d8
JQuery In Drupal
Intro to Laravel 4
Phinx talk
Apostrophe (improved Paris edition)
Laravel
Building Lithium Apps
Apostrophe
関西PHP勉強会 php5.4つまみぐい
PHP and Rich Internet Applications
Yii Introduction
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
SPL: The Missing Link in Development
All I Need to Know I Learned by Writing My Own Web Framework
Intro To Mvc Development In Php
Demystifying AJAX Callback Commands in Drupal 8
Doctrine For Beginners
以 Laravel 經驗開發 Hyperf 應用
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
cuic standard and advanced reporting.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
cuic standard and advanced reporting.pdf
sap open course for s4hana steps from ECC to s4
Diabetes mellitus diagnosis method based random forest with bat algorithm
“AI and Expert System Decision Support & Business Intelligence Systems”
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
NewMind AI Weekly Chronicles - August'25 Week I
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Teaching material agriculture food technology

Simple acl with laravel

  • 1. Simple ACL with Laravel Based on the tutorial by Ollie Read http://ollieread.com/blog/2014/03/18/a-simplified-laravel-acl/ Migrations php artisan migrate:make create_acl_groups_table <?php use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateAclGroupsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // Schema::create('acl_groups', function ($table){ $table->increments('id'); $table->string('name', 50); $table->string('description', 255); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::drop('acl_groups'); } } php artisan migrate:make create_acl_permissions_table <?php
  • 2. use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateAclPermissionsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // Schema::create('acl_permissions', function($table){ $table->increments('id'); $table->string('ident', 255); $table->string('description', 255); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::drop('acl_permissions'); } } php artisan migrate:make create_acl_group_permissions_table <?php use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateAclGroupPermissionsTable extends Migration { /** * Run the migrations. * * @return void */ public function up()
  • 3. { // Schema::create('acl_group_permissions', function($table){ $table->integer('group_id', false); $table->integer('permission_id', false); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::drop('acl_group_permissions'); } } php artisan migrate:make create acl_user_groups_table <?php use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class CreateAclUserGroupsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // Schema::create('acl_user_groups', function($table){ $table->integer('user_id', false); $table->integer('group_id', false); }); } /** * Reverse the migrations. * * @return void */
  • 4. public function down() { // Schema::drop('acl_user_groups'); } } Models AclGroup.php <?php class AclGroup extends Eloquent { protected $table = 'acl_groups'; protected $fillable = array('name', 'description'); public $timestamps = false; public function users() { return $this->belongsToMany('User', 'acl_user_groups', 'group_id', 'user_id'); } public function permissions() { return $this->belongsToMany('AclPermission', 'acl_group_permissions', 'group_id', 'permission_id'); } } AclPermission.php <?php class AclPermission extends Eloquent { protected $table = 'acl_permissions'; protected $fillable = array('ident', 'description'); public $timestamps = false; public function groups(){ return $this->belongsToMany('AclGroup', 'acl_group_permissions', 'group_id', 'permission_id'); } public function getKey() {
  • 5. return $this->attributes['ident']; } } AclPermitted.php <?php class AclPermittedFilter { public function filter($route, $request){ $user = Auth::user(); $user->load('groups', 'groups.permissions'); $permitted = false; foreach($user->groups as $group){ if ( $group->permissions->contains($route->getName()) ){ $permitted = true; break; } } if (!$permitted) { return Redirect::route('user.denied'); } } public static function checkPermission($route) { $user = Auth::user(); $user->load('groups', 'groups.permissions'); $permitted = false; foreach($user->groups as $group){ if ( $group->permissions->contains($route) ){ $permitted = true; break; } } return $permitted; } } Sample Usage routes.php Route::filter('acl.permitted', 'AclPermittedFilter');
  • 6. Route::group(array('prefix'=>'user'), function () { Route::get('supersecret', array( 'before'=> ['auth.ldap', 'acl.permitted'], 'as' => 'user.supersecret', 'uses' => 'UserController@supersecret' )); Route::get('denied', array( 'as' => 'user.denied', 'uses' => 'UserController@denied' )); }); From a view @if ( AclPermittedFilter::checkPermission('user.supersecret') ) <h5> You are allowed to view secret stuff</h5> @endif