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); } } }
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; }
/** * * @param User $user */ public function setUser(User $user) { $this->user = $user; if (!in_array($this, $user->getServices())) { $this->user->addService($this); } }