Skip to content

Instantly share code, notes, and snippets.

@jaytaph
Created October 19, 2014 18:29
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 jaytaph/a326e3d5b3daddc3ae18 to your computer and use it in GitHub Desktop.
Save jaytaph/a326e3d5b3daddc3ae18 to your computer and use it in GitHub Desktop.
{
"name": "jaytaph/sec",
"require": {
"symfony/security": "2.5.x"
},
"autoload" : {
"psr-0" : { "Noxlogic" : "vendor" }
},
"authors": [
{
"name": "Joshua Thijssen",
"email": "jthijssen@noxlogic.nl"
}
]
}
<?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