Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0 #53

Open
wants to merge 2 commits into
base: 2.0
Choose a base branch
from
Open

2.0 #53

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions Command/CreateUserByTypeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace PUGX\MultiUserBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Create a user by type
*
*/
class CreateUserByTypeCommand extends ContainerAwareCommand
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('pugx:user:create-by-type')
->setDescription('Create a user by type.')
->setDefinition(array(
new InputArgument('type', InputArgument::REQUIRED, 'The user type'),
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
new InputArgument('email', InputArgument::REQUIRED, 'The email'),
new InputArgument('password', InputArgument::REQUIRED, 'The password'),
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'),
new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
))
->setHelp(<<<EOT
The <info>pugx:user:create-by-type</info> command creates a user:

<info>php app/console pugx:user:create-by-type buyer matthieu</info>

This interactive shell will ask you for an email and then a password.

You can alternatively specify the email and password as the second and third arguments:

<info>php app/console pugx:user:create-by-type buyer matthieu [email protected] mypassword</info>

You can create a super admin via the super-admin flag:

<info>php app/console pugx:user:create-by-type admin gerard --super-admin</info>

You can create an inactive user (will not be able to log in):

<info>php app/console pugx:user:create-by-type buyer thibault --inactive</info>

EOT
);
}

/**
* @see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$type = $input->getArgument('type');
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$password = $input->getArgument('password');
$inactive = $input->getOption('inactive');
$superadmin = $input->getOption('super-admin');

$manipulator = $this->getContainer()->get('pugx_multi_user.util.user_manipulator');
$manipulator->createByType($type, $username, $password, $email, !$inactive, $superadmin);

$output->writeln(sprintf('Created user <comment>%s</comment>', $username));
}

/**
* @see Command
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('type')) {
$type = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a type:',
function($type) {
if (empty($type)) {
throw new \Exception('Type can not be empty');
}

return $type;
}
);
$input->setArgument('type', $type);
}

if (!$input->getArgument('username')) {
$username = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a username:',
function($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}

return $username;
}
);
$input->setArgument('username', $username);
}

if (!$input->getArgument('email')) {
$email = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose an email:',
function($email) {
if (empty($email)) {
throw new \Exception('Email can not be empty');
}

return $email;
}
);
$input->setArgument('email', $email);
}

if (!$input->getArgument('password')) {
$password = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a password:',
function($password) {
if (empty($password)) {
throw new \Exception('Password can not be empty');
}

return $password;
}
);
$input->setArgument('password', $password);
}
}
}
81 changes: 81 additions & 0 deletions Controller/ProfileManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace PUGX\MultiUserBundle\Controller;

use PUGX\MultiUserBundle\Model\UserDiscriminator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use FOS\UserBundle\Controller\ProfileController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use PUGX\MultiUserBundle\Form\FormFactory;

class ProfileManager
{
/**
*
* @var \PUGX\MultiUserBundle\Model\UserDiscriminator
*/
protected $userDiscriminator;

/**
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;

/**
*
* @var \FOS\UserBundle\Controller\ProfileController
*/
protected $controller;

/**
*
* @var \PUGX\MultiUserBundle\Form\FormFactory
*/
protected $formFactory;

/**
*
* @param \PUGX\MultiUserBundle\Model\UserDiscriminator $userDiscriminator
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param \FOS\UserBundle\Controller\ProfileController $controller
* @param \PUGX\MultiUserBundle\Form\FormFactory $formFactory
*/
public function __construct(UserDiscriminator $userDiscriminator,
ContainerInterface $container,
ProfileController $controller,
FormFactory $formFactory)
{
$this->userDiscriminator = $userDiscriminator;
$this->container = $container;
$this->controller = $controller;
$this->formFactory = $formFactory;
}

/**
*
* @param string $class
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function profile($class)
{
$this->userDiscriminator->setClass($class);

$this->controller->setContainer($this->container);
$result = $this->controller->editAction($this->container->get('request'));
if ($result instanceof RedirectResponse) {
return $result;
}

$template = $this->userDiscriminator->getTemplate('profile');
if (is_null($template)) {
$engine = $this->container->getParameter('fos_user.template.engine');
$template = 'FOSUserBundle:Profile:edit.html.'.$engine;
}

$form = $this->formFactory->createForm();
return $this->container->get('templating')->renderResponse($template, array(
'form' => $form->createView(),
));
}
}
17 changes: 12 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->root('pugx_multi_user');

$supportedDrivers = array('orm');

$rootNode->
children()
->scalarNode('db_driver')
Expand All @@ -31,7 +31,14 @@ public function getConfigTreeBuilder()
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
->end()
->end();


$rootNode->
children()
->scalarNode('user_manager')
->defaultValue('pugx_user.manager.orm_user_manager.default')
->end()
->end();

$rootNode->
children()
->arrayNode('users')->prototype('array')
Expand All @@ -40,7 +47,7 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue('PUGX\MultiUserBundle\Model\UserFactory')->end()
->end()
->end()
->end()
->end()
->children()
Expand Down Expand Up @@ -80,11 +87,11 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()

->end()
->end()
->end();

return $treeBuilder;
}
}
86 changes: 84 additions & 2 deletions DependencyInjection/PUGXMultiUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,95 @@ public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);


/** Extract parameters from config file */
$users = $config['users'];
/** Default users */
$container->setParameter('pugx_user_discriminator_users', $users);

/** Build Conf from parameters in config file */
$conf = $this->buildConf($users);
$container->setParameter('pugx_user.discriminator.conf', $conf);

/** Build User Types from parameters in config file */
$userTypes = $this->buildUserTypes($users);
$container->setParameter('pugx_user.discriminator.user_types', $userTypes);

/** Alias default manager */
$container->setAlias('pugx_user.manager.orm_user_manager', $config['user_manager']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

$loader->load(sprintf('%s.yml', $config['db_driver']));
}


/**
*
* @param array $entities
* @param array $registrationForms
* @param array $profileForms
*/
protected function buildConf(array $users)
{
foreach ($users as $user) {

$class = $user['entity']['class'];

if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('UserDiscriminator, configuration error : "%s" not found', $class));
}

$conf[$class] = array(
'factory' => $user['entity']['factory'],
'registration' => array(
'form' => array(
'type' => $user['registration']['form']['type'],
'name' => $user['registration']['form']['name'],
'validation_groups' => $user['registration']['form']['validation_groups'],
),
'template' => $user['registration']['template'],
),
'profile' => array(
'form' => array(
'type' => $user['profile']['form']['type'],
'name' => $user['profile']['form']['name'],
'validation_groups' => $user['profile']['form']['validation_groups'],
),
'template' => $user['profile']['template'],
),
);
}

return $conf;

}

/**
* Extract the user types from the pugx multi user configuration parameters into an array
* 'type' => 'class'
* e.g. array(
* 'user_one' => 'Acme\UserBundle\Entity\UserOne',
* 'user_two' => 'Acme\UserBundle\Entity\UserTwo',
* )
*
* @param array $entities
*/
protected function buildUserTypes(array $parameters)
{

$userTypes = array();
while ($user = current($parameters)) {
$class = $user['entity']['class'];
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('ControllerListener, configuration error : "%s" not found', $class));
}
$userType = strtolower(trim(key($parameters)));
$userTypes[$userType]= $class;
next($parameters);
}
return $userTypes;
}

}
Loading