Last active
April 15, 2017 17:33
-
-
Save ollieread/9620972 to your computer and use it in GitHub Desktop.
Gists for my simple laravel ACL article.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan migrate:make create_acl_group_permissions_table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan migrate:make create_acl_groups_table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan migrate:make create_acl_permissions_table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php artisan migrate:make create_acl_user_groups_table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AclPermittedFilter { | |
public function filter($route, $request) | |
{ | |
$permitted = false; | |
$user = Auth::user(); | |
$user->load('groups', 'group.permissions'); | |
foreach($user->groups as $group) { | |
if($group->permissions->has($route->getName())) { | |
$permitted = true; | |
break; | |
} | |
} | |
if(!$permitted) { | |
return Redirect::route('user.denied'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
public function permissions() | |
{ | |
return $this->belongsToMany('AclPermission', 'acl_group_permissions'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
public function getKey() | |
{ | |
return $this->attributes['ident']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::filter('acl.permitted', 'AclPermittedFilter'); | |
Route::group(array('prefix' => 'user'), function() { | |
// other routes here | |
Route::get('supersecret', array( | |
'before' => ['auth', 'acl.permitted'], | |
'as' => 'user.supersecret', | |
'uses' => 'UserController@supersecret' | |
)) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::group(array('prefix' => 'user'), function() { | |
// for account registration | |
Route::post('register', array( | |
'as' => 'user.register', | |
'uses' => 'UserController@register' | |
)); | |
// for authorising/logging in | |
Route::post('authorise', array( | |
'as' => 'user.authorise', | |
'uses' => 'UserController@authorise' | |
)); | |
// check that a token is valid | |
Route::get('validate', array( | |
'as' => 'user.validate', | |
'uses' => 'UserController@validate' | |
)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You miss " ; " at line 13 [ routes-filtered.php ]