-
-
Save sstok/170f443e4a15946d33803e3a3c20f49f 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
<?php | |
namespace App; | |
require __DIR__.'/vendor/autoload.php'; | |
use Symfony\Component\Console\Application; | |
use Symfony\Component\Console\ConsoleEvents; | |
use Symfony\Component\Console\Event\ConsoleCommandEvent; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\EventDispatcher\EventDispatcher; | |
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager; | |
use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider; | |
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | |
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | |
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; | |
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; | |
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter; | |
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter; | |
use Symfony\Component\Security\Core\Encoder\EncoderFactory; | |
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder; | |
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | |
use Symfony\Component\Security\Core\User\InMemoryUserProvider; | |
use Symfony\Component\Security\Core\User\User; | |
use Symfony\Component\Security\Core\User\UserChecker; | |
use WouterJ\Security\Cli\CliToken; | |
$app = new Application('Security CLI app'); | |
$app->register('hello') | |
->addArgument('user', InputArgument::REQUIRED) | |
->addOption('pass', null, InputOption::VALUE_REQUIRED) | |
->setCode(function (InputInterface $input, OutputInterface $output) { | |
$authenticator = new AuthenticationProviderManager([ | |
new DaoAuthenticationProvider( | |
new InMemoryUserProvider([ | |
'wouter' => ['password' => 'test', 'roles' => ['ROLE_USER']] | |
]), | |
new UserChecker(), | |
'cli', | |
new EncoderFactory([ | |
User::class => new PlaintextPasswordEncoder(), | |
]) | |
) | |
]); | |
$tokenStorage = new TokenStorage(); | |
$token = new UsernamePasswordToken( | |
$input->getArgument('user'), | |
$input->getOption('pass'), | |
'cli' | |
); | |
$tokenStorage->setToken($authenticator->authenticate($token)); | |
$accessDecisionManager = new AccessDecisionManager([new RoleVoter()]); | |
$authorizationChecker = new AuthorizationChecker( | |
$tokenStorage, | |
$authenticator, | |
$accessDecisionManager | |
); | |
if (!$authorizationChecker->isGranted('ROLE_USER')) { | |
throw new AccessDeniedException(); | |
} | |
$output->writeln('Hello '.$input->getArgument('user').'!'); | |
}); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment