diff --git a/bin/console b/bin/console
index 552cb672d0..7c2d146ea2 100755
--- a/bin/console
+++ b/bin/console
@@ -46,6 +46,7 @@ use Alchemy\Phrasea\Command\Task\TaskRun;
use Alchemy\Phrasea\Command\Task\TaskStart;
use Alchemy\Phrasea\Command\Task\TaskState;
use Alchemy\Phrasea\Command\Task\TaskStop;
+use Alchemy\Phrasea\Command\User\UserCreateCommand;
use Alchemy\Phrasea\Command\UpgradeDBDatas;
require_once __DIR__ . '/../lib/autoload.php';
@@ -110,6 +111,8 @@ $cli->command(new \module_console_fieldsMerge('fields:merge'));
$cli->command(new CreateCollection('collection:create'));
$cli->command(new CreateDataboxCommand('databox:create'));
+$cli->command(new UserCreateCommand('user:create'));
+
$cli->command(new RecordAdd('records:add'));
$cli->command(new RescanTechnicalDatas('records:rescan-technical-datas'));
$cli->command(new BuildMissingSubdefs('records:build-missing-subdefs'));
diff --git a/lib/Alchemy/Phrasea/Command/User/UserCreateCommand.php b/lib/Alchemy/Phrasea/Command/User/UserCreateCommand.php
new file mode 100644
index 0000000000..510e9f458f
--- /dev/null
+++ b/lib/Alchemy/Phrasea/Command/User/UserCreateCommand.php
@@ -0,0 +1,223 @@
+setDescription('Create user in Phraseanet')
+ ->addOption('user_login', null, InputOption::VALUE_REQUIRED, 'The desired login for created user.')
+ ->addOption('user_mail', null, InputOption::VALUE_OPTIONAL, 'The desired mail for created user.')
+ ->addOption('user_password', null, InputOption::VALUE_OPTIONAL, 'The desired password')
+ ->addOption('send_mail_confirm', null, InputOption::VALUE_NONE, 'Send an email to user, for validate email.')
+ ->addOption('send_mail_password', null, InputOption::VALUE_NONE, 'Send an email to user, for define password')
+ ->addOption('model_number', null, InputOption::VALUE_OPTIONAL, 'Id of model')
+ ->addOption('user_gender', null, InputOption::VALUE_OPTIONAL, 'The gender for created user.')
+ ->addOption('user_firstname', null, InputOption::VALUE_OPTIONAL, 'The first name for created user.')
+ ->addOption('user_lastname', null, InputOption::VALUE_OPTIONAL, 'The last name for created user.')
+ ->addOption('user_adress', null, InputOption::VALUE_OPTIONAL, 'The adress name for created user.')
+ ->addOption('user_zipcode', null, InputOption::VALUE_OPTIONAL, 'The zip code for created user.')
+ ->addOption('user_city', null, InputOption::VALUE_OPTIONAL, 'The city for created user.')
+ ->addOption('user_compagny', null, InputOption::VALUE_OPTIONAL, 'The compagny for created user.')
+ ->addOption('user_job', null, InputOption::VALUE_OPTIONAL, 'The job for created user.')
+ ->addOption('user_activitie', null, InputOption::VALUE_OPTIONAL, 'The activitie for created user.')
+ ->addOption('user_phone', null, InputOption::VALUE_OPTIONAL, 'The phone number for created user.')
+ ->addOption('user_fax', null, InputOption::VALUE_OPTIONAL, 'The fax number for created user.')
+ ->setHelp('');
+
+ return $this;
+ }
+
+ protected function doExecute(InputInterface $input, OutputInterface $output)
+ {
+
+ $userLogin = $input->getOption('user_login');
+ $userMail = $input->getOption('user_mail');
+ $userPassword = $input->getOption('user_password');
+ $sendMailConfirm = $input->getOption('send_mail_confirm');
+ $sendMailPassword = $input->getOption('send_mail_password');
+ $modelNumber = $input->getOption('model_number');
+ $userGender = $input->getOption('user_gender');
+ $userFirstName = $input->getOption('user_firstname');
+ $userLastName = $input->getOption('user_lastname');
+ $userAdress = $input->getOption('user_adress');
+ $userZipCode = $input->getOption('user_zipcode');
+ $userCity = $input->getOption('user_city');
+ $userCompagny = $input->getOption('user_compagny');
+ $userJob = $input->getOption('user_job');
+ $userActivity = $input->getOption('user_activitie');
+ $userPhone = $input->getOption('user_phone');
+ $userFax = $input->getOption('user_fax');
+
+ $userRepository = $this->container['repo.users'];
+
+ if ($userMail) {
+ if (!\Swift_Validate::email($userMail)) {
+ $output->writeln('Invalid mail address');
+ return 0;
+ }
+
+ if (null !== $userRepository->findByEmail($userMail)) {
+ $output->writeln('An user exist with this email.');
+ return 0;
+ }
+
+ }
+
+ $password = (!is_null($userPassword)) ? $userPassword : $this->container['random.medium']->generateString(128);
+ $userManipulator = $this->container['manipulator.user'];
+ $user = $userManipulator->createUser($userLogin, $password, $userMail);
+
+ if ($userGender) {
+ if (null === $gender = $this->verifyGender($userGender)) {
+ $output->writeln('Gender '.$userGender.' not exists.>');
+ }
+ $user->setGender($gender);
+ }
+
+ if($userFirstName) $user->setFirstName($userFirstName);
+ if($userLastName) $user->setLastName($userLastName);
+ if($userAdress) $user->setAddress($userAdress);
+ if($userZipCode) $user->setZipCode($userZipCode);
+ if($userCity) $user->setCity($userCity);
+ if($userCompagny) $user->setCompany($userCompagny);
+ if($userJob) $user->setJob($userJob);
+ if($userActivity) $user->setActivity($userActivity);
+ if($userPhone) $user->setPhone($userPhone);
+ if($userFax) $user->setFax($userFax);
+
+ if ($sendMailPassword and $userMail and is_null($userPassword)) {
+ $this->sendPasswordSetupMail($user);
+ }
+
+ if ($sendMailConfirm and $userMail) {
+ $user->setMailLocked(true);
+ $this->sendAccountUnlockEmail($user);
+ }
+
+ if ($modelNumber) {
+ $template = $userRepository->find($modelNumber);
+ if (!$template) {
+ $output->writeln('Model '.$modelNumber.' not found.>');
+ } else {
+ $base_ids = [];
+ foreach ($this->container->getApplicationBox()->get_databoxes() as $databox) {
+ foreach ($databox->get_collections() as $collection) {
+ $base_ids[] = $collection->get_base_id();
+ }
+ }
+ $this->container->getAclForUser($user)->apply_model($template, $base_ids);
+ }
+ }
+
+ $this->container['orm.em']->flush();
+
+ $output->writeln("Create new user successful !");
+
+ return 0;
+ }
+
+ /**
+ * Get gender for user
+ * @param $type
+ * @return int|null
+ */
+ private function verifyGender($type)
+ {
+ switch (strtolower($type)) {
+ case "mlle.":
+ case "mlle":
+ case "miss":
+ case "mademoiselle":
+ case "0":
+ $gender = User::GENDER_MISS;
+ break;
+ case "mme":
+ case "madame":
+ case "ms":
+ case "ms.":
+ case "1":
+ $gender = User::GENDER_MRS;
+ break;
+ case "m":
+ case "m.":
+ case "mr":
+ case "mr.":
+ case "monsieur":
+ case "mister":
+ case "2":
+ $gender = User::GENDER_MR;
+ break;
+ default:
+ $gender = null;
+ }
+ return $gender;
+ }
+
+ /**
+ * Send mail for renew password
+ * @param User $user
+ */
+ public function sendPasswordSetupMail(User $user)
+ {
+ $this->setDelivererLocator(new LazyLocator($this->container, 'notification.deliverer'));
+ $receiver = Receiver::fromUser($user);
+
+ $token = $this->container['manipulator.token']->createResetPasswordToken($user);
+
+ $mail = MailRequestPasswordSetup::create($this->container, $receiver);
+ $servername = $this->container['conf']->get('servername');
+ $mail->setButtonUrl('http://'.$servername.'/login/renew-password/?token='.$token->getValue());
+ $mail->setLogin($user->getLogin());
+
+ $this->deliver($mail);
+ }
+
+ /**
+ * @param User $user
+ */
+ public function sendAccountUnlockEmail(User $user)
+ {
+ $this->setDelivererLocator(new LazyLocator($this->container, 'notification.deliverer'));
+ $receiver = Receiver::fromUser($user);
+
+ $token = $this->container['manipulator.token']->createAccountUnlockToken($user);
+
+ $mail = MailRequestEmailConfirmation::create($this->container, $receiver);
+ $servername = $this->container['conf']->get('servername');
+ $mail->setButtonUrl('http://'.$servername.'/login/register-confirm/?code='.$token->getValue());
+ $mail->setExpiration($token->getExpiration());
+
+ $this->deliver($mail);
+ }
+
+}