Skip to content
Snippets Groups Projects
foo.php 2.62 KiB
Newer Older
Maxime Veber's avatar
Maxime Veber committed
<?php

require __DIR__ . '/vendor/autoload.php';
Maxime Veber's avatar
Maxime Veber committed
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcher;
use BiiG\SecurityTest\Kernel;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\FirewallMap;
use Symfony\Component\HttpFoundation\RequestMatcher,
    Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage,
    BiiG\SecurityTest\CustomAuthenticationListener,
    BiiG\SecurityTest\CustomAuthenticationProvider,
    Symfony\Component\Security\Http\Firewall,
    Symfony\Component\HttpKernel\KernelEvents,
    Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter,
    Symfony\Component\Security\Core\Role\RoleHierarchy,
    Symfony\Component\Security\Core\Authorization\AccessDecisionManager
    ;
use Symfony\Component\Security\Http\AccessMap;
use Symfony\Component\Security\Http\Firewall\AccessListener;




$request = Request::createFromGlobals();
$dispatcher = new EventDispatcher();
$kernel = new Kernel($dispatcher, function() {
    return new Response("<h1>Hello</h1>");
});
Maxime Veber's avatar
Maxime Veber committed



///////////////////////
Maxime Veber's avatar
Maxime Veber committed
/// FIREWALL CONFIG
Maxime Veber's avatar
Maxime Veber committed

Maxime Veber's avatar
Maxime Veber committed
$map = new FirewallMap();
Maxime Veber's avatar
Maxime Veber committed

Maxime Veber's avatar
Maxime Veber committed
$requestMatcher = new RequestMatcher('^/');
$tokenStorage = new TokenStorage();
Maxime Veber's avatar
Maxime Veber committed
// instances of Symfony\Component\Security\Http\Firewall\ListenerInterface
Maxime Veber's avatar
Maxime Veber committed
$authManager = new CustomAuthenticationProvider();
Maxime Veber's avatar
Maxime Veber committed
$listeners = [
Maxime Veber's avatar
Maxime Veber committed
    new CustomAuthenticationListener(
Maxime Veber's avatar
Maxime Veber committed
        $tokenStorage,
Maxime Veber's avatar
Maxime Veber committed
        $authManager,
Maxime Veber's avatar
Maxime Veber committed
        'swagg'
    )
];
Maxime Veber's avatar
Maxime Veber committed
// The exception listener object is too complex for this example
/*
$exceptionListener = new \Symfony\Component\Security\Http\Firewall\ExceptionListener($tokenStorage, $trustResolver);
//*/

$map->add($requestMatcher, $listeners);


Maxime Veber's avatar
Maxime Veber committed
$firewall = new Firewall($map, $dispatcher);
Maxime Veber's avatar
Maxime Veber committed
$dispatcher->addListener(
Maxime Veber's avatar
Maxime Veber committed
    KernelEvents::REQUEST,
Maxime Veber's avatar
Maxime Veber committed
    array($firewall, 'onKernelRequest')
);

Maxime Veber's avatar
Maxime Veber committed
/////////////////////////
/// Authorization

// instances of Symfony\Component\Security\Core\Authorization\Voter\VoterInterface
$voters = [
    new RoleHierarchyVoter(
        new RoleHierarchy([
            'ROLE_SUPER_ADMIN' => [
                'ROLE_ADMIN',
                'ROLE_USER'
            ]
        ])
    ),
];
$strategy = AccessDecisionManager::STRATEGY_AFFIRMATIVE;

$accessDecisionManager = new AccessDecisionManager(
    $voters,
    $strategy
);

$accessMap = new AccessMap();
$requestMatcher = new RequestMatcher('^/admin');
$accessMap->add($requestMatcher, array('ROLE_ADMIN'));

$accessListener = new AccessListener(
    $tokenStorage,
    $accessDecisionManager,
    $accessMap,
    $authManager
);


/////////////////////////
/// Run kernel
Maxime Veber's avatar
Maxime Veber committed

Maxime Veber's avatar
Maxime Veber committed
$kernel->handle($request)->send();