Skip to content

Instantly share code, notes, and snippets.

@Ellrion
Last active August 29, 2015 14:04
Show Gist options
  • Save Ellrion/ca66b6ef159237ae8c3d to your computer and use it in GitHub Desktop.
Save Ellrion/ca66b6ef159237ae8c3d to your computer and use it in GitHub Desktop.
SimpleSecurity integration with Laravel (https://github.com/Ellrion/SimpleSecurity)
<?php
// access to routes. access allow for admin or manager users
Route::group(['prefix' => 'adminka', 'before' => 'acl:admin+manager'], function() {
Route::controller('/', 'TestController');
});
//in code. views some only for ussers who is admin and manager together
if (Security::isGranted('admin*manager')) {
echo '...';
}
//...
<?php
/**
* application & route filters
*/
//...
Route::filter('acl', function($route, $request, $value) {
if (!Security::isGranted($value)) {
App::abort(403, 'Access deny');
}
});
<?php
/**
* file in config dir
* example acl list
*/
return array(
'user:' => function ($user, $params) {
return !empty($user->id) && !empty($params['user'])
&& (int)$params['user']===(int)$user->id;
}
, 'rand' => function ($user, $params) {
return rand(0,1) ? 'user:18' : 'user:21';
}
, 'admin' => function ($user, $params) {
return !empty($user->type) && User::ADMIN_TYPE === (int)$user->type;
}
, 'manager' => function ($user, $params) {
return !empty($user->type) && User::MANAGER_TYPE === (int)$user->type;
}
, 'advertiser' => function ($user, $params) {
return !empty($user->type) && User::ADVERTISER_TYPE === (int)$user->type;
}
);
<?php
use Illuminate\Support\Facades\Facade;
/**
* @see \Ellrion\SimpleSecurity\Security
*/
class SecurityFacade extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'security'; }
}
<?php
use Illuminate\Support\ServiceProvider;
use Ellrion\SimpleSecurity\Security;
class SecurityServiceProvider
extends ServiceProvider
{
public function register()
{
$this->app->singleton('security', function($app) {
return new Security($app['config']['security'], $app['auth']->user());
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment