Created
October 5, 2015 02:59
-
-
Save enygma/ff242d12253accf4858d to your computer and use it in GitHub Desktop.
Example of the Property Auth engine working in a laravel app
This file contains hidden or 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 | |
// 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'); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This uses the PropAuth package: https://github.com/psecio/propauth