protected function execute(InputInterface $input, OutputInterface $output)
 {
     $helper = $this->getHelper('question');
     try {
         $this->getContainer()->get('cloud.ldap.userprovider');
     } catch (\Exception $e) {
         $output->writeln("<error>Can't connect to database</error>");
         return 255;
     }
     // check options
     $action = null;
     try {
         $action = $this->getAction($input->getOptions());
     } catch (\InvalidArgumentException $e) {
         $output->writeln('<error>' + $e->getMessage() + '</error>');
     }
     // read username
     if ($input->getArgument('username') !== null) {
         $username = $input->getArgument('username');
     } else {
         $question = new Question('Please enter the name of the User:'******'cloud.ldap.userprovider')->getUsernames());
         $username = $helper->ask($input, $output, $question);
     }
     // check username for existence
     try {
         $this->user = $this->getContainer()->get('cloud.ldap.userprovider')->loadUserByUsername($username);
     } catch (UserNotFoundException $e) {
         $output->writeln('User not found');
         return 1;
     }
     // check for service attribute
     $service = null;
     if ($input->getOptions()['service'] !== null) {
         $serviceName = $input->getOption('service');
         if ($serviceName == '.') {
             $question = new Question('Please enter the name of the Service:');
             $question->setAutocompleterValues($this->getContainer()->get('cloud.ldap.userprovider')->getServiceNames());
             $serviceName = $helper->ask($input, $output, $question);
         }
         $service = $this->user->getService($serviceName);
         if ($service == null) {
             $output->writeln('<error>can\'t find service</error>');
             return 1;
         }
     }
     if ($action == null) {
         $passwords = null;
         if ($service != null) {
             $passwords = $service->getPasswords();
         } else {
             $passwords = $this->user->getPasswords();
         }
         foreach ($passwords as $password) {
             $output->writeln($password->getId());
         }
         return 0;
         // complete listed alls passwords
     }
     // read id
     if ($input->getArgument('id')) {
         $this->passwordId = $input->getArgument('id');
     } else {
         $question = new Question('Please enter password id:');
         if ($service != null) {
             $passwords = $service->getPasswords();
         } else {
             $passwords = $this->user->getPasswords();
         }
         $question->setAutocompleterValues(array_keys($passwords));
         $this->passwordId = $helper->ask($input, $output, $question);
     }
     if ($action == "delete") {
         if (!$input->getOption('force')) {
             $question = new ConfirmationQuestion('You realy whant to delete this password? [y/N]:', false);
             if (!$helper->ask($input, $output, $question)) {
                 $output->writeln('<error>canceled by user, if you use a script use \'-f\' to force delete</error>');
                 return 1;
             }
         }
         if ($service != null) {
             $password = $service->getPassword($this->passwordId);
             if ($password == null) {
                 $output->writeln('<error>can\'t find password with this id</error>');
                 return 1;
             }
             $service->removePassword($password);
         } else {
             $password = $this->user->getPassword($this->passwordId);
             if ($password == null) {
                 $output->writeln('<error>can\'t find password with this id</error>');
                 return 1;
             }
             $this->user->removePassword($password);
         }
         $this->getContainer()->get('cloud.ldap.util.usermanipulator')->update($this->user);
         return 0;
         // complete delete a password
     }
     // read password
     if ($input->getArgument('password')) {
         $this->password = $input->getArgument('password');
     } else {
         $question = new Question('Please enter password:'******'cloud.ldap.util.usermanipulator')->update($this->user);
         return 0;
     }
     if ($action == "modify") {
         if ($service != null) {
             $password = $service->getPassword($this->passwordId);
         } else {
             $password = $this->user->getPassword($this->passwordId);
         }
         $password->setPasswordPlain($this->password);
         $this->getContainer()->get('cloud.ldap')->updateUser($this->user);
         return 0;
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $helper = $this->getHelper('question');
     try {
         $this->getContainer()->get('cloud.ldap.userprovider');
     } catch (\Exception $e) {
         $output->writeln("<error>Can't connect to database</error>");
         return 255;
     }
     // read username
     $username = null;
     if ($input->getArgument('username') !== null) {
         $username = $input->getArgument('username');
     } else {
         $question = new Question('Please enter the name of the User:'******'cloud.ldap.userprovider')->getUsernames());
         $username = $helper->ask($input, $output, $question);
     }
     // check username for existence
     try {
         $this->user = $this->getContainer()->get('cloud.ldap.userprovider')->loadUserByUsername($username);
     } catch (UsernameNotFoundException $e) {
         $output->writeln('<error>User not found</error>');
         return 1;
     }
     $action = null;
     if ($input->getArgument('action') !== null) {
         $action = $input->getArgument('action');
     } else {
         $question = new Question('Please enter a action (list|show|enable|disable|enableMasterPassword|disableMasterPassword):');
         $question->setAutocompleterValues(['list', 'show', 'enable', 'disable', 'enableMasterPassword', 'disableMasterPassword']);
         $action = $helper->ask($input, $output, $question);
     }
     if ($action === 'list') {
         $table = new Table($output);
         $table->setHeaders(array('Service Name', 'Enabled', 'MasterPasswordEnabled', 'passwords'));
         foreach ($this->user->getServices() as $service) {
             $table->addRow([$service->getName(), $service->isEnabled() ? 'X' : '', $service->isMasterPasswordEnabled() ? 'X' : '', implode(',', array_map(function ($password) {
                 return !$password->isMasterPassword() ? $password->getId() : '';
             }, $service->getPasswords()))]);
         }
         $table->render();
         return 0;
     }
     // read service
     $serviceName = null;
     if ($input->getArgument('service')) {
         $serviceName = $input->getArgument('service');
     } else {
         $question = new Question('Please enter service name:');
         $question->setAutocompleterValues(array_map(function ($service) {
             return $service->getName();
         }, $this->user->getServices()));
         $serviceName = $helper->ask($input, $output, $question);
     }
     $this->service = $this->user->getService($serviceName);
     switch ($action) {
         case 'show':
             //@TODO
             break;
         case 'enable':
             $this->service->setEnabled(true);
             $this->service->setMasterPasswordEnabled(true);
             $this->getContainer()->get('cloud.ldap.util.usermanipulator')->update($this->user);
             break;
         case 'disable':
             if (!$input->getOption('force')) {
                 $question = new ConfirmationQuestion('You really want to disable this service for the user \'' . $this->user->getUsername() . "?\n<error>all passwords get deleted</error> [y/N]:", false);
                 if (!$helper->ask($input, $output, $question)) {
                     $output->writeln('<error>canceled by user, if you use a script use \'-f\' to force delete</error>');
                     return 1;
                 }
             }
             $this->service->setEnabled(false);
             $this->getContainer()->get('cloud.ldap.util.usermanipulator')->update($this->user);
             break;
         case 'enableMasterPassword':
             $this->service->setMasterPasswordEnabled(true);
             $this->getContainer()->get('cloud.ldap.util.usermanipulator')->update($this->user);
             break;
         case 'disableMasterPassword':
             $this->service->setMasterPasswordEnabled(false);
             $this->getContainer()->get('cloud.ldap.util.usermanipulator')->update($this->user);
             break;
         default:
             $output->writeln('<error>action not found</error>');
             return 1;
     }
     return 0;
 }
 /**
  * @expectedException InvalidArgumentException
  */
 public function testException2()
 {
     $user = new User("testUser");
     $user->addPassword(new Password("123456", "tetID"));
     $user->addPassword(new Password("123456", "tetID"));
 }
 public function delete(User $user)
 {
     $dn = 'uid=' . $user->getUsername() . ',ou=users,' . $this->baseDn;
     if ($this->client->isEntityExist($dn)) {
         $this->client->delete($dn);
     }
     foreach ($user->getServices() as $service) {
         $dn = 'uid=' . $user->getUsername() . ',ou=users,dc=' . $service->getName() . ',' . $this->baseDn;
         if ($this->client->isEntityExist($dn)) {
             $this->client->delete($dn);
         }
     }
 }
 /**
  *
  * @param User $user
  */
 public function setUser(User $user)
 {
     $this->user = $user;
     if (!in_array($this, $user->getServices())) {
         $this->user->addService($this);
     }
 }