public function setEnabled($value)
 {
     if ($this->isEnabled() === true && $value !== false || $this->isEnabled() !== true && $value !== true) {
         //nothing changed
         return $this;
     } elseif (!$value) {
         // disable
         $this->serviceDisabled();
         $this->objects = new ArrayCollection();
         $this->attributes = new ArrayCollection();
         return $this;
     }
     // enable
     foreach ($this->getObjectClasses() as $class) {
         $this->addObject($class);
     }
     $this->attributes['uid']->set($this->user->getUsername());
     $this->serviceEnabled();
     return $this;
 }
 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;
 }
 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);
         }
     }
 }