Skip to content

Instantly share code, notes, and snippets.

@enygma
Created September 26, 2015 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enygma/e8c53e98c9bcc69a6d85 to your computer and use it in GitHub Desktop.
Save enygma/e8c53e98c9bcc69a6d85 to your computer and use it in GitHub Desktop.
Testing out the policy sets for the PropAuth library - not exactly where I want it to be yet (a little clunky) but getting there
<?php
require_once 'vendor/autoload.php';
use \Psecio\PropAuth\Enforcer;
use \Psecio\PropAuth\User;
use \Psecio\PropAuth\Policy;
use \Psecio\PropAuth\PolicySet;
//---------------------------
$user = (object)[
'username' => 'ccornutt',
'user_id' => 1
];
$post = (object)[
'title' => 'Test Post #1',
'id' => 4321,
'user_id' => 1
];
//---------------------------
$policy = new Policy();
$policy
->hasUsername('ccornutt')
->can($post, function($user) {
return ($post->user_id === $user->id);
});
$policySet = new PolicySet();
$policySet->add('update-post', $policy);
$enforcer = new Enforcer($policySet);
/**
Returns true because our username is "ccornutt" and
the User's ID and the one on the post match
*/
$result = $enforcer->allows('update-post', $user);
echo 'RESULT: '.var_export($result, true)."\n\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment