Skip to content

Instantly share code, notes, and snippets.

@enygma
Created October 5, 2015 02:59
Show Gist options
  • Save enygma/ff242d12253accf4858d to your computer and use it in GitHub Desktop.
Save enygma/ff242d12253accf4858d to your computer and use it in GitHub Desktop.
Example of the Property Auth engine working in a laravel app
<?php
// First, our service provider defining the policies
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Psecio\PropAuth\Enforcer;
use Psecio\PropAuth\Policy;
use Psecio\PropAuth\PolicySet;
class PolicyServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('policies', function($app) {
$set = PolicySet::instance()
->add('can-edit', Policy::instance()->hasUsername('ccornutt'))
->add('can-delete', Policy::instance()->can(function($post) {
return $post->author == 'ccornutt';
})
);
return Enforcer::instance($set);
});
}
}
// And now it in use in a simple controller
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Auth as Auth;
class TestController extends Controller
{
public function showTest()
{
$result = \App::make('policies')->allows('can-edit', Auth::user());
var_export($result); // true for a normal check
$post = (object)[ 'author' => 'ccornutt'];
$result = \App::make('policies')->allows('can-delete', Auth::user(), [$post]);
var_export($result); // true for a closure-based check
return view('test/test');
}
}
?>
@enygma
Copy link
Author

enygma commented Oct 5, 2015

This uses the PropAuth package: https://github.com/psecio/propauth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment