Created
October 19, 2014 18:29
-
-
Save jaytaph/a326e3d5b3daddc3ae18 to your computer and use it in GitHub Desktop.
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
{ | |
"name": "jaytaph/sec", | |
"require": { | |
"symfony/security": "2.5.x" | |
}, | |
"autoload" : { | |
"psr-0" : { "Noxlogic" : "vendor" } | |
}, | |
"authors": [ | |
{ | |
"name": "Joshua Thijssen", | |
"email": "[email protected]" | |
} | |
] | |
} |
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 | |
require_once "../vendor/autoload.php"; | |
// Initialize authentication part | |
$providers = array(); | |
$providers[] = new stdClass(); // Dummy provider. The manager does not accept empty arrays | |
$authenticationManager = new AuthenticationProviderManager($providers); | |
// Initialize authorization part | |
$voters = array(); | |
$voters[] = new RoleVoter(); // The role-voter allows us to match against ROLE_* | |
$accessDecisionManager = new AccessDecisionManager($voters, AccessDecisionManager::STRATEGY_AFFIRMATIVE); | |
// Tie everything together in the security context | |
$securityContext = new SecurityContext( | |
$authenticationManager, | |
$accessDecisionManager | |
); | |
// Create a token with my username, password, a dummy provider key (not used), and the roles for this user | |
$token = new UsernamePasswordToken("jaytaph", "my_secret_password", "dummykey", array('ROLE_ADMIN', 'ROLE_USER')); | |
$securityContext->setToken($token); | |
// Does the user have the ROLE_ADMIN? | |
if ($securityContext->isGranted('ROLE_ADMIN')) { | |
print "<strong>We have the admin role!</strong>"; | |
} | |
// Does the user have the ROLE_EDITOR? | |
if ($securityContext->isGranted('ROLE_EDITOR')) { | |
print "<strong>We have the admin role!</strong>"; | |
} else { | |
print "<strong>No editor role for us</strong>"; | |
} | |
// Display the content of the token | |
print "<pre>"; | |
print_r($securityContext->getToken()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment