|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2022, ownCloud GmbH. |
| 4 | + * @license AGPL-3.0 |
| 5 | + * |
| 6 | + * This code is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 8 | + * as published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +namespace OCA\User_LDAP\Command; |
| 21 | + |
| 22 | +use Symfony\Component\Console\Command\Command; |
| 23 | +use Symfony\Component\Console\Helper\Table; |
| 24 | +use Symfony\Component\Console\Input\InputArgument; |
| 25 | +use Symfony\Component\Console\Input\InputInterface; |
| 26 | +use Symfony\Component\Console\Input\InputOption; |
| 27 | +use Symfony\Component\Console\Output\OutputInterface; |
| 28 | + |
| 29 | +use OCA\User_LDAP\Mapping\UserMapping; |
| 30 | +use OCA\User_LDAP\Helper as LDAPHelper; |
| 31 | +use OCA\User_LDAP\User_Proxy; |
| 32 | + |
| 33 | +class RemapUser extends Command { |
| 34 | + /** @var \OCA\User_LDAP\User_Proxy */ |
| 35 | + protected $backend; |
| 36 | + |
| 37 | + /** @var \OCA\User_LDAP\Helper */ |
| 38 | + protected $helper; |
| 39 | + |
| 40 | + /** @var \OCA\User_LDAP\Mapping\UserMapping */ |
| 41 | + protected $mapping; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param User_Proxy $uBackend |
| 45 | + * @param LDAPHelper $helper |
| 46 | + * @param UserMapping $mapping |
| 47 | + */ |
| 48 | + public function __construct(User_Proxy $uBackend, LDAPHelper $helper, UserMapping $mapping) { |
| 49 | + $this->backend = $uBackend; |
| 50 | + $this->helper = $helper; |
| 51 | + $this->mapping = $mapping; |
| 52 | + parent::__construct(); |
| 53 | + } |
| 54 | + |
| 55 | + protected function configure() { |
| 56 | + $this |
| 57 | + ->setName('ldap:remap-user') |
| 58 | + ->setDescription('checks whether a user exists on LDAP') |
| 59 | + ->addArgument( |
| 60 | + 'ocName', |
| 61 | + InputArgument::REQUIRED, |
| 62 | + 'the user name as used in ownCloud' |
| 63 | + ) |
| 64 | + ->addOption( |
| 65 | + 'force', |
| 66 | + null, |
| 67 | + InputOption::VALUE_NONE, |
| 68 | + 'ignores disabled LDAP configuration' |
| 69 | + ) |
| 70 | + ; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param InputInterface $input |
| 75 | + * @param OutputInterface $output |
| 76 | + * @return int|void|null |
| 77 | + */ |
| 78 | + protected function execute(InputInterface $input, OutputInterface $output) { |
| 79 | + $uid = $input->getArgument('ocName'); |
| 80 | + $this->isAllowed($input->getOption('force')); |
| 81 | + //$this->confirmUserIsMapped($uid); |
| 82 | + |
| 83 | + $mappedData = $this->getMappedUUIDAndDN($uid); |
| 84 | + if ($mappedData['mappedDN'] === false || $mappedData['mappedUUID'] === false) { |
| 85 | + $output->writeln('User not mapped yet. Try to sync it with the user:sync command'); |
| 86 | + return -1; |
| 87 | + } |
| 88 | + |
| 89 | + $output->writeln('Mapped user found in the DB:'); |
| 90 | + $table1 = new Table($output); |
| 91 | + $table1->setHeaders(['username', 'uuid', 'dn']); |
| 92 | + $table1->addRow([$uid, $mappedData['mappedUUID'], $mappedData['mappedDN']]); |
| 93 | + $table1->render(); |
| 94 | + |
| 95 | + $entries = $this->backend->findUsername($uid); |
| 96 | + $entryCount = \count($entries); |
| 97 | + |
| 98 | + $output->writeln(''); |
| 99 | + $output->writeln('Candidates found in LDAP:'); |
| 100 | + $table2 = new Table($output); |
| 101 | + $table2->setHeaders(['username', 'uuid', 'dn']); |
| 102 | + foreach ($entries as $entry) { |
| 103 | + $table2->addRow([$entry['owncloud_name'], $entry['directory_uuid'], $entry['dn']]); |
| 104 | + } |
| 105 | + $table2->render(); |
| 106 | + |
| 107 | + if ($entryCount > 1) { |
| 108 | + $output->writeln('<error>Found too many candidates in LDAP for the target user, remapping isn\'t possible</error>'); |
| 109 | + return 1; |
| 110 | + } elseif ($entryCount < 1) { |
| 111 | + $output->writeln('<error>User not found in LDAP. Consider removing the ownCloud\'s account</error>'); |
| 112 | + return 2; |
| 113 | + } |
| 114 | + |
| 115 | + if ($mappedData['mappedDN'] === $entries[0]['dn'] && $mappedData['mappedUUID'] === $entries[0]['directory_uuid']) { |
| 116 | + $output->writeln('The same user is already mapped. Nothing to do'); |
| 117 | + return 0; // just show a message and return a success code |
| 118 | + } |
| 119 | + |
| 120 | + $result = $this->mapping->replaceUUIDAndDN($uid, $entries[0]['dn'], $entries[0]['directory_uuid']); |
| 121 | + if ($result === false) { |
| 122 | + $output->writeln("<error>Failed to replace mapping data for user {$uid}</error>"); |
| 123 | + return 3; |
| 124 | + } |
| 125 | + $output->writeln('Mapping data replaced'); |
| 126 | + } |
| 127 | + |
| 128 | + private function getMappedUUIDAndDN($username) { |
| 129 | + $dn = $this->mapping->getDNByName($username); |
| 130 | + $uuid = $this->mapping->getUUIDByName($username); |
| 131 | + return [ |
| 132 | + 'mappedDN' => $dn, |
| 133 | + 'mappedUUID' => $uuid, |
| 134 | + ]; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * checks whether a user is actually mapped |
| 139 | + * @param string $ocName the username as used in ownCloud |
| 140 | + * @throws \Exception |
| 141 | + * @return true |
| 142 | + */ |
| 143 | + private function confirmUserIsMapped($ocName) { |
| 144 | + $dn = $this->mapping->getDNByName($ocName); |
| 145 | + if ($dn === false) { |
| 146 | + throw new \Exception('The given user is not a recognized LDAP user.'); |
| 147 | + } |
| 148 | + |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * checks whether the setup allows reliable checking of LDAP user existence |
| 154 | + * @throws \Exception |
| 155 | + * @return true |
| 156 | + */ |
| 157 | + private function isAllowed($force) { |
| 158 | + if ($this->helper->haveDisabledConfigurations() && !$force) { |
| 159 | + throw new \Exception('Cannot check user existence, because ' |
| 160 | + . 'disabled LDAP configurations are present.'); |
| 161 | + } |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | +} |
0 commit comments