Skip to content

Commit

Permalink
Feature : multiple userclass (#3)
Browse files Browse the repository at this point in the history
* Allow being linked to multiple user types

* Fixed Archive command
  • Loading branch information
yann-eugone authored Mar 15, 2017
1 parent 418af61 commit b9b6fe9
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 218 deletions.
23 changes: 4 additions & 19 deletions Command/ArchiveTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,26 @@
namespace Yokai\SecurityTokenBundle\Command;

use DateTime;
use Symfony\Component\Console\Command\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Yokai\SecurityTokenBundle\Archive\ArchivistInterface;

/**
* @author Yann Eugoné <[email protected]>
*/
class ArchiveTokenCommand extends Command
class ArchiveTokenCommand extends ContainerAwareCommand
{
/**
* @var ContainerInterface
*/
private $container;

/**
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;

parent::__construct('yokai:security-token:archive');
}

/**
* @inheritDoc
*/
protected function configure()
{
$this
->setName('yokai:security-token:archive')
->addOption('purpose', null, InputOption::VALUE_OPTIONAL, 'Filter tokens to archive on purpose.')
->addOption('before', null, InputOption::VALUE_OPTIONAL, 'Filter tokens to archive on created date.')
;
Expand Down Expand Up @@ -71,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/** @var $archivist ArchivistInterface */
$archivist = $this->container->get('yokai_security_token.resolved.archivist');
$archivist = $this->getContainer()->get('yokai_security_token.archivist');

$count = $archivist->archive($purpose, $beforeDate);

Expand Down
38 changes: 17 additions & 21 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,25 @@
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Yokai\SecurityTokenBundle\Archive\DeleteArchivist;
use Yokai\SecurityTokenBundle\Factory\TokenFactory;
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesser;
use Yokai\SecurityTokenBundle\Manager\TokenManager;
use Yokai\SecurityTokenBundle\Manager\UserManager;
use Yokai\SecurityTokenBundle\Repository\TokenRepository;

/**
* @author Yann Eugoné <[email protected]>
*/
class Configuration implements ConfigurationInterface
{
/**
* @var string
*/
private $name;

/**
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}

/**
* @inheritdoc
*/
public function getConfigTreeBuilder()
{
$builder = new TreeBuilder();
$root = $builder->root($this->name);
$root = $builder->root('yokai_security_token');

$root
->addDefaultsIfNotSet()
Expand Down Expand Up @@ -80,16 +73,19 @@ private function getServicesNode()
->addDefaultsIfNotSet()
->children()
->scalarNode('information_guesser')
->defaultValue('yokai_security_token.information_guesser')
->defaultValue('yokai_security_token.default_information_guesser')
->end()
->scalarNode('token_factory')
->defaultValue('yokai_security_token.default_token_factory')
->end()
->scalarNode('factory')
->defaultValue('yokai_security_token.token_factory')
->scalarNode('token_repository')
->defaultValue('yokai_security_token.default_token_repository')
->end()
->scalarNode('repository')
->defaultValue('yokai_security_token.token_repository')
->scalarNode('token_manager')
->defaultValue('yokai_security_token.default_token_manager')
->end()
->scalarNode('manager')
->defaultValue('yokai_security_token.token_manager')
->scalarNode('user_manager')
->defaultValue('yokai_security_token.default_user_manager')
->end()
->scalarNode('archivist')
->defaultValue('yokai_security_token.delete_archivist')
Expand Down
35 changes: 7 additions & 28 deletions DependencyInjection/YokaiSecurityTokenExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,23 @@

namespace Yokai\SecurityTokenBundle\DependencyInjection;

use ReflectionClass;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Yokai\SecurityTokenBundle\Archive\ArchivistInterface;
use Yokai\SecurityTokenBundle\DependencyInjection\Factory\TokenConfigurationFactory;
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface;
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
use Yokai\SecurityTokenBundle\Manager\TokenManagerInterface;
use Yokai\SecurityTokenBundle\Manager\UserManagerInterface;
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface;

/**
* @author Yann Eugoné <[email protected]>
*/
class YokaiSecurityTokenExtension extends Extension
{
/**
* @var string
*/
private $name;

/**
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}

/**
* @inheritdoc
*/
Expand All @@ -42,18 +33,6 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerAliases($config, $container);
}

/**
* @inheritDoc
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
$configuration = new Configuration($this->name);
$reflection = new ReflectionClass($configuration);
$container->addResource(new FileResource($reflection->getFileName()));

return $configuration;
}

/**
* @param array $config
* @param ContainerBuilder $container
Expand All @@ -72,7 +51,7 @@ private function registerTokens(array $config, ContainerBuilder $container)
private function registerAliases(array $config, ContainerBuilder $container)
{
foreach ($config['services'] as $name => $service) {
$container->setAlias(sprintf('yokai_security_token.resolved.%s', $name), $service);
$container->setAlias(sprintf('yokai_security_token.%s', $name), $service);
}
}
}
40 changes: 27 additions & 13 deletions Entity/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Yokai\SecurityTokenBundle\Entity;

use DateTime;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @author Yann Eugoné <[email protected]>
Expand All @@ -16,9 +15,14 @@ class Token
private $id;

/**
* @var UserInterface
* @var string
*/
private $userClass;

/**
* @var string
*/
private $user;
private $userId;

/**
* @var string
Expand Down Expand Up @@ -56,15 +60,17 @@ class Token
private $usedInformation;

/**
* @param UserInterface $user
* @param string $value
* @param string $purpose
* @param string $duration
* @param array $information
* @param string $userClass
* @param string $userId
* @param string $value
* @param string $purpose
* @param string $duration
* @param array $information
*/
public function __construct(UserInterface $user, $value, $purpose, $duration, array $information)
public function __construct($userClass, $userId, $value, $purpose, $duration, array $information)
{
$this->user = $user;
$this->userClass = $userClass;
$this->userId = $userId;
$this->value = $value;
$this->purpose = $purpose;
$this->createdAt = new DateTime();
Expand All @@ -81,11 +87,19 @@ public function getId()
}

/**
* @return UserInterface
* @return string
*/
public function getUserClass()
{
return $this->userClass;
}

/**
* @return string
*/
public function getUser()
public function getUserId()
{
return $this->user;
return $this->userId;
}

/**
Expand Down
23 changes: 17 additions & 6 deletions Factory/TokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Yokai\SecurityTokenBundle\Factory;

use Symfony\Component\Security\Core\User\UserInterface;
use Yokai\SecurityTokenBundle\Configuration\TokenConfigurationRegistry;
use Yokai\SecurityTokenBundle\Entity\Token;
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface;
use Yokai\SecurityTokenBundle\Manager\UserManagerInterface;

/**
* @author Yann Eugoné <[email protected]>
Expand All @@ -23,24 +23,35 @@ class TokenFactory implements TokenFactoryInterface
private $informationGuesser;

/**
* @param TokenConfigurationRegistry $registry
* @var UserManagerInterface
*/
private $userManager;

/**
* @param TokenConfigurationRegistry $registry
* @param InformationGuesserInterface $informationGuesser
* @param UserManagerInterface $userManager
*/
public function __construct(TokenConfigurationRegistry $registry, InformationGuesserInterface $informationGuesser)
{
public function __construct(
TokenConfigurationRegistry $registry,
InformationGuesserInterface $informationGuesser,
UserManagerInterface $userManager
) {
$this->registry = $registry;
$this->informationGuesser = $informationGuesser;
$this->userManager = $userManager;
}

/**
* @inheritdoc
*/
public function create(UserInterface $user, $purpose)
public function create($user, $purpose)
{
$configuration = $this->registry->get($purpose);

return new Token(
$user,
$this->userManager->getClass($user),
$this->userManager->getId($user),
$configuration->getGenerator()->generate(),
$purpose,
$configuration->getDuration(),
Expand Down
7 changes: 3 additions & 4 deletions Factory/TokenFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Yokai\SecurityTokenBundle\Factory;

use Symfony\Component\Security\Core\User\UserInterface;
use Yokai\SecurityTokenBundle\Entity\Token;

/**
Expand All @@ -11,10 +10,10 @@
interface TokenFactoryInterface
{
/**
* @param UserInterface $user
* @param string $purpose
* @param mixed $user
* @param string $purpose
*
* @return Token
*/
public function create(UserInterface $user, $purpose);
public function create($user, $purpose);
}
Loading

0 comments on commit b9b6fe9

Please sign in to comment.